-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
64 lines (53 loc) · 1.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
// Package main - example usage of chacha20poly1305 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
keyLength := 32 // length of the derived key in bytes
// derive a 256-bit key from the user's secret pass using argon2id
key := argon2.IDKey([]byte(secretPass), salt, uint32(timeCost), uint32(memoryCost), uint8(cpuCost), uint32(keyLength))
// encrypt the data
ciphertext, nonce, err := crypt.EncryptChacha20poly1305(key, text)
if err != nil {
fmt.Println(err)
return
}
fmt.Println("ciphertext:", ciphertext)
// decrypt the data
plaintext, err := crypt.DecryptChacha20poly1305(key, nonce, ciphertext)
if err != nil {
fmt.Println(err)
return
}
fmt.Println("plaintext:", plaintext)
// encrypt the data using EncryptChacha20poly1305WithNonceAppended function
ciphertext, err = crypt.EncryptChacha20poly1305WithNonceAppended(key, text)
if err != nil {
fmt.Println(err)
return
}
fmt.Println("ciphertext:", ciphertext)
// decrypt the data using DecryptChacha20poly1305WithNonceAppended function
plaintext, err = crypt.DecryptChacha20poly1305WithNonceAppended(key, ciphertext)
if err != nil {
fmt.Println(err)
return
}
fmt.Println("plaintext:", plaintext)
}