-
Notifications
You must be signed in to change notification settings - Fork 3
/
crypto.go
151 lines (126 loc) · 4.08 KB
/
crypto.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
package superdog
import (
"bytes"
"crypto/sha256"
"encoding/base64"
"encoding/binary"
"errors"
)
var DefaultKeyProvider KeyProvider = new(DevKeyProvider)
var DefaultSaltProvider SaltProvider = new(DevSaltProvider)
// Encrypt will encrypt the provided byte slice with the latesg key. It returns a new slice as it prepends the key version, and IV.
func Encrypt(prefix string, dst, src []byte) ([]byte, error) {
v, err := DefaultKeyProvider.CurrentKeyVersion(prefix)
if err != nil {
return nil, err
}
return EncryptWithVersion(prefix, v, dst, src)
}
// EncryptWithVersion will encrypt the provided byte slice with the supplied key version. It returns a new slice as it prepends the key version, and IV.
func EncryptWithVersion(keyPrefix string, keyVersion uint64, dst []byte, src []byte) ([]byte, error) {
if len(src) == 0 {
return dst[:0], nil
}
k, err := DefaultKeyProvider.GetKey(keyPrefix, keyVersion)
if err != nil {
return nil, err
}
return k.Encrypt(dst, src)
}
// Decrypt will decrypt the provided byte slice using the provided key at the version it was encrypted with. It returns a new slice as it trims the prefixed key version and IV. It modifies the same underlying array.
func Decrypt(keyPrefix string, dst, src []byte) ([]byte, error) {
if len(src) == 0 {
return []byte{}, nil
}
if len(src) <= 8 {
return nil, errors.New("Insufficient length")
}
buf := bytes.NewBuffer(src)
version, err := binary.ReadUvarint(buf)
if err != nil {
return src, err
}
k, err := DefaultKeyProvider.GetKey(keyPrefix, version)
if err != nil {
return nil, err
}
return k.Decrypt(src, src[8:])
}
// Reencrypt takes encrypted ciphertext, decrypts it with the version of the key used to decrypt it, and re-encrypts the plaintext with the current version of the key.
func Reencrypt(keyPrefix string, dst, src []byte) ([]byte, error) {
dst, err := Decrypt(keyPrefix, dst, src)
if err != nil {
return src, err
}
v, err := DefaultKeyProvider.CurrentKeyVersion(keyPrefix)
if err != nil {
return nil, err
}
return EncryptWithVersion(keyPrefix, v, dst, dst)
}
// CurrentHashes returns a list of all possible hashes for the given prefix and value, used as search criteria during rotation
func CurrentHashes(prefix string, value []byte) ([][]byte, error) {
hashes := make([][]byte, 0)
salts, err := DefaultSaltProvider.CurrentSalts(prefix)
if err != nil {
return nil, err
}
for _, v := range salts {
h, err := HashWithVersion(prefix, v, value)
if err != nil {
return nil, err
}
hashes = append(hashes, h)
}
return hashes, nil
}
// CurrentHashesString returns a list of all possible hashes for the given prefix and value, used as search criteria during rotation
func CurrentHashesString(prefix string, value string) ([]string, error) {
hashes := make([]string, 0)
if len(value) == 0 {
return hashes, nil
}
salts, err := DefaultSaltProvider.CurrentSalts(prefix)
if err != nil {
return nil, err
}
for _, v := range salts {
h, err := HashWithVersion(prefix, v, []byte(value))
if err != nil {
return nil, err
}
hashes = append(hashes, string(h))
}
return hashes, nil
}
// Hash returns a hash to be used for the given value using the current version
func Hash(prefix string, value []byte) ([]byte, error) {
v, err := DefaultSaltProvider.CurrentSaltVersion(prefix)
if err != nil {
return nil, err
}
return HashWithVersion(prefix, v, value)
}
// HashWithVersion returns a hash to be used for the given value using the supplied version
func HashWithVersion(prefix string, version uint64, value []byte) ([]byte, error) {
if len(value) == 0 {
return []byte{}, nil
}
s, err := DefaultSaltProvider.GetSalt(prefix, version)
if err != nil {
return nil, err
}
hash := sha256.Sum256(append(s, value...))
i := base64.StdEncoding.EncodedLen(32)
out := make([]byte, i)
base64.StdEncoding.Encode(out, hash[:])
return out, nil
}
// HashString returns a hash to be used for the given value using the current version
func HashString(prefix string, value string) (string, error) {
b, err := Hash(prefix, []byte(value))
if err != nil {
return "", err
}
return string(b), nil
}