Use multiple KDFs (add Scrypt for AES256)

This commit is contained in:
2025-05-08 17:26:12 +00:00
parent ce0b5d4bf5
commit ac5314396e
4 changed files with 24 additions and 11 deletions
+18 -6
View File
@@ -4,21 +4,33 @@ import (
"runtime"
"golang.org/x/crypto/argon2"
"golang.org/x/crypto/scrypt"
)
const (
// parameters for Argon2
argonTime = 8 // set to pass 1-second test in dev environment
argonMemory = 384 * 1024 // 384 MB
argonKeyLen = 32 // 256 bits, key length for both ChaCha20 and AES256
// parameters for Scrypt
scryptN = 262144
scryptR = 8 // block size, recommended to be 8
scryptP = 1 // parallelization factor, recommended to be 1
// general constants
saltSize = 16
keyLen = 32 // 256 bits, key length for both algorithms
saltSize = 16 // 128 bits, recommended salt size for both algorithms
)
var argonThreads = uint8(runtime.NumCPU())
var threads = uint8(runtime.NumCPU())
// DeriveKey derives an encryption key from a passphrase using Argon2.
func deriveKey(passphrase []byte, salt []byte) []byte {
return argon2.IDKey(passphrase, salt, argonTime, argonMemory, argonThreads, argonKeyLen)
// DeriveKeyArgon2 derives an encryption key from a passphrase using Argon2.
func deriveKeyArgon2(passphrase []byte, salt []byte) []byte {
return argon2.IDKey(passphrase, salt, argonTime, argonMemory, threads, keyLen)
}
// DeriveKeyScrypt derives an encryption key from a passphrase using Scrypt.
func deriveKeyScrypt(passphrase []byte, salt []byte) []byte {
key, _ := scrypt.Key(passphrase, salt, scryptN, scryptR, scryptP, keyLen)
return key
}