-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
functions: en/decrypt with XChaCha20-Poly1305
- Loading branch information
Showing
4 changed files
with
99 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
// Package main - example usage of XChaCha20-Poly1305 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.EncryptXChacha20poly1305(key, text) | ||
if err != nil { | ||
fmt.Println(err) | ||
return | ||
} | ||
fmt.Println("ciphertext:", ciphertext) | ||
|
||
// decrypt the data | ||
plaintext, err := crypt.DecryptXChacha20poly1305(key, nonce, ciphertext) | ||
if err != nil { | ||
fmt.Println(err) | ||
return | ||
} | ||
fmt.Println("plaintext:", plaintext) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters