# Nimcrypto

Nimcrypto is Nim's cryptographic library. It implements several popular cryptographic algorithms and their tests with some examples (opens new window).

Most notably, this library has been used in the Nimbus Ethereum client (opens new window). To see the implementation, check out its Github repository (opens new window).

# The most basic usage

nimble install nimcrypto # installation
1
# example.nim
import nimcrypto

echo keccak_256.digest("Alice makes a hash") 
# outputs EF0CC652868DF797522FB1D5A39E58E069154D9E47E5D7DB288B7200DB6EDFEE
1
2
3
4
5

# Documentation

For usage examples of the below algorithm implementations see each module's individual page. In depth documentation also available at Nimbus Libs (opens new window).

# nimcrypto/hash

This module provides helper procedures for calculating secure digests supported by nimcrypto library.

# nimcrypto/sha2

This module implements SHA2 (Secure Hash Algorithm 2) set of cryptographic hash functions designed by National Security Agency, version FIPS-180-4. http://csrc.nist.gov/publications/fips/fips180-4/fips-180-4.pdf (opens new window)

# nimcrypto/ripemd

This module implements RIPEMD set of cryptographic hash functions, designed by Hans Dobbertin, Antoon Bosselaers and Bart Preneel. http://www.esat.kuleuven.be/~bosselae/ripemd160/pdf/AB-9601/AB-9601.pdf (opens new window)

This module is Nim adoptation of original C source code by Antoon Bosselaers. https://homes.esat.kuleuven.be/~bosselae/ripemd160/ps/AB-9601/rmd160.c (opens new window)

This module includes support of RIPEMD-128/160/256/320.

# nimcrypto/keccak

This module implements SHA3 (Secure Hash Algorithm 3) set of cryptographic hash functions designed by Guido Bertoni, Joan Daemen, Michaƫl Peeters and Gilles Van Assche.

This module supports SHA3-224/256/384/512 and SHAKE-128/256.

# nimcrypto/blake2

This module implements BLAKE2 set of cryptographic hash functions designed by Jean-Philippe Aumasson, Luca Henzen, Willi Meier, Raphael C.W. Phan.

This module supports BLAKE2s-224/256 and BLAKE2b-384/512.

# nimcrypto/hmac

This module implements HMAC (Keyed-Hashing for Message Authentication) http://www.ietf.org/rfc/rfc2104.txt (opens new window).

# nimcrypto/rijndael

This module implements Rijndael(AES) crypto algorithm by Vincent Rijmen, Antoon Bosselaers and Paulo Barreto.

Code based on version 3.0 (December 2000) of Optimised ANSI C code for the Rijndael cipher http://www.fastcrypto.org/front/misc/rijndael-alg-fst.c (opens new window).

# nimcrypto/blowfish

This module implements Blowfish crypto algorithm by Bruce Schneier

Code based on C implementation of the Blowfish algorithm created by Paul Kocher https://www.schneier.com/code/bfsh-koc.zip (opens new window).

# nimcrypto/twofish

This module implements Twofish crypto algorithm by Bruce Schneier.

Code based on Optimized C created by Drew Csillag https://www.schneier.com/code/twofish-cpy.zip (opens new window).

# nimcrypto/bcmode

This module implements various Block Cipher Modes.

The five modes currently supported:

  • ECB (Electronic Code Book)
  • CBC (Cipher Block Chaining)
  • CFB (Cipher FeedBack)
  • OFB (Output FeedBack)
  • CTR (Counter)
  • GCM (Galois/Counter Mode)

You can use any of this modes with all the block ciphers of nimcrypto library

GHASH implementation is Nim version of ghash_ctmul64.c which is part of decent BearSSL project https://bearssl.org (opens new window). Copyright (c) 2016 Thomas Pornin

# nimcrypto/utils

Utility functions common to all submodules.

# nimcrypto/sysrand

This module implements interface to operation system's random number generator.

Windows using BCryptGenRandom (if available), CryptGenRandom(PROV_INTEL_SEC) (if available), RtlGenRandom.

RtlGenRandom (available from Windows XP) BCryptGenRandom (available from Windows Vista SP1) CryptGenRandom(PROV_INTEL_SEC) (only when Intel SandyBridge CPU is available).

Linux using genrandom (if available), /dev/urandom.

OpenBSD using getentropy.

NetBSD, FreeBSD, MacOS, Solaris using /dev/urandom.