-
Notifications
You must be signed in to change notification settings - Fork 59
/
passphrase.go
61 lines (49 loc) · 1.35 KB
/
passphrase.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
package vault
import (
crypto_rand "crypto/rand"
"encoding/base64"
"fmt"
"io"
"golang.org/x/crypto/nacl/secretbox"
)
type PassphraseBoxer struct {
passphrase string
}
func NewPassphraseBoxer(password string) *PassphraseBoxer {
return &PassphraseBoxer{
passphrase: password,
}
}
func (b *PassphraseBoxer) WrapType() string {
return "passphrase"
}
func (b *PassphraseBoxer) Seal(in []byte) (string, error) {
var nonce [nonceLength]byte
if _, err := io.ReadFull(crypto_rand.Reader, nonce[:]); err != nil {
return "", err
}
salt := make([]byte, saltLength)
if _, err := crypto_rand.Read(salt); err != nil {
return "", err
}
secretKey := deriveKey(b.passphrase, salt)
prefix := append(salt, nonce[:]...)
cipherText := secretbox.Seal(prefix, in, &nonce, &secretKey)
return base64.RawStdEncoding.EncodeToString(cipherText), nil
}
func (b *PassphraseBoxer) Open(in string) ([]byte, error) {
buf, err := base64.RawStdEncoding.DecodeString(in)
if err != nil {
return []byte{}, err
}
salt := make([]byte, saltLength)
copy(salt, buf[:saltLength])
var nonce [nonceLength]byte
copy(nonce[:], buf[saltLength:nonceLength+saltLength])
secretKey := deriveKey(b.passphrase, salt)
decrypted, ok := secretbox.Open(nil, buf[nonceLength+saltLength:], &nonce, &secretKey)
if !ok {
return []byte{}, fmt.Errorf("failed to decrypt")
}
return decrypted, nil
}