hydra/x/secret.go

38 lines
1.1 KiB
Go

// Copyright © 2022 Ory Corp
// SPDX-License-Identifier: Apache-2.0
package x
import (
"crypto/sha256"
"github.com/ory/x/randx"
)
var secretCharSet = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890_-.~")
func GenerateSecret(length int) ([]byte, error) {
secret, err := randx.RuneSequence(length, secretCharSet)
if err != nil {
return []byte{}, err
}
return []byte(string(secret)), nil
}
// HashStringSecret hashes the secret for consumption by the AEAD encryption algorithm which expects exactly 32 bytes.
//
// The system secret is being hashed to always match exactly the 32 bytes required by AEAD, even if the secret is long or
// shorter.
func HashStringSecret(secret string) []byte {
return HashByteSecret([]byte(secret))
}
// HashByteSecret hashes the secret for consumption by the AEAD encryption algorithm which expects exactly 32 bytes.
//
// The system secret is being hashed to always match exactly the 32 bytes required by AEAD, even if the secret is long or
// shorter.
func HashByteSecret(secret []byte) []byte {
r := sha256.Sum256(secret)
return r[:]
}