-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathkey.go
91 lines (81 loc) · 1.84 KB
/
key.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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
package fernet
import (
"crypto/rand"
"encoding/base64"
"encoding/hex"
"errors"
"io"
)
var (
errKeyLen = errors.New("fernet: key decodes to wrong size")
errNoKeys = errors.New("fernet: no keys provided")
)
// Key represents a key.
type Key [32]byte
func (k *Key) cryptBytes() []byte {
return k[len(k)/2:]
}
func (k *Key) signBytes() []byte {
return k[:len(k)/2]
}
// Generate initializes k with pseudorandom data from package crypto/rand.
func (k *Key) Generate() error {
_, err := io.ReadFull(rand.Reader, k[:])
return err
}
// Encode returns the URL-safe base64 encoding of k.
func (k *Key) Encode() string {
return encoding.EncodeToString(k[:])
}
// DecodeKey decodes a key from s and returns it. The key can be in
// hexadecimal, standard base64, or URL-safe base64.
func DecodeKey(s string) (*Key, error) {
var b []byte
var err error
if s == "" {
return nil, errors.New("empty key")
}
if len(s) == hex.EncodedLen(len(Key{})) {
b, err = hex.DecodeString(s)
} else {
b, err = base64.StdEncoding.DecodeString(s)
if err != nil {
b, err = base64.URLEncoding.DecodeString(s)
}
}
if err != nil {
return nil, err
}
if len(b) != len(Key{}) {
return nil, errKeyLen
}
k := new(Key)
copy(k[:], b)
return k, nil
}
// DecodeKeys decodes each element of a using DecodeKey and returns the
// resulting keys. Requires at least one key.
func DecodeKeys(a ...string) ([]*Key, error) {
if len(a) == 0 {
return nil, errNoKeys
}
var err error
ks := make([]*Key, len(a))
for i, s := range a {
ks[i], err = DecodeKey(s)
if err != nil {
return nil, err
}
}
return ks, nil
}
// MustDecodeKeys is like DecodeKeys, but panics if an error occurs.
// It simplifies safe initialization of global variables holding
// keys.
func MustDecodeKeys(a ...string) []*Key {
k, err := DecodeKeys(a...)
if err != nil {
panic(err)
}
return k
}