Introduction

A pure Dart implementation of Chacha20, a popular stream encryption algorithm. Tests utilize examples from RFC 7539, an implementation guide by the the Internet Research Task Force.

Performance on a recent Intel CPU + Dart VM is:

  • About 100MB/s when message size is 1 MB.
  • About 50MB/s when message size is 100 bytes.

Licensed under the Apache License 2.0.

A simple example

import 'dart:typed_data';

void main() {
    // ----------
    // Parameters
    // ----------
    // 256-bit (secret) key
    var key = Chacha20.randomKey();
    
    // 96-bit (non-secret) nonce
    var nonce = new Uint8List(12);
    
    // -------------
    // Convert bytes
    // -------------
    // Initialize
    final chacha20 = new Chacha20();
    chacha20.initialize(key: key, nonce: nonce);
    
    // convert message [1,2,3]
    print(chacha20.convert([1,2,3]));
    
    // ---------------------------------------
    // Convert more messages with the same key
    // ---------------------------------------
    // It's important that we don't re-use the same (key, nonce) combination.
    // So let's increment the nonce for the next message.
    nonce = Chacha20.incrementNonceBytes(nonce);
    
    // ------------------
    // Converting streams
    // ------------------
    // Let's say we have the following stream
    final stream = new Stream<List<int>>.fromIterable([[1,2,3], [5], [7,11]]);
    
    // Initialize
    chacha20.initialize(key: key, nonce: nonce);
    
    // Convert chunk-by-chunk.
    // 'keyStreamIndex' is incremented when you advance in the stream.
    final convertedStream = stream.map((chunk) {
      return chacha20.convert(chunk);
    });
    
}

Libraries

chacha20