ChaCha20-Poly1305
ChaCha20-Poly1305 is a modern high-performance authenticated encryption algorithm (AEAD) that combines ChaCha20
stream cipher and Poly1305
message authentication code. It uses fixed-length 32
byte keys and 12
byte nonces to encrypt and authenticate data. dongle
supports standard and streaming ChaCha20-Poly1305
encryption, providing multiple input formats, output formats and streaming processing capabilities.
ChaCha20-Poly1305 is a symmetric encryption algorithm that uses the same key for encryption and decryption. As an AEAD
algorithm, it not only provides confidentiality protection, but also provides integrity and authenticity verification, capable of detecting data tampering.
Important Notes
- Key Length: ChaCha20-Poly1305 key must be
32
bytes - Nonce Length: ChaCha20-Poly1305 nonce must be
12
bytes - Additional Data: Optional additional authenticated data (AAD), used for verification but not encryption
- Authentication Tag: Encrypted data contains
16
bytes of authentication tag - Nonce Uniqueness: Nonce must be unique under each key and cannot be reused
- Security: ChaCha20-Poly1305 provides high security and is widely adopted by standards such as
TLS1.3
Import related modules:
import (
"github.com/dromara/dongle"
"github.com/dromara/dongle/crypto/cipher"
)
Create Cipher
c := cipher.NewChaCha20Poly1305Cipher()
// Set key (must be 32 bytes)
c.SetKey([]byte("dongle1234567890abcdef123456789x"))
// Set nonce (must be 12 bytes)
c.SetNonce([]byte("123456789012"))
// Set additional authenticated data (optional)
c.SetAAD([]byte("additional authenticated data"))
Encrypt Data
Input Data
// Input string
encrypter := dongle.Encrypt.FromString("hello world").ByChaCha20Poly1305(c)
// Input byte slice
encrypter := dongle.Encrypt.FromBytes([]byte("hello world")).ByChaCha20Poly1305(c)
// Input file stream
file, _ := os.Open("test.txt")
encrypter := dongle.Encrypt.FromFile(file).ByChaCha20Poly1305(c)
// Check encryption error
if encrypter.Error != nil {
fmt.Printf("Encryption error: %v\n", encrypter.Error)
return
}
Output Data
// Output hex-encoded string
hexString := encrypter.ToHexString() // 4a1c8f2d3e5a6b7c...
// Output hex-encoded byte slice
hexBytes := encrypter.ToHexBytes() // []byte("4a1c8f2d3e5a6b7c...")
// Output base64-encoded string
base64String := encrypter.ToBase64String() // ShyPLT5aa3w=...
// Output base64-encoded byte slice
base64Bytes := encrypter.ToBase64Bytes() // []byte("ShyPLT5aa3w=...")
// Output raw string
rawString := encrypter.ToRawString()
// Output raw byte slice
rawBytes := encrypter.ToRawBytes()
Decrypt Data
Input Data
// Input hex-encoded string
decrypter := dongle.Decrypt.FromHexString(hexString).ByChaCha20Poly1305(c)
// Input hex-encoded byte slice
decrypter := dongle.Decrypt.FromHexBytes(hexBytes).ByChaCha20Poly1305(c)
// Input hex-encoded file stream
file, _ := os.Open("encrypted.hex")
decrypter := dongle.Decrypt.FromHexFile(file).ByChaCha20Poly1305(c)
// Input base64-encoded string
decrypter := dongle.Decrypt.FromBase64String(base64String).ByChaCha20Poly1305(c)
// Input base64-encoded byte slice
decrypter := dongle.Decrypt.FromBase64Bytes(base64Bytes).ByChaCha20Poly1305(c)
// Input base64-encoded file stream
file, _ := os.Open("encrypted.base64")
decrypter := dongle.Decrypt.FromBase64File(file).ByChaCha20Poly1305(c)
// Input raw string
decrypter := dongle.Decrypt.FromRawString(rawString).ByChaCha20Poly1305(c)
// Input raw byte slice
decrypter := dongle.Decrypt.FromRawBytes(rawBytes).ByChaCha20Poly1305(c)
// Input raw file stream
file, _ := os.Open("encrypted.bin")
decrypter := dongle.Decrypt.FromRawFile(file).ByChaCha20Poly1305(c)
// Check decryption error
if decrypter.Error != nil {
fmt.Printf("Decryption error: %v\n", decrypter.Error)
return
}
Output Data
// Output decrypted string
decrypter.ToString() // hello world
// Output decrypted byte slice
decrypter.ToBytes() // []byte("hello world")