-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscrypt.go
45 lines (39 loc) · 994 Bytes
/
scrypt.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
36
37
38
39
40
41
42
43
44
45
/**
* This file is a part of the poni project and is licensed under the MIT license.
* See LICENSE.md for details.
*
* scrypt.go
* Contains the scrypt key derivation function.
*
* @created 2023-08-23
*/
package main
import (
"crypto/rand"
"golang.org/x/crypto/scrypt"
)
// deriveKey
// Derives a key from a password using scrypt.
// Returns the key, the salt, and an error.
func deriveKey(password string) (key []byte, salt []byte, err error) {
salt = make([]byte, 32)
_, err = rand.Read(salt)
if err != nil {
return nil, nil, err
}
key, err = scrypt.Key([]byte(password), salt, 131072, 8, 1, 32)
if err != nil {
return nil, nil, err
}
return key, salt, nil
}
// deriveKeyWithSalt
// Derives a key from a password and a salt using scrypt.
// Returns the key and an error.
func deriveKeyWithSalt(password string, salt []byte) ([]byte, error) {
key, err := scrypt.Key([]byte(password), salt, 131072, 8, 1, 32)
if err != nil {
return nil, err
}
return key, nil
}