-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.go
77 lines (61 loc) · 1.55 KB
/
utils.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
package macaroons
import (
"crypto/aes"
"crypto/cipher"
"crypto/hmac"
"crypto/rand"
"crypto/sha256"
"encoding/base64"
)
const (
keyLength = 32
)
func deriveMacaroonKey(identifier string) []byte {
// BUG(tdaniels): Hard coded master key used for mKey derivation.
mKey := []byte("secret")
derivedKey := signature(mKey, []byte(identifier))
return derivedKey
}
func signature(key, data []byte) []byte {
mac := hmac.New(sha256.New, key)
mac.Write([]byte(data))
return mac.Sum(nil)
}
func newCaveatKey() (*[keyLength]byte, error) {
var key [keyLength]byte
_, err := rand.Reader.Read(key[:])
if err != nil {
return nil, err
}
return &key, nil
}
func encrypt(key, data []byte) (string, error) {
block, err := aes.NewCipher(key)
if err != nil {
return "", err
}
// BUG(tdaniels): Static IV used for encryption
iv := []byte("12345678901234567890123456789012")
iv = iv[:aes.BlockSize]
encrypter := cipher.NewCFBEncrypter(block, iv)
ciphertext := make([]byte, len(data))
encrypter.XORKeyStream(ciphertext, data)
encrypted := base64.StdEncoding.EncodeToString(ciphertext)
return encrypted, nil
}
func decrypt(key []byte, ciphertext string) ([]byte, error) {
block, err := aes.NewCipher(key)
if err != nil {
return nil, err
}
iv := []byte("12345678901234567890123456789012")
iv = iv[:aes.BlockSize]
encrypter := cipher.NewCFBDecrypter(block, iv)
data, err := base64.StdEncoding.DecodeString(ciphertext)
if err != nil {
return nil, err
}
plaintext := make([]byte, len(data))
encrypter.XORKeyStream(plaintext, data)
return plaintext, nil
}