Files
HackBrowserData/internal/crypto/pbkdf2.go
T
Roger 9e9d1c531d refactor: rename identifiers and expand the browser table (#685)
Names say what each thing is: no package stutter, one word per concept, spelled out, none that differ only in case.
The browser table puts one field per line. Wire names (flags, output fields, keys.json) are unchanged.
2026-11-09 17:25:35 +08:00

41 lines
1.0 KiB
Go

package crypto
import (
"crypto/hmac"
"hash"
)
// PBKDF2Key is adapted from golang.org/x/crypto/pbkdf2.
func PBKDF2Key(password, salt []byte, iterations, keyLength int, newHash func() hash.Hash) []byte {
prf := hmac.New(newHash, password)
hashLen := prf.Size()
numBlocks := (keyLength + hashLen - 1) / hashLen
var blockIndex [4]byte
derivedKey := make([]byte, 0, numBlocks*hashLen)
previous := make([]byte, hashLen)
for block := 1; block <= numBlocks; block++ {
prf.Reset()
prf.Write(salt)
blockIndex[0] = byte(block >> 24)
blockIndex[1] = byte(block >> 16)
blockIndex[2] = byte(block >> 8)
blockIndex[3] = byte(block)
prf.Write(blockIndex[:4])
derivedKey = prf.Sum(derivedKey)
lastBlock := derivedKey[len(derivedKey)-hashLen:]
copy(previous, lastBlock)
for iteration := 2; iteration <= iterations; iteration++ {
prf.Reset()
prf.Write(previous)
previous = previous[:0]
previous = prf.Sum(previous)
for byteIndex := range previous {
lastBlock[byteIndex] ^= previous[byteIndex]
}
}
}
return derivedKey[:keyLength]
}