/
SCEE.go
118 lines (94 loc) · 2.92 KB
/
SCEE.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
package scee
import (
"crypto/aes"
"crypto/cipher"
"crypto/rand"
"crypto/sha256"
"encoding/base64"
"golang.org/x/crypto/pbkdf2"
)
const AlgorithmNonceSize int = 12
const AlgorithmKeySize int = 16
const PBKDF2SaltSize int = 16
const PBKDF2Iterations int = 32767
func EncryptString(plaintext, password string) (string, error) {
// Generate a 128-bit salt using a CSPRNG.
salt := make([]byte, PBKDF2SaltSize)
_, err := rand.Read(salt)
if err != nil {
return "", err
}
// Derive a key using PBKDF2.
key := pbkdf2.Key([]byte(password), salt, PBKDF2Iterations, AlgorithmKeySize, sha256.New)
// Encrypt and prepend salt.
ciphertextAndNonce, err := Encrypt([]byte(plaintext), key)
if err != nil {
return "", err
}
ciphertextAndNonceAndSalt := make([]byte, 0)
ciphertextAndNonceAndSalt = append(ciphertextAndNonceAndSalt, salt...)
ciphertextAndNonceAndSalt = append(ciphertextAndNonceAndSalt, ciphertextAndNonce...)
// Return as base64 string.
return base64.StdEncoding.EncodeToString(ciphertextAndNonceAndSalt), nil
}
func DecryptString(base64CiphertextAndNonceAndSalt, password string) (string, error) {
// Decode the base64.
ciphertextAndNonceAndSalt, err := base64.StdEncoding.DecodeString(base64CiphertextAndNonceAndSalt)
if err != nil {
return "", err
}
// Create slices pointing to the salt and ciphertextAndNonce.
salt := ciphertextAndNonceAndSalt[:PBKDF2SaltSize]
ciphertextAndNonce := ciphertextAndNonceAndSalt[PBKDF2SaltSize:]
// Derive the key using PBKDF2.
key := pbkdf2.Key([]byte(password), salt, PBKDF2Iterations, AlgorithmKeySize, sha256.New)
// Decrypt and return result.
plaintext, err := Decrypt(ciphertextAndNonce, key)
if err != nil {
return "", err
}
return string(plaintext), nil
}
func Encrypt(plaintext, key []byte) ([]byte, error) {
// Generate a 96-bit nonce using a CSPRNG.
nonce := make([]byte, AlgorithmNonceSize)
_, err := rand.Read(nonce)
if err != nil {
return nil, err
}
// Create the cipher and block.
block, err := aes.NewCipher(key)
if err != nil {
return nil, err
}
cipher, err := cipher.NewGCM(block)
if err != nil {
return nil, err
}
// Encrypt and prepend nonce.
ciphertext := cipher.Seal(nil, nonce, plaintext, nil)
ciphertextAndNonce := make([]byte, 0)
ciphertextAndNonce = append(ciphertextAndNonce, nonce...)
ciphertextAndNonce = append(ciphertextAndNonce, ciphertext...)
return ciphertextAndNonce, nil
}
func Decrypt(ciphertextAndNonce, key []byte) ([]byte, error) {
// Create slices pointing to the ciphertext and nonce.
nonce := ciphertextAndNonce[:AlgorithmNonceSize]
ciphertext := ciphertextAndNonce[AlgorithmNonceSize:]
// Create the cipher and block.
block, err := aes.NewCipher(key)
if err != nil {
return nil, err
}
cipher, err := cipher.NewGCM(block)
if err != nil {
return nil, err
}
// Decrypt and return result.
plaintext, err := cipher.Open(nil, nonce, ciphertext, nil)
if err != nil {
return nil, err
}
return plaintext, nil
}