/
libCrypto2Go.go
70 lines (52 loc) · 1.28 KB
/
libCrypto2Go.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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
package libCrypto2Go
import (
"crypto/rand"
"golang.org/x/crypto/sha3"
"golang.org/x/crypto/scrypt"
)
const (
// Key length and salt length are 32 bytes (256 bits)
KEYLENGTH int = 32
// scrypt default parameters
SCRYPT_CONST_N int = 16384
SCRYPT_CONST_R int = 8
SCRYPT_CONST_P int = 1
)
func GenKey(password []byte, salt []byte ) ([]byte, error) {
encryptedKey, err := scrypt.Key(password, salt, SCRYPT_CONST_N, SCRYPT_CONST_R, SCRYPT_CONST_P, KEYLENGTH)
if err != nil {
return nil, err
}
return encryptedKey, nil
}
func GenRandomBytes(n int) ([]byte, error) {
b := make([]byte, n)
_, err := rand.Read(b)
if err != nil {
return nil, err
}
return b, nil
}
func Sum256(password string) (string) {
buf := []byte(password)
hash := sha3.Sum256(buf)
return string(hash[:]);
}
func Sum384(password string) (string) {
buf := []byte(password)
hash := sha3.Sum384(buf)
return string(hash[:])
}
func Sum512(password string) (string) {
buf := []byte(password)
hash := sha3.Sum512(buf)
return string(hash[:])
}
func ShakeSum256(password string) ([]byte) {
buf := []byte(password)
// A hash needs to be 64 bytes long to have 256-bit collision resistance.
h := make([]byte, 64)
// Compute a 64-byte hash of buf and put it in h.
sha3.ShakeSum256(h, buf)
return h;
}