mirror of
https://github.com/hay-kot/homebox.git
synced 2024-12-23 23:36:30 +00:00
31 lines
503 B
Go
31 lines
503 B
Go
|
package hasher
|
||
|
|
||
|
import (
|
||
|
"crypto/rand"
|
||
|
"crypto/sha256"
|
||
|
"encoding/base32"
|
||
|
)
|
||
|
|
||
|
type Token struct {
|
||
|
Raw string
|
||
|
Hash []byte
|
||
|
}
|
||
|
|
||
|
func GenerateToken() Token {
|
||
|
randomBytes := make([]byte, 16)
|
||
|
rand.Read(randomBytes)
|
||
|
|
||
|
plainText := base32.StdEncoding.WithPadding(base32.NoPadding).EncodeToString(randomBytes)
|
||
|
hash := HashToken(plainText)
|
||
|
|
||
|
return Token{
|
||
|
Raw: plainText,
|
||
|
Hash: hash,
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func HashToken(plainTextToken string) []byte {
|
||
|
hash := sha256.Sum256([]byte(plainTextToken))
|
||
|
return hash[:]
|
||
|
}
|