cryptography 0.2.2 cryptography: ^0.2.2 copied to clipboard
Cryptography for applications. Key exchange (X25519, ECDH-P256, etc.), encryption (AES, Chacha20), and cryptographic hash functions (Blake2s, SHA2). Web Crypto API support.
Introduction #
This package gives you a collection of cryptographic algorithms.
Some algorithms are implemented in pure Dart. Some algorithms are implemented with Web Cryptography API and don't work outside the browser yet.
Copyright 2019 Gohilla Ltd. Licensed under the Apache License 2.0.
Links #
Available algorithms #
Key exchange algorithms #
- P256/P384/P521 (currently browser-only)
- P256/P384/P521 are elliptic curves approved for key exchange by NIST.
- X25519
- X25519 is Elliptic Curve Diffie-Hellman (ECDH) using Curve25519. The algorithm is used in protocols such as SSH, TLS, Signal, WhatsApp, and Wireguard. Performance of this Dart implementation is about 1k exchanges per second on Macbook Pro.
For more more documentation, see KeyExchangeAlgorithm.
Digital signature algorithms #
- P256/P384/P521 (currently browser-only)
- P256/P384/P521 are elliptic curves approved for digital signature by NIST.
For more more documentation, see SignatureAlgorithm.
Ciphers #
- AES (CBC, CTR, GCM) (currently browser-only)
- AES is a symmetric cipher approved by NIST.
- CHACHA20 and AEAD_CHACHA20_POLY1305
- Chacha20 is a symmetric encryption algorithm that's simpler than AES and may also perform better than AES in CPUs that don't have AES instructions. The algorithm is used in protocols such as TLS, SSH, Signal, and Wireguard. Performance of this Dart implementation is about 50-100MB/s on Macbook Pro.
For more more documentation, see Cipher.
Message authentication codes #
- HMAC
- The implementation uses package:crypto, a package by Dart SDK team.
- POLY1305
- Often used with Chacha20. The current implementation uses BigInt instead of optimized 128bit arithmetic, which is a known issue.
For more more documentation, see MacAlgorithm.
Cryptographic hash functions #
- BLAKE2S
- Blake2 is used in protocols such as WhatsApp and WireGuard.
- SHA1
- SHA1 is used by older software. The implementation uses package:crypto (a package by Dart SDK team).
- SHA2 (SHA224, SHA256, SHA384, SHA512)
- SHA2 is approved by NIST. The implementation uses package:crypto (a package by Dart SDK team).
For more more documentation, see HashAlgorithm.
Getting started #
1. Add dependency #
dependencies:
cryptography: ^0.2.2
2. Use #
AEAD_CHACHA20_POLY1305 #
In this example, we use chacha20.
import 'package:cryptography/cryptography.dart';
Future<void> main() async {
// Generate a random 256-bit secret key
final secretKey = await chacha20.newSecretKey();
// Generate a random 96-bit nonce.
final nonce = chacha20.newNonce();
// Encrypt
final result = await chacha20Poly1305Aead.encrypt(
[1, 2, 3],
secretKey: secretKey,
nonce: nonce, // The same secretKey/nonce combination should not be used twice
aad: const <int>[], // You can authenticate additional data here
);
print('Ciphertext: ${result.cipherText}');
print('MAC: ${result.mac}');
}
X25519 #
In this example, we use x25519.
import 'package:cryptography/cryptography.dart';
void main() async {
// Let's generate two keypairs.
final localKeyPair = await x25519.newKeyPair();
final remoteKeyPair = await x5519.newKeyPair();
// We can now calculate a shared secret
var secretKey = await x25519.sharedSecret(
localPrivateKey: localKeyPair.privateKey,
remotePublicKey: remoteKeyPair.publicKey,
);
}