cryptography 0.2.0 copy "cryptography: ^0.2.0" to clipboard
cryptography: ^0.2.0 copied to clipboard

outdated

Cryptography for applications, including key exchange algorithms (X25519, ECDH-P256, etc.), ciphers (AES, Chacha20) and cryptographic hash functions (Blake2s, SHA2). Web Crypto API support.

Pub Package Github Actions CI

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.

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.

Digital signature algorithms #

  • P256/P384/P521 (currently browser-only)
    • P256/P384/P521 are elliptic curves approved for digital signature by NIST.

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.

Message authentication codes #

  • HMAC
  • POLY1305
    • Often used with Chacha20. The current implementation uses BigInt instead of optimized 128bit arithmetic, which is a known issue.

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).

Getting started #

1. Add dependency #

dependencies:
  cryptography: ^0.2.0

2. Use #

AEAD_CHACHA20_POLY1305 #

import 'package:cryptography/cryptography.dart';

Future<void> main() async {
  // Generate a random 256-bit secret key
  final secretKey = 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('Bytes: ${result.cipherText}');
  print('MAC: ${result.mac}');
}

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 sharedSecret = await x25519.sharedSecret(
    privateKey: localKeyPair.privateKey,
    publicKey: remoteKeyPair.publicKey,
  );
  print('Shared secret: ${sharedSecret.toHex()}');
}
245
likes
0
pub points
98%
popularity

Publisher

verified publisherdint.dev

Cryptography for applications, including key exchange algorithms (X25519, ECDH-P256, etc.), ciphers (AES, Chacha20) and cryptographic hash functions (Blake2s, SHA2). Web Crypto API support.

Repository (GitHub)
View/report issues

License

unknown (LICENSE)

Dependencies

collection, crypto, js, meta, typed_data

More

Packages that depend on cryptography