-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
129 lines (110 loc) · 3.69 KB
/
main.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
119
120
121
122
123
124
125
126
127
128
129
// Package main - example usage of AES encryption - decryption
package main
import (
"crypto/rand"
"fmt"
"golang.org/x/crypto/argon2"
"github.com/pilinux/crypt"
)
func main() {
text := "Hello world"
secretPass := ",D'(bHOpO#beU(Fn@~_6Enn3a2n=aEQWg''vz"
// generate a random 256-bit salt
salt := make([]byte, 32)
if _, err := rand.Read(salt); err != nil {
fmt.Println("error generating salt:", err)
return
}
// parameters for argon2 key derivation
timeCost := 2 // number of iterations
memoryCost := 64 * 1024 // memory usage in KiB
cpuCost := 2 // number of threads used
// derive key from the user's secret pass using argon2id
key128 := argon2.IDKey([]byte(secretPass), salt, uint32(timeCost), uint32(memoryCost), uint8(cpuCost), 128/8)
key192 := argon2.IDKey([]byte(secretPass), salt, uint32(timeCost), uint32(memoryCost), uint8(cpuCost), 192/8)
key256 := argon2.IDKey([]byte(secretPass), salt, uint32(timeCost), uint32(memoryCost), uint8(cpuCost), 256/8)
// encrypt the data with AES-128 in GCM
ciphertext128, nonce128, err := crypt.EncryptAesGcm(key128, text)
if err != nil {
fmt.Println(err)
return
}
fmt.Println("ciphertext (AES-128):", ciphertext128)
// decrypt the data with AES-128 in GCM
plaintext, err := crypt.DecryptAesGcm(key128, nonce128, ciphertext128)
if err != nil {
fmt.Println(err)
return
}
fmt.Println("plaintext:", plaintext)
// encrypt the data with AES-192 in GCM
ciphertext192, nonce192, err := crypt.EncryptAesGcm(key192, text)
if err != nil {
fmt.Println(err)
return
}
fmt.Println("ciphertext (AES-192):", ciphertext192)
// decrypt the data with AES-192 in GCM
plaintext, err = crypt.DecryptAesGcm(key192, nonce192, ciphertext192)
if err != nil {
fmt.Println(err)
return
}
fmt.Println("plaintext:", plaintext)
// encrypt the data with AES-256 in GCM
ciphertext256, nonce256, err := crypt.EncryptAesGcm(key256, text)
if err != nil {
fmt.Println(err)
return
}
fmt.Println("ciphertext (AES-256):", ciphertext256)
// decrypt the data with AES-256 in GCM
plaintext, err = crypt.DecryptAesGcm(key256, nonce256, ciphertext256)
if err != nil {
fmt.Println(err)
return
}
fmt.Println("plaintext:", plaintext)
// encrypt the data with AES-128 in GCM using EncryptAesGcmWithNonceAppended function
ciphertext128, err = crypt.EncryptAesGcmWithNonceAppended(key128, text)
if err != nil {
fmt.Println(err)
return
}
fmt.Println("ciphertext (AES-128):", ciphertext128)
// decrypt the data with AES-128 in GCM using DecryptAesGcmWithNonceAppended function
plaintext, err = crypt.DecryptAesGcmWithNonceAppended(key128, ciphertext128)
if err != nil {
fmt.Println(err)
return
}
fmt.Println("plaintext:", plaintext)
// encrypt the data with AES-192 in GCM using EncryptAesGcmWithNonceAppended function
ciphertext192, err = crypt.EncryptAesGcmWithNonceAppended(key192, text)
if err != nil {
fmt.Println(err)
return
}
fmt.Println("ciphertext (AES-192):", ciphertext192)
// decrypt the data with AES-192 in GCM using DecryptAesGcmWithNonceAppended function
plaintext, err = crypt.DecryptAesGcmWithNonceAppended(key192, ciphertext192)
if err != nil {
fmt.Println(err)
return
}
fmt.Println("plaintext:", plaintext)
// encrypt the data with AES-256 in GCM using EncryptAesGcmWithNonceAppended function
ciphertext256, err = crypt.EncryptAesGcmWithNonceAppended(key256, text)
if err != nil {
fmt.Println(err)
return
}
fmt.Println("ciphertext (AES-256):", ciphertext256)
// decrypt the data with AES-256 in GCM using DecryptAesGcmWithNonceAppended function
plaintext, err = crypt.DecryptAesGcmWithNonceAppended(key256, ciphertext256)
if err != nil {
fmt.Println(err)
return
}
fmt.Println("plaintext:", plaintext)
}