You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
50 lines
844 B
50 lines
844 B
2 years ago
|
package toolm
|
||
|
|
||
|
import (
|
||
|
"crypto/hmac"
|
||
|
"crypto/sha256"
|
||
|
"fmt"
|
||
|
"math/rand"
|
||
|
"time"
|
||
|
)
|
||
|
|
||
|
const (
|
||
|
CharsAll = 1 << iota
|
||
|
Digit
|
||
|
LowerCase
|
||
|
UpperCase
|
||
|
)
|
||
|
|
||
|
const (
|
||
|
digit = "0123456789"
|
||
|
lowercase = "abcdefghijklmnopqrstuvwxyz"
|
||
|
uppercase = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
|
||
|
//octal = "01234567"
|
||
|
//hex = "0123456789abcdefg"
|
||
|
)
|
||
|
|
||
|
func RandString(l, chars int) string {
|
||
|
charset := ""
|
||
|
if chars&CharsAll > 0 {
|
||
|
charset = digit + lowercase + uppercase
|
||
|
}
|
||
|
|
||
|
charsetLen := len(charset)
|
||
|
|
||
|
seededRand := rand.New(rand.NewSource(time.Now().UnixNano()))
|
||
|
|
||
|
b := make([]byte, l)
|
||
|
for i := range b {
|
||
|
b[i] = charset[seededRand.Intn(charsetLen)]
|
||
|
}
|
||
|
|
||
|
return string(b)
|
||
|
}
|
||
|
|
||
|
func Sha256HMacString(message, key string) string {
|
||
|
mac := hmac.New(sha256.New, []byte(key))
|
||
|
mac.Write([]byte(message))
|
||
|
expectedMAC := mac.Sum(nil)
|
||
|
return fmt.Sprintf("%X", expectedMAC)
|
||
|
}
|