Files
HackBrowserData/internal/crypto/pbkdf2.go
T
Roger 3e9150249c refactor: rename core types and spell out every name (#677)
Def, Request, DirID, and Source said too little, and short variables and doc comments that skip their name hid intent.
varnamelen and staticcheck ST1020-22 now keep both from coming back.
2026-11-02 11:06:05 +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, keyLen int, newHash func() hash.Hash) []byte {
prf := hmac.New(newHash, password)
hashLen := prf.Size()
numBlocks := (keyLen + 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[:keyLen]
}