github.com/devops-filetransfer/sshego@v7.0.4+incompatible/cryrand.go (about) 1 package sshego 2 3 import ( 4 cryrand "crypto/rand" 5 "encoding/binary" 6 7 "github.com/glycerine/sshego/dict" 8 ) 9 10 // Use crypto/rand to get an random int64 11 func CryptoRandInt64() int64 { 12 c := 8 13 b := make([]byte, c) 14 _, err := cryrand.Read(b) 15 if err != nil { 16 panic(err) 17 } 18 r := int64(binary.LittleEndian.Uint64(b)) 19 return r 20 } 21 22 func CryptoRandBytes(n int) []byte { 23 b := make([]byte, n) 24 _, err := cryrand.Read(b) 25 if err != nil { 26 panic(err) 27 } 28 return b 29 } 30 31 func getNewPasswordStarter() string { 32 return dict.GetNewPasswordStarter() + " " 33 } 34 35 func CryptoRandNonNegInt(n int64) int64 { 36 x := CryptoRandInt64() 37 if x < 0 { 38 x = -x 39 } 40 return x % n 41 } 42 43 var ch = []byte("0123456789abcdefghijklmnopqrstuvwxyz") 44 45 func RandomString(n int) string { 46 s := make([]byte, n) 47 m := int64(len(ch)) 48 for i := 0; i < n; i++ { 49 r := CryptoRandInt64() 50 if r < 0 { 51 r = -r 52 } 53 k := r % m 54 a := ch[k] 55 s[i] = a 56 } 57 return string(s) 58 }