/
number.go
83 lines (66 loc) · 1.69 KB
/
number.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
71
72
73
74
75
76
77
78
79
80
81
82
83
package rand
import (
"crypto/rand"
"crypto/sha512"
"golang.org/x/crypto/pbkdf2"
)
// 定义不同int类型对应的key length
const (
// int8 类型
KeyLengthInt8 = 8
// int16 类型
KeyLengthInt16 = 16
// int32 类型
KeyLengthInt32 = 32
// int64 类型
KeyLengthInt64 = 64
)
const (
// 安全强度低
KeyStrengthEasy = iota
// 安全强度中
KeyStrengthMiddle
// 安全强度高
KeyStrengthHard
)
// 底层调用跟操作系统相关的函数(读取系统熵)来产生一些伪随机数,
// 对外建议管这个返回值叫做“熵”
func generateEntropy(bitSize int) ([]byte, error) {
err := validateEntropyBitSize(bitSize)
if err != nil {
return nil, err
}
entropy := make([]byte, bitSize/8)
_, err = rand.Read(entropy)
return entropy, err
}
// 生成一个指定长度的随机数种子
func generateSeedWithRandomPassword(randomPassword []byte, keyLen int) []byte {
salt := "jingbo is handsome."
seed := pbkdf2.Key(randomPassword, []byte(salt), 2048, keyLen, sha512.New)
return seed
}
func GenerateSeedWithStrengthAndKeyLen(strength int, keyLength int) ([]byte, error) {
var entropyBitLength = 0
//根据强度来判断随机数长度
switch strength {
case KeyStrengthEasy: // 弱
entropyBitLength = 128
case KeyStrengthMiddle: // 中
entropyBitLength = 192
case KeyStrengthHard: // 高
entropyBitLength = 256
default: // 不支持的语言类型
entropyBitLength = 0
}
// 判断强度是否合法
if entropyBitLength == 0 {
return nil, ErrStrengthNotSupported
}
// 产生随机熵
entropyByte, err := generateEntropy(entropyBitLength)
if err != nil {
return nil, err
}
return generateSeedWithRandomPassword(entropyByte, keyLength), nil
}