Skip to content

Instantly share code, notes, and snippets.

@Zenithar
Created September 6, 2018 16:27
Show Gist options
  • Save Zenithar/bc25f8136a88289d5dcee7f884719d05 to your computer and use it in GitHub Desktop.
Save Zenithar/bc25f8136a88289d5dcee7f884719d05 to your computer and use it in GitHub Desktop.
package pbkdf1_ms
import (
"crypto/sha1"
"strconv"
)
// Key implements C# PasswordDeriveBytes
func Key(password, salt []byte, iterations int, dkLen int) []byte {
// Prepare base hash
base := append(password, salt...)
// Process derivation through iteration count
for i := 0; i < iterations-1; i++ {
h := sha1.Sum(base[:])
base = h[:]
}
last := sha1.Sum(base[:])
// Finalize hash
out := last[:]
// Process tailing bytes
counter := 1
var extra []byte
// If ouput is smaller than expected derived key length
for len(out) < dkLen {
// Concat counter
extra = strconv.AppendInt(extra, int64(counter), 10)
// Concat base hash
extra = append(extra, base[:]...)
// Sum it
extraHash := sha1.Sum(extra)
// Append to output
out = append(out, extraHash[:]...)
// Increment counter
counter++
}
// Return result
return out[:dkLen]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment