cipherlib 0.0.14

Implementations of cryptographic algorithms for encryption and decryption in Dart.

cipherlib

plugin version test codecov likes pub points popularity dart support

Implementations of cryptographic algorithms for encryption and decryption in Dart.

Depencencies

There are only 1 dependency used by this package:

Features

CiphersPublic class and methodsSource
AESAES,NIST.FIPS.197
XORXOR, xor, xorStreamWikipedia
ChaCha20ChaCha20, chacha20, chacha20StreamRFC-8439
ChaCha20/Poly1305ChaCha20Poly1305, chacha20poly1305RFC-8439
XChaCha20XChaCha20, xchacha20, xchacha20Streamlibsodium
XChaCha20/Poly1305XChaCha20Poly1305, xchacha20poly1305libsodium
Salsa20Salsa20, salsa20, salsa20StreamSnuffle-2005
Salsa20/Poly1305Salsa20Poly1305, salsa20poly1305Snuffle-2005
XSalsa20XSalsa20, xsalsa20, xsalsa20Streamlibsodium
XSalsa20/Poly1305XSalsa20Poly1305, xsalsa20poly1305libsodium

Available modes for AES:

  • ECB : Electronic Codeblock
  • CBC : Cipher Block Chaining
  • CTR : Counter
  • GCM : Galois/Counter Mode
  • CFB : Cipher Feedback
  • OFB : Output Feedback
  • IGE : Infinite Garble Extension
  • PCBC : Propagating Cipher Block Chaining
  • XTS : XEX (XOR-Encrypt-XOR) Tweakable Block Cipher with Ciphertext Stealing

Getting started

The following import will give you access to all of the algorithms in this package.

import 'package:cipherlib/cipherlib.dart';

Check the API Reference for details.

Usage

Examples can be found inside the example folder.

import 'package:cipherlib/cipherlib.dart';
import 'package:hashlib/hashlib.dart';
import 'package:hashlib/codecs.dart';

void main() {
  print('----- AES -----');
  {
    var plain = 'A not very secret message';
    var key = randomBytes(32);
    var iv = randomBytes(16);
    print('  Text: $plain');
    print('   Key: ${toHex(key)}');
    print(' Nonce: ${toHex(iv)}');
    // different modes
    print('  ECB: ${toHex(AES(key).ecb().encryptString(plain))}');
    print('  CBC: ${toHex(AES(key).cbc(iv).encryptString(plain))}');
    print('  CTR: ${toHex(AES(key).ctr(iv).encryptString(plain))}');
    print('  GCM: ${toHex(AES(key).gcm(iv).encryptString(plain))}');
    print('  CFB: ${toHex(AES(key).cfb(iv).encryptString(plain))}');
    print('  OFB: ${toHex(AES(key).ofb(iv).encryptString(plain))}');
    print('  XTS: ${toHex(AES(key).xts(iv).encryptString(plain))}');
    print('  IGE: ${toHex(AES(key).ige(iv).encryptString(plain))}');
    print(' PCBC: ${toHex(AES(key).pcbc(iv).encryptString(plain))}');
  }
  print('');

  print('----- XChaCha20 -----');
  {
    var text = "Hide me!";
    var key = randomBytes(32);
    var nonce = randomBytes(24);
    // encrypt and sign
    var cipher = xchacha20poly1305(
      toUtf8(text),
      key,
      nonce: nonce,
    );
    // verify and decrypt
    var plain = xchacha20poly1305(
      cipher.data,
      key,
      nonce: nonce,
      mac: cipher.tag.bytes,
    );
    print('  Text: $text');
    print('   Key: ${toHex(key)}');
    print(' Nonce: ${toHex(nonce)}');
    print('Cipher: ${toHex(cipher.data)}');
    print('   Tag: ${cipher.tag.hex()}');
    print(' Plain: ${fromUtf8(plain.data)}');
  }
  print('');
}

Benchmarks

Libraries:

With 1MB message (10 iterations):

AlgorithmscipherlibPointyCastlecryptography
XOR5.74 Gbps
ChaCha201.21 Gbps264 Mbps
4.6x slow
ChaCha20/Poly1305765 Mbps194 Mbps
3.94x slow
289 Mbps
2.65x slow
Salsa201.23 Gbps253 Mbps
4.85x slow
Salsa20/Poly1305771 Mbps
AES-128/ECB944 Mbps161 Mbps
5.85x slow
AES-192/ECB853 Mbps145 Mbps
5.88x slow
AES-256/ECB776 Mbps127 Mbps
6.12x slow
AES-128/CBC964 Mbps157 Mbps
6.14x slow
859 Mbps
1.12x slow
AES-192/CBC872 Mbps138 Mbps
6.32x slow
783 Mbps
1.11x slow
AES-256/CBC793 Mbps123 Mbps
6.46x slow
712 Mbps
1.11x slow
AES-128/CTR944 Mbps153 Mbps
6.16x slow
497 Mbps
1.9x slow
AES-192/CTR858 Mbps136 Mbps
6.28x slow
473 Mbps
1.81x slow
AES-256/CTR781 Mbps121 Mbps
6.48x slow
449 Mbps
1.74x slow
AES-128/GCM143 Mbps11.98 Mbps
11.9x slow
129 Mbps
1.1x slow
AES-192/GCM141 Mbps11.9 Mbps
11.88x slow
129 Mbps
1.09x slow
AES-256/GCM139 Mbps11.75 Mbps
11.86x slow
126 Mbps
1.11x slow
AES-128/CFB453 Mbps658 kbps
688.39x slow
AES-192/CFB416 Mbps661 kbps
629.53x slow
AES-256/CFB378 Mbps659 kbps
573.25x slow
AES-128/OFB807 Mbps155 Mbps
5.19x slow
AES-192/OFB744 Mbps139 Mbps
5.34x slow
AES-256/OFB678 Mbps124 Mbps
5.47x slow
AES-128/XTS667 Mbps
AES-192/XTS618 Mbps
AES-256/XTS578 Mbps
AES-128/IGE836 Mbps150 Mbps
5.56x slow
AES-192/IGE762 Mbps131 Mbps
5.8x slow
AES-256/IGE698 Mbps117 Mbps
5.96x slow
AES-128/PCBC835 Mbps
AES-192/PCBC774 Mbps
AES-256/PCBC700 Mbps

With 5KB message (5000 iterations):

AlgorithmscipherlibPointyCastlecryptography
XOR6.74 Gbps
ChaCha201.26 Gbps277 Mbps
4.55x slow
ChaCha20/Poly1305765 Mbps198 Mbps
3.87x slow
280 Mbps
2.73x slow
Salsa201.25 Gbps254 Mbps
4.9x slow
Salsa20/Poly1305761 Mbps
AES-128/ECB953 Mbps165 Mbps
5.77x slow
AES-192/ECB866 Mbps144 Mbps
6.01x slow
AES-256/ECB790 Mbps128 Mbps
6.18x slow
AES-128/CBC973 Mbps158 Mbps
6.15x slow
854 Mbps
1.14x slow
AES-192/CBC879 Mbps138 Mbps
6.35x slow
774 Mbps
1.14x slow
AES-256/CBC798 Mbps123 Mbps
6.49x slow
708 Mbps
1.13x slow
AES-128/CTR950 Mbps156 Mbps
6.1x slow
503 Mbps
1.89x slow
AES-192/CTR866 Mbps138 Mbps
6.3x slow
475 Mbps
1.82x slow
AES-256/CTR780 Mbps122 Mbps
6.39x slow
450 Mbps
1.73x slow
AES-128/GCM145 Mbps11.72 Mbps
12.34x slow
131 Mbps
1.11x slow
AES-192/GCM144 Mbps11.7 Mbps
12.3x slow
128 Mbps
1.12x slow
AES-256/GCM141 Mbps11.59 Mbps
12.12x slow
128 Mbps
1.1x slow
AES-128/CFB453 Mbps136 Mbps
3.32x slow
AES-192/CFB419 Mbps121 Mbps
3.47x slow
AES-256/CFB381 Mbps108 Mbps
3.54x slow
AES-128/OFB760 Mbps158 Mbps
4.8x slow
AES-192/OFB747 Mbps139 Mbps
5.39x slow
AES-256/OFB689 Mbps123 Mbps
5.61x slow
AES-128/XTS688 Mbps
AES-192/XTS634 Mbps
AES-256/XTS595 Mbps
AES-128/IGE843 Mbps151 Mbps
5.59x slow
AES-192/IGE770 Mbps133 Mbps
5.77x slow
AES-256/IGE710 Mbps119 Mbps
5.96x slow
AES-128/PCBC843 Mbps
AES-192/PCBC778 Mbps
AES-256/PCBC690 Mbps

With 16B message (100000 iterations):

AlgorithmscipherlibPointyCastlecryptography
XOR4.65 Gbps
ChaCha20420 Mbps50.69 Mbps
8.29x slow
ChaCha20/Poly1305110 Mbps41.08 Mbps
2.68x slow
34.87 Mbps
3.15x slow
Salsa20410 Mbps48.25 Mbps
8.51x slow
Salsa20/Poly1305110 Mbps
AES-128/ECB358 Mbps54.07 Mbps
6.61x slow
AES-192/ECB313 Mbps49.44 Mbps
6.33x slow
AES-256/ECB280 Mbps45.83 Mbps
6.12x slow
AES-128/CBC312 Mbps50.29 Mbps
6.2x slow
146 Mbps
2.13x slow
AES-192/CBC286 Mbps47.28 Mbps
6.04x slow
142 Mbps
2.02x slow
AES-256/CBC254 Mbps44.26 Mbps
5.74x slow
132 Mbps
1.92x slow
AES-128/CTR493 Mbps50.47 Mbps
9.78x slow
80.75 Mbps
6.11x slow
AES-192/CTR480 Mbps46.99 Mbps
10.22x slow
78.85 Mbps
6.09x slow
AES-256/CTR425 Mbps43.79 Mbps
9.7x slow
76 Mbps
5.59x slow
AES-128/GCM27.19 Mbps6.44 Mbps
4.22x slow
41.33 Mbps
1.52x fast
AES-192/GCM27.06 Mbps6.38 Mbps
4.24x slow
40.41 Mbps
1.49x fast
AES-256/GCM26.68 Mbps6.27 Mbps
4.26x slow
39.05 Mbps
1.46x fast
AES-128/CFB307 Mbps50.4 Mbps
6.1x slow
AES-192/CFB288 Mbps46.91 Mbps
6.13x slow
AES-256/CFB254 Mbps43.66 Mbps
5.81x slow
AES-128/OFB433 Mbps51.04 Mbps
8.48x slow
AES-192/OFB423 Mbps47.07 Mbps
8.99x slow
AES-256/OFB364 Mbps44.54 Mbps
8.18x slow
AES-128/XTS229 Mbps
AES-192/XTS224 Mbps
AES-256/XTS196 Mbps
AES-128/IGE275 Mbps49.15 Mbps
5.6x slow
AES-192/IGE270 Mbps45.42 Mbps
5.94x slow
AES-256/IGE238 Mbps42.69 Mbps
5.58x slow
AES-128/PCBC303 Mbps
AES-192/PCBC288 Mbps
AES-256/PCBC248 Mbps

All benchmarks are done on AMD Ryzen 7 5800X processor and 3200MHz RAM using compiled exe

Dart SDK version: 3.3.3 (stable) (Tue Mar 26 14:21:33 2024 +0000) on "windows_x64"