Skip to content

Implementations of cryptographic algorithms for encryption and decryption in Dart

License

Notifications You must be signed in to change notification settings

bitanon/cipherlib

Repository files navigation

cipherlib

plugin version dart support likes pub points popularity

Implementations of cryptographic algorithms for encryption and decryption in Dart.

Depencencies

There are only 2 dependencies used by this package:

Features

Ciphers Public class and methods Source
XOR XOR, xor, xorStream Wikipedia
ChaCha20 ChaCha20, chacha20, chacha20Stream RFC-8439
ChaCha20/Poly1305 ChaCha20Poly1305, chacha20poly1305, chacha20poly1305Stream RFC-8439
Salsa20 Salsa20, salsa20, salsa20Stream Snuffle-2005
Salsa20/Poly1305 Salsa20Poly1305, salsa20poly1305, salsa20poly1305Stream Snuffle-2005

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 'dart:convert';

import 'package:cipherlib/cipherlib.dart';
import 'package:hashlib_codecs/hashlib_codecs.dart';

void main() {
  print('----- XOR -----');
  {
    var key = [0x54];
    var inp = [0x03, 0xF1];
    var cipher = xor(inp, key);
    var plain = xor(cipher, key);
    print('  Text: ${toBinary(inp)}');
    print('   Key: ${toBinary(key)}');
    print('   XOR: ${toBinary(cipher)}');
    print(' Plain: ${toBinary(plain)}');
  }

  print('----- ChaCha20 -----');
  {
    var text = "Hide me!";
    var key = fromHex(
        "000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f");
    var nonce = fromHex("000000000000004a00000000");
    var result = chacha20poly1305(utf8.encode(text), key, nonce: nonce);
    var plain = chacha20poly1305(
      result.cipher,
      key,
      nonce: nonce,
      tag: result.tag.bytes,
    );
    print('  Text: $text');
    print('   Key: ${toHex(key)}');
    print(' Nonce: ${toHex(nonce)}');
    print('Cipher: ${toHex(result.cipher)}');
    print('   Tag: ${result.tag.hex()}');
    print(' Plain: ${utf8.decode(plain.cipher)}');
  }
}

Benchmarks

Libraries:

With 5MB message (10 iterations):

Algorithms cipherlib PointyCastle cryptography
XOR 241MB/s
ChaCha20 107.60MB/s 30.48MB/s
253% slower
ChaCha20/Poly1305 75.32MB/s 33.24MB/s
127% slower
ChaCha20/Poly1305(digest) 247.47MB/s
Salsa20 107.24MB/s 27.91MB/s
284% slower
Salsa20/Poly1305 76.42MB/s
Salsa20/Poly1305(digest) 248.50MB/s

With 1KB message (5000 iterations):

Algorithms cipherlib PointyCastle cryptography
XOR 250.20MB/s
ChaCha20 108.38MB/s 30.87MB/s
251% slower
ChaCha20/Poly1305 71.48MB/s 31.39MB/s
128% slower
ChaCha20/Poly1305(digest) 213.58MB/s
Salsa20 108.21MB/s 29.29MB/s
269% slower
Salsa20/Poly1305 72.17MB/s
Salsa20/Poly1305(digest) 217.38MB/s

With 10B message (100000 iterations):

Algorithms cipherlib PointyCastle cryptography
XOR 185.62MB/s
ChaCha20 32.03MB/s 3.91MB/s
719% slower
ChaCha20/Poly1305 9.71MB/s 4.14MB/s
134% slower
ChaCha20/Poly1305(digest) 14.31MB/s
Salsa20 32.33MB/s 3.81MB/s
748% slower
Salsa20/Poly1305 9.81MB/s
Salsa20/Poly1305(digest) 14.25MB/s

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"

About

Implementations of cryptographic algorithms for encryption and decryption in Dart

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Sponsor this project

Packages

No packages published

Languages