35 lines
562 B
Go
35 lines
562 B
Go
|
package token
|
||
|
|
||
|
import (
|
||
|
"crypto/rand"
|
||
|
"crypto/sha256"
|
||
|
"encoding/base32"
|
||
|
"encoding/hex"
|
||
|
"strings"
|
||
|
)
|
||
|
|
||
|
func randomBytes(n int) ([]byte, error) {
|
||
|
b := make([]byte, n)
|
||
|
|
||
|
_, err := rand.Read(b)
|
||
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
|
||
|
return b, nil
|
||
|
}
|
||
|
|
||
|
func New() (string, error) {
|
||
|
b, err := randomBytes(16)
|
||
|
if err != nil {
|
||
|
return "", err
|
||
|
}
|
||
|
|
||
|
return strings.ToLower(base32.StdEncoding.WithPadding(base32.NoPadding).EncodeToString(b)), nil
|
||
|
}
|
||
|
|
||
|
func Hash(plaintext string) string {
|
||
|
hash := sha256.Sum256([]byte(plaintext))
|
||
|
return hex.EncodeToString(hash[:])
|
||
|
}
|