-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathvault.go
106 lines (86 loc) · 2.38 KB
/
vault.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
package vault
import (
"crypto/hmac"
"crypto/sha256"
"errors"
"io/ioutil"
"strings"
)
var (
// ErrEmptyPassword is returned when password is empty
ErrEmptyPassword = errors.New("password is blank")
// ErrInvalidFormat is returned when secret content is not valid
ErrInvalidFormat = errors.New("invalid secret format")
// ErrInvalidPadding is returned when invalid key is used
ErrInvalidPadding = errors.New("invalid padding")
)
// Encrypt encrypts the input string with the vault password
func Encrypt(input string, password string) (string, error) {
if password == "" {
return "", ErrEmptyPassword
}
salt, err := generateRandomBytes(saltLength)
if err != nil {
return "", err
}
key := generateKey([]byte(password), salt)
// Encrypt the secret content
data, err := encrypt([]byte(input), salt, key)
if err != nil {
return "", err
}
// Hash the secret content
hash := hmac.New(sha256.New, key.hmacKey)
hash.Write(data)
hashSum := hash.Sum(nil)
// Encode the secret payload
return encodeSecret(&secret{data: data, salt: salt, hmac: hashSum}, key)
}
// EncryptFile encrypts the input string and saves it into the file
func EncryptFile(path string, input string, password string) error {
result, err := Encrypt(input, password)
if err != nil {
return err
}
return ioutil.WriteFile(path, []byte(result), 0666)
}
// Decrypt decrypts the input string with the vault password
func Decrypt(input string, password string) (string, error) {
if password == "" {
return "", ErrEmptyPassword
}
lines := strings.Split(input, "\n")
// Valid secret must include header and body
if len(lines) < 2 {
return "", ErrInvalidFormat
}
// Validate the vault file format
if strings.TrimSpace(lines[0]) != vaultHeader {
return "", ErrInvalidFormat
}
decoded, err := hexDecode(strings.Join(lines[1:], "\n"))
if err != nil {
return "", err
}
secret, err := decodeSecret(decoded)
if err != nil {
return "", err
}
key := generateKey([]byte(password), secret.salt)
if err := checkDigest(secret, key); err != nil {
return "", err
}
result, err := decrypt(secret, key)
if err != nil {
return "", err
}
return result, nil
}
// DecryptFile decrypts the content of the file with the vault password
func DecryptFile(path string, password string) (string, error) {
data, err := ioutil.ReadFile(path)
if err != nil {
return "", err
}
return Decrypt(string(data), password)
}