forked from mirrors/homebox
Initial commit
This commit is contained in:
commit
29f583e936
135 changed files with 18463 additions and 0 deletions
30
backend/pkgs/hasher/token.go
Normal file
30
backend/pkgs/hasher/token.go
Normal file
|
@ -0,0 +1,30 @@
|
|||
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[:]
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue