/
derive_seed.go
35 lines (31 loc) · 996 Bytes
/
derive_seed.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
package bip39
import (
"crypto/sha512"
"strings"
"golang.org/x/crypto/pbkdf2"
)
const (
// SeedSaltPrefix is the salt used for BIP39 seed generation via the PBKDF2 function.
SeedSaltPrefix = "mnemonic"
// SeedIterationCount is the PBKDF2 iteration count used to generate seeds.
SeedKDFIterationCount = 2048
// SeedLength is the output size of BIP39 seeds generated by the PBKDF2 function.
SeedLength = 64
)
// DeriveSeed determinstically converts a BIP39 mnemonic and passphrase
// into 64-bytes suitable for use as a cryptographic key.
//
// NOTE: the validity of the mnemonic is not checked in this function.
// Use DecodeWords to confirm the words and checksum are valid before
// deriving key material from a user-given mnemonic.
func DeriveSeed(words []string, passphrase string) []byte {
mnemonic := []byte(strings.Join(words, " "))
salt := []byte(SeedSaltPrefix + passphrase)
return pbkdf2.Key(
mnemonic,
salt,
SeedKDFIterationCount,
SeedLength,
sha512.New,
)
}