-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathutil.go
58 lines (48 loc) · 1.21 KB
/
util.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 atoll
import (
"bytes"
"crypto/rand"
"math/big"
"reflect"
"regexp"
"runtime"
"strings"
"sync"
)
var commonPatterns = regexp.MustCompile(`(?i)abc|123|qwerty|asdf|zxcv|1qaz|
zaq1|qazwsx|pass|login|admin|master|!@#$|!234|!Q@W`)
var pool = &sync.Pool{
New: func() interface{} {
return &bytes.Buffer{}
},
}
// getBuf returns a buffer from the pool.
func getBuf() *bytes.Buffer {
return pool.Get().(*bytes.Buffer)
}
// putBuf resets buf and puts it back to the pool.
func putBuf(buf *bytes.Buffer) {
buf.Reset()
pool.Put(buf)
}
// getFuncName returns the name of the function passed.
func getFuncName(f list) string {
// Example: github.com/GGP1/atoll.NoList
fn := runtime.FuncForPC(reflect.ValueOf(f).Pointer()).Name()
lastDot := strings.LastIndexByte(fn, '.')
return fn[lastDot+1:]
}
// randInt returns a cryptographically secure random integer in [0, max).
func randInt(max int) int64 {
// The error is skipped as max is always > 0.
randN, _ := rand.Int(rand.Reader, big.NewInt(int64(max)))
return randN.Int64()
}
// shuffle changes randomly the order of the password elements.
func shuffle(key []byte) []byte {
for i := range key {
j := randInt(i + 1)
key[i], key[j] = key[j], key[i]
}
return key
}