-
Notifications
You must be signed in to change notification settings - Fork 3
/
Encrypt.go
53 lines (33 loc) · 1021 Bytes
/
Encrypt.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
package cvs
import "cookie.engineer/console"
import "crypto/aes"
import "crypto/cipher"
import "crypto/rand"
import "encoding/hex"
import "strconv"
func Encrypt(buffer []byte, password string) []byte {
var result []byte
console.Group("cvs/Encrypt")
salt := make([]byte, 16)
rand.Read(salt)
iv := make([]byte, 12)
rand.Read(iv)
key := DeriveKey(password, salt)
console.Log("Salt: " + hex.EncodeToString(salt))
console.Log("IV: " + hex.EncodeToString(iv))
console.Log("Key: " + hex.EncodeToString(key))
console.Log("Input Buffer: " + strconv.Itoa(len(buffer)) + " bytes")
block, err0 := aes.NewCipher(key)
if err0 == nil {
aes_gcm, err1 := cipher.NewGCM(block)
if err1 == nil {
tmp := aes_gcm.Seal(nil, iv, buffer, nil)
console.Log("Output Buffer: " + strconv.Itoa(len(tmp)) + " bytes")
result = append(result, salt...)
result = append(result, iv...)
result = append(result, tmp...)
}
}
console.GroupEnd("cvs/Encrypt")
return result
}