|
| 1 | +// Copyright (c) Jeevanandam M. (https://github.com/jeevatkm) |
| 2 | +// go-aah/essentials source code and usage is governed by a MIT style |
| 3 | +// license that can be found in the LICENSE file. |
| 4 | + |
| 5 | +package ess |
| 6 | + |
| 7 | +import ( |
| 8 | + "crypto/rand" |
| 9 | + "encoding/hex" |
| 10 | + "io" |
| 11 | + mrand "math/rand" |
| 12 | + "sync" |
| 13 | + "time" |
| 14 | +) |
| 15 | + |
| 16 | +const ( |
| 17 | + letterBytes = "abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ" |
| 18 | + letterIdxBits = 6 // 6 bits to represent a letter index |
| 19 | + letterIdxMask = 1<<letterIdxBits - 1 // All 1-bits, as many as letterIdxBits |
| 20 | + letterIdxMax = 63 / letterIdxBits // # of letter indices fitting in 63 bits |
| 21 | +) |
| 22 | + |
| 23 | +var ( |
| 24 | + mRandSrc mrand.Source |
| 25 | + mr *sync.Mutex |
| 26 | +) |
| 27 | + |
| 28 | +//‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾ |
| 29 | +// Random String methods |
| 30 | +//___________________________________ |
| 31 | + |
| 32 | +// RandomString method generates the random string for given length using |
| 33 | +// `crypto/rand`. |
| 34 | +func RandomString(length int) string { |
| 35 | + return hex.EncodeToString(GenerateRandomKey(length / 2)) |
| 36 | +} |
| 37 | + |
| 38 | +// RandomStringbm method generates the random string for given length using |
| 39 | +// `math/rand.Source` and byte mask. |
| 40 | +func RandomStringbm(length int) string { |
| 41 | + return string(GenerateRandomKeybm(length)) |
| 42 | +} |
| 43 | + |
| 44 | +//‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾ |
| 45 | +// Random key methods |
| 46 | +//___________________________________ |
| 47 | + |
| 48 | +// GenerateRandomKey method generates the random bytes for given length using |
| 49 | +// `crypto/rand`. |
| 50 | +func GenerateRandomKey(length int) []byte { |
| 51 | + k := make([]byte, length) |
| 52 | + if _, err := io.ReadFull(rand.Reader, k); err != nil { |
| 53 | + // fallback to math based random key generater |
| 54 | + return GenerateRandomKeybm(length) |
| 55 | + } |
| 56 | + return k |
| 57 | +} |
| 58 | + |
| 59 | +// GenerateRandomKeybm method generates the random bytes for given length using |
| 60 | +// `math/rand.Source` and byte mask. |
| 61 | +// StackOverflow Ref - http://stackoverflow.com/a/31832326 |
| 62 | +func GenerateRandomKeybm(length int) []byte { |
| 63 | + b := make([]byte, length) |
| 64 | + // A randSrc() generates 63 random bits, enough for letterIdxMax characters! |
| 65 | + for i, cache, remain := length-1, randSrc(), letterIdxMax; i >= 0; { |
| 66 | + if remain == 0 { |
| 67 | + cache, remain = randSrc(), letterIdxMax |
| 68 | + } |
| 69 | + if idx := int(cache & letterIdxMask); idx < len(letterBytes) { |
| 70 | + b[i] = letterBytes[idx] |
| 71 | + i-- |
| 72 | + } |
| 73 | + cache >>= letterIdxBits |
| 74 | + remain-- |
| 75 | + } |
| 76 | + |
| 77 | + return b |
| 78 | +} |
| 79 | + |
| 80 | +//‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾ |
| 81 | +// Unexported methods |
| 82 | +//___________________________________ |
| 83 | + |
| 84 | +func randSrc() int64 { |
| 85 | + mr.Lock() |
| 86 | + defer mr.Unlock() |
| 87 | + return mRandSrc.Int63() |
| 88 | +} |
| 89 | + |
| 90 | +//‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾ |
| 91 | +// init |
| 92 | +//___________________________________ |
| 93 | + |
| 94 | +func init() { |
| 95 | + mRandSrc = mrand.NewSource(time.Now().UnixNano()) |
| 96 | + mr = &sync.Mutex{} |
| 97 | +} |
0 commit comments