(Very) Basic Intro to the Scrypt Hash (2024)

Scrypt is a slow-by-design key derivation function designed to create strong cryptographic keys. Simply put, the purpose of the Scrypt hash is to create a fingerprint of its input data but to do it very slowly. A common use-case is to create a strong private key from a password, where the new private key is longer and more secure. Here at boot.dev, we use a similar KDF for securing user passwords.

Let’s pretend your password is password1234. By using Scrypt, we can extend that deterministically into a 256-bit key:

password1234 -> AwEEDA4HCwQFAA8DAwwHDQwPDwUOBwoOCQACAgUJBQ0JAAYNBAMCDQ4JCQgLDwcGDQMDDgMKAQsNBAkLAwsACA==

That long 256-bit key can now be used as a private key to encrypt and decrypt data. For example, it could be the key in an AES-256 cipher.

Some cryptocurrencies, like Litecoin, use Scrypt as their proof-of-work algorithm due to how slow and memory-intensive the key derivation process is. By using a slower and more memory-intensive algorithm, it’s harder for engineers to create specialized hardware (ASICs) to mine the coin.

Other hash function explainers πŸ”—

Before we move on, if you’re looking for an explanation of a different hash function, we may have you covered

  • SHA-2 Hash Step by Step
  • Bcrypt Step by Step
  • (Very) Basic Intro to Hash Functions

Why Not Encrypt With The Password Directly? πŸ”—

Most encryption algorithms, including AES-256, require that a key of sufficient length is used. By hashing the password, we can derive a longer, more secure, fixed-size key.

Furthermore, using a KDF like Scrypt provides additional benefits over a traditional hash function like SHA-2:

  • Computationally expensive and slow
  • Memory intensive (potentially several gigabytes of RAM is used to execute the hash)

Often times brute-force attackers will try to break encryption by guessing passwords over and over until they get it right. AES-256 and SHA-2 are fast, so an attacker would be able to guess many passwords per second. By using a slow hashing function like Scrypt to derive a key, we can force the attacker to waste more resources trying to break in.

Scrypt Step-by-Step πŸ”—

Scrypt can be visualized by some psuedo-code:

func Scrypt(passphrase, // string of characters to be hashedsalt, // random saltcostFactor, // CPU/Memory cost, must be power of 2blockSizeFactor,parallelizationFactor, // (1..232-1 * hLen/MFlen)desiredKeyLen // Desired key length in bytes) derivedKey {// we'll get to this}

Let’s go through the steps of converting those inputs into the desired derivedKey

1 - Define Blocksize πŸ”—

const blockSize = 128 * blockSizeFactor

2 - Generate Initial Salt πŸ”—

Scrypt uses PBKDF2 as a child key-derivation function. We use it to generate an initial salt. PBKDF2 has the following signature:

func PBKDF2(prf,password,salt,numIterations,desiredKeyLen) derivedKey {}

We use it as follows:

const initialSalt = PBKDF2(HMAC-SHA256, passphrase, salt, 1, blockSize * parallelizationFactor)

3 - Mix Salt πŸ”—

Next, we mix the salt. We split initialSalt into splitSalt, which is a 2D array of bytes. Each sub-array contains 1024 bytes

splitSalt := [][1024]byte(initialSalt)for i, block := range splitSalt {newBlock := roMix(block, costFactor)splitSalt[i] = newBlock}

Where roMix is the following function:

func roMix(block, iterations){v := []x := blockfor i := 0; i < iterations; i++ {v[i] = xx = blockMix(x)}for i := 0; i < iterations; i++ {j := integerify(x) % iterationsx = blockMix(x ^ v[j])}return x}

integerify is defined by RFC-7914 and blockMix is:

func blockMix(block){r := len(block) / 128// split block into an array of 2r 64-byte chunkschunks := get2r64ByteChunks()x := chunks[len(chunks)-1]y := []for i := 0; i < len(chunks); i++{x = salsa20-8(x ^ chunks[i])y[i] = x}return [y[0], y[2], ...y[2r-2], y[1], y[3], ...y[2r-1]]}

salsa20-8 is the 8-round version of the algorithm defined here.

4 - Finalize Salt πŸ”—

Now splitSalt has been mixed in such a computationally exhausting way that we will call it an expensiveSalt. Expensive salt will be a single array of bytes, so we need to concatenate all the subarrays in splitSalt.

expensiveSalt := append([], splitSalt...)

5 - Return Final KDF πŸ”—

return PBKDF2(HMAC-SHA256, passphrase, expensiveSalt, 1, desiredKeyLen)

The final pseudocode for our top level function is as follows:

func Scrypt(passphrase, // string of characters to be hashedsalt, // random saltcostFactor, // CPU/Memory cost, must be power of 2blockSizeFactor,parallelizationFactor, // (1..232-1 * hLen/MFlen)desiredKeyLen // Desired key length in bytes) derivedKey {const blockSize = 128 * blockSizeFactorconst initialSalt = PBKDF2(HMAC-SHA256, passphrase, salt, 1, blockSize * parallelizationFactor)splitSalt := [][1024]byte(initialSalt)for i, block := range splitSalt {newBlock := roMix(block, costFactor)splitSalt[i] = newBlock}expensiveSalt := append([], splitSalt...)return PBKDF2(HMAC-SHA256, passphrase, expensiveSalt, 1, desiredKeyLen)}

Or, if you prefer, the pseudocode as defined by Wikipedia:

Function scrypt Inputs: Passphrase: Bytes string of characters to be hashed Salt: Bytes random salt CostFactor (N): Integer CPU/memory cost parameter - Must be a power of 2 (e.g. 1024) BlockSizeFactor (r): Integer blocksize parameter (8 is commonly used) ParallelizationFactor (p): Integer Parallelization parameter. (1..232-1 * hLen/MFlen) DesiredKeyLen: Integer Desired key length in bytes Output: DerivedKey: Bytes array of bytes, DesiredKeyLen long Step 1. Generate expensive salt blockSize ← 128*BlockSizeFactor //Length (in bytes) of the SMix mixing function output (e.g. 128*8 = 1024 bytes) Use PBKDF2 to generate initial 128*BlockSizeFactor*p bytes of data (e.g. 128*8*3 = 3072 bytes) Treat the result as an array of p elements, each entry being blocksize bytes (e.g. 3 elements, each 1024 bytes) [B0...Bpβˆ’1] ← PBKDF2HMAC-SHA256(Passphrase, Salt, 1, blockSize*ParallelizationFactor) Mix each block in B Costfactor times using ROMix function (each block can be mixed in parallel) for i ← 0 to p-1 do Bi ← ROMix(Bi, CostFactor) All the elements of B is our new "expensive" salt expensiveSalt ← B0βˆ₯B1βˆ₯B2βˆ₯ ... βˆ₯Bp-1 //where βˆ₯ is concatenation Step 2. Use PBKDF2 to generate the desired number of bytes, but using the expensive salt we just generated return PBKDF2HMAC-SHA256(Passphrase, expensiveSalt, 1, DesiredKeyLen);

Find a problem with this article?

Report an issue on GitHub

(Very) Basic Intro to the Scrypt Hash (2024)
Top Articles
Rukuri - Arca del Gusto - Slow Food Foundation
How much does it cost to use M1? | M1 Help Center
Antisis City/Antisis City Gym
Public Opinion Obituaries Chambersburg Pa
Riverrun Rv Park Middletown Photos
It's Official: Sabrina Carpenter's Bangs Are Taking Over TikTok
Somboun Asian Market
No Limit Telegram Channel
La connexion Γ  Mon Compte
CKS is only available in the UK | NICE
Craigslist Mexico Cancun
The Pope's Exorcist Showtimes Near Cinemark Hollywood Movies 20
Moviesda Dubbed Tamil Movies
Giovanna Ewbank Nua
Catsweb Tx State
Osrs Blessed Axe
Valentina Gonzalez Leak
Job Shop Hearthside Schedule
Puretalkusa.com/Amac
Niche Crime Rate
Officialmilarosee
Welcome to GradeBook
Site : Storagealamogordo.com Easy Call
Our History
Free Personals Like Craigslist Nh
How to Grow and Care for Four O'Clock Plants
Hannaford To-Go: Grocery Curbside Pickup
Sadie Sink Reveals She Struggles With Imposter Syndrome
Low Tide In Twilight Ch 52
Bento - A link in bio, but rich and beautiful.
Skycurve Replacement Mat
Makemv Splunk
10-Day Weather Forecast for Santa Cruz, CA - The Weather Channel | weather.com
Black Adam Showtimes Near Amc Deptford 8
T&J Agnes Theaters
AI-Powered Free Online Flashcards for Studying | Kahoot!
Studentvue Columbia Heights
Danielle Ranslow Obituary
Electric Toothbrush Feature Crossword
Booknet.com Contract Marriage 2
Dickdrainersx Jessica Marie
How Big Is 776 000 Acres On A Map
20 Mr. Miyagi Inspirational Quotes For Wisdom
Market Place Tulsa Ok
Grace Family Church Land O Lakes
Wild Fork Foods Login
Game Like Tales Of Androgyny
Hampton Inn Corbin Ky Bed Bugs
Autozone Battery Hold Down
David Turner Evangelist Net Worth
Buildapc Deals
Craigslist Yard Sales In Murrells Inlet
Latest Posts
Article information

Author: Annamae Dooley

Last Updated:

Views: 5266

Rating: 4.4 / 5 (45 voted)

Reviews: 92% of readers found this page helpful

Author information

Name: Annamae Dooley

Birthday: 2001-07-26

Address: 9687 Tambra Meadow, Bradleyhaven, TN 53219

Phone: +9316045904039

Job: Future Coordinator

Hobby: Archery, Couponing, Poi, Kite flying, Knitting, Rappelling, Baseball

Introduction: My name is Annamae Dooley, I am a witty, quaint, lovely, clever, rich, sparkling, powerful person who loves writing and wants to share my knowledge and understanding with you.