32 lines
842 B
Go
32 lines
842 B
Go
// Package agenttoken creates and hashes the per-agent tokens agents present
|
|
// to the master.
|
|
package agenttoken
|
|
|
|
import (
|
|
"crypto/rand"
|
|
"crypto/sha256"
|
|
"encoding/base64"
|
|
"encoding/hex"
|
|
"fmt"
|
|
)
|
|
|
|
const prefix = "pua_"
|
|
|
|
// Generate returns a new random agent token. Show it to the operator once;
|
|
// store only Hash(token).
|
|
func Generate() (string, error) {
|
|
var b [32]byte
|
|
if _, err := rand.Read(b[:]); err != nil {
|
|
return "", fmt.Errorf("generate agent token: %w", err)
|
|
}
|
|
return prefix + base64.RawURLEncoding.EncodeToString(b[:]), nil
|
|
}
|
|
|
|
// Hash returns the value stored for token. A plain SHA-256 is sufficient
|
|
// because tokens carry 256 bits of randomness; salting protects low-entropy
|
|
// passwords, which these are not.
|
|
func Hash(token string) string {
|
|
sum := sha256.Sum256([]byte(token))
|
|
return hex.EncodeToString(sum[:])
|
|
}
|