-
Notifications
You must be signed in to change notification settings - Fork 19
/
cryrand.go
58 lines (50 loc) · 921 Bytes
/
cryrand.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
package sshego
import (
cryrand "crypto/rand"
"encoding/binary"
"github.com/glycerine/sshego/dict"
)
// Use crypto/rand to get an random int64
func CryptoRandInt64() int64 {
c := 8
b := make([]byte, c)
_, err := cryrand.Read(b)
if err != nil {
panic(err)
}
r := int64(binary.LittleEndian.Uint64(b))
return r
}
func CryptoRandBytes(n int) []byte {
b := make([]byte, n)
_, err := cryrand.Read(b)
if err != nil {
panic(err)
}
return b
}
func getNewPasswordStarter() string {
return dict.GetNewPasswordStarter() + " "
}
func CryptoRandNonNegInt(n int64) int64 {
x := CryptoRandInt64()
if x < 0 {
x = -x
}
return x % n
}
var ch = []byte("0123456789abcdefghijklmnopqrstuvwxyz")
func RandomString(n int) string {
s := make([]byte, n)
m := int64(len(ch))
for i := 0; i < n; i++ {
r := CryptoRandInt64()
if r < 0 {
r = -r
}
k := r % m
a := ch[k]
s[i] = a
}
return string(s)
}