-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
secret.go
37 lines (30 loc) · 1.08 KB
/
secret.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
// 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[:]
}