0.0
The project is in a healthy, maintained state
ChaCha20 stream cipher algorithm.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
 Dependencies

Development

~> 5.0
~> 13.0
 Project Readme

ChaCha20

A Ruby wrapper for DJBs ChaCha20 implementation (C code in the /ext folder). Based off/inspired by https://github.com/dubek/salsa20-ruby. Supports arbitrary seeking inside the keystream.

NOTE: This is not intended to be used in production software. Use for hobby projects only. This Gem does encryption only, it does not provide any kind of message authentication or integrity checking.

Installation

Add this Gem to your Gemfile:

gem "ruby-chacha20"

Usage

Initialize a new cipher with a 32-byte key and an 8-byte nonce (both bytestrings of class String):

cipher = ChaCha20.new(key, nonce)

Alternatively, you can set the nonce at a later point in time:

cipher = ChaCha20.new(key)
cipher.init_nonce(nonce)  # Returns the ChaCha20 object itself
                          # It will raise if nonce has already been set

Warning: Do not reuse the nonce value, since this will compromise the security of the encryption. If you need to encrypt multiple messages, use a different nonce for each message.

You can then encrypt or decrypt data with the encrypt and decrypt methods:

ciphertext = cipher.encrypt(plaintext)
plaintext = cipher.decrypt(ciphertext)

Note that these methods advance the internal position inside the key stream, so you can keep calling them for chunk-wise de-/encryption. If you want to jump to a specific byte-position in the key stream, you can use the seek method:

cipher.seek(4711)