Chacha20 class abstract
ChaCha20 (RFC 7539) StreamingCipher.
We recommend you to use Chacha20.poly1305Aead(), which does message authentication with a standard AEAD construction for ChaCha20.
About the algorithm
- secretKeyLength is 32 bytes.
- nonceLength is 12 bytes.\
Example
import 'package:better_cryptography/better_cryptography.dart';
Future<void> main() async {
final message = <int>[1,2,3];
final algorithm = Chacha20(macAlgorithm: Hmac.sha256());
final secretKey = await algorithm.newSecretKey();
// Encrypt
final secretBox = await algorithm.encrypt(
message,
secretKey: secretKey,
);
print('Nonce: ${secretBox.nonce}')
print('Ciphertext: ${secretBox.cipherText}')
print('MAC: ${secretBox.mac.bytes}')
// Decrypt
final clearText = await algorithm.decrypt(
secretBox,
secretKey: secretKey,
);
print('Cleartext: $clearText');
}
In need of synchronous APIs?
If you need to perform operations synchronously, use DartChacha20 in package:better_cryptography/dart.dart.
- Inheritance
-
- Object
- Cipher
- StreamingCipher
- Chacha20
- Implementers
Constructors
- Chacha20.new({required MacAlgorithm macAlgorithm})
-
factory
- Chacha20.constructor()
-
Constructor for classes that extend this class.
const
- Chacha20.poly1305Aead()
-
AEAD_CHACHA20_POLY1305 (
https://tools.ietf.org/html/rfc7539
(RFC 7539)) Cipher.factory
Properties
- hashCode → int
-
The hash code for this object.
no setteroverride
- macAlgorithm → MacAlgorithm
-
Message authentication code (MacAlgorithm) used by the cipher.
no setterinherited
- nonceLength → int
-
Number of bytes in the nonce ("Initialization Vector", "IV", "salt").
no setteroverride
- runtimeType → Type
-
A representation of the runtime type of the object.
no setterinherited
- secretKeyLength → int
-
Number of bytes in the SecretKey.
no setteroverride
Methods
-
decrypt(
SecretBox secretBox, {required SecretKey secretKey, List< int> aad = const <int>[], int keyStreamIndex = 0}) → Future<List< int> > -
Decrypts a ciphertext.
inherited
-
encrypt(
List< int> clearText, {required SecretKey secretKey, List<int> ? nonce, List<int> aad = const <int>[], int keyStreamIndex = 0}) → Future<SecretBox> -
Encrypts a cleartext.
inherited
-
newNonce(
) → List< int> -
Generates a new nonce with the correct length (nonceLength).
inherited
-
newSecretKey(
) → Future< SecretKey> -
Generates a new SecretKey with the correct length (secretKeyLength).
inherited
-
newSecretKeyFromBytes(
List< int> bytes) → Future<SecretKey> -
Constructs a new SecretKey from the bytes.
inherited
-
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
toString(
) → String -
A string representation of this object.
override
Operators
-
operator ==(
Object other) → bool -
The equality operator.
override