/
seedGenerator.go
80 lines (60 loc) · 1.61 KB
/
seedGenerator.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
package tryte_cipher
import (
"crypto/rand"
"encoding/base64"
"errors"
"math/big"
)
const letters = "9ABCDEFGHIJKLMNOPQRSTUVWXYZ" //pool of letters to generate IOTA seed
func RandomPassphraseGenerator(n int) (stringBytes string, err error) {
if n < 8 {
//longer passphrase is more secure enforce this
err = errors.New("number of bytes cannot be less than 8")
return
}
randomPassphraseBytes, err := GenerateRandomBytes(n)
stringBytes = base64.URLEncoding.EncodeToString(randomPassphraseBytes)
return
}
// GenerateRandomBytes returns securely generated random bytes.
// It will return an error if the system's secure random
// number generator fails to function correctly, in which
// case the caller should not continue.
func GenerateRandomBytes(n int) (b []byte, err error) {
b = make([]byte, n)
_, err = rand.Read(b)
// Note that err == nil only if we read len(b) bytes.
if err != nil {
return nil, err
}
return
}
// GenerateRandomSeed returns a securely generated string.
// It will return an error if a secure random int generator
// fails to function correctly
func GenerateRandomSeed() (seed string, err error) {
ints, err := generateRandomInts(81)
if err != nil {
return
}
token := make([]byte, 81)
for i, x := range ints {
token[i] = intToCharByte(x)
}
seed = string(token)
return
}
func generateRandomInts(n int) (ints []int64, err error) {
ints = make([]int64, n)
for i := range ints {
randomInt, err := rand.Int(rand.Reader, big.NewInt(27))
if err != nil {
return nil, err
}
ints[i] = randomInt.Int64()
}
return
}
func intToCharByte(i int64) byte {
return letters[i]
}