Eigenstate : libcrypto

Libcrypto contains a set of implementations of common hash functions. It supports taking the hash of an entire string, as well as a streaming API.

API Reference

pkg crypto =
        /* state types */
        type md5
        type sha1
        type sha256
        type sha224
        type sha512
        type aesctx
        type chacha20ctx

        /* md5 funtions */
        const md5       : (data : byte[:] -> byte[16])
        const md5init   : (st : md5# -> void)
        const md5add    : (st : md5#, data : byte[:] -> void)
        const md5fin    : (st : md5# -> byte[16])

        /* sha1 functions */
        const sha1      : (data : byte[:] -> byte[20])
        const sha1init  : (st : sha1# -> void)
        const sha1add   : (st : sha1#, data : byte[:] -> void)
        const sha1fin   : (st : sha1# -> byte[20])

        /* sha256 functions */
        const sha256    : (data : byte[:] -> byte[32])
        const sha256init        : (st : sha256# -> void)
        const sha256add : (st : sha256#, data : byte[:] -> void)
        const sha256fin : (st : sha256# -> byte[32])

        /* sha224 functions */
        const sha224    : (data : byte[:] -> byte[28])
        const sha224init        : (st : sha224# -> void)
        const sha224add : (st : sha224#, data : byte[:] -> void)
        const sha224fin : (st : sha224# -> byte[28])

        /* sha512 functions */
        const sha512    : (data : byte[:] -> byte[64])
        const sha512init        : (st : sha512# -> void)
        const sha512add : (st : sha512#, data : byte[:] -> void)
        const sha512fin : (st : sha512# -> byte[64])

        /* sha384 functions */
        const sha384    : (data : byte[:] -> byte[48])
        const sha384init        : (st : sha384# -> void)
        const sha384add : (st : sha384#, data : byte[:] -> void)
        const sha384fin : (st : sha384# -> byte[48])

    /* AES functions */
        const ortho : (q : uint64[:] -> void)
        const aeskeysched   : (x : aesctx#, k : byte[:] -> void) 
        const aesencrypt    : (x : aesctx#, m : byte[:], c : byte[:] -> void)
        const aesdecrypt    : (x : aesctx#, c : byte[:], m : byte[:] -> void)

        /* Chacha20 functions */
        const chacha20keysetup  : (x : chacha20ctx#, k : byte[:] -> void) 
        const chacha20ivsetup   : (x : chacha20ctx#, iv : byte[:] -> void)
        const chacha20encrypt   : (x : chacha20ctx#, m : byte[:], c : byte[:] -> void)

        /* Randomness */
        const randbytes : (buf : byte[:] -> void)
        generic rand    : (lo : @a::(integral,numeric), hi : @a::(integral,numeric) -> @a::(numeric,integral))
        generic randnum : (-> @a::(numeric,integral))
;;

The hashtype functions (eg, md5(), sha1(), etc) all compute and immediately return the hash of their argument.

The hashtypeinit() functions will initialize a hashing state of type hashtype, and prepare it to accept input.

The hashtypeadd() functions must be called on an initialized state, and will add data argument to the hashed value.

The hashtypefin() functions will return the hash value that was computed on the data added by hashtypeadd(). If hashtypeadd() was not called, the hash of 0 bytes of data will be returned.

Example: Simple hashing

Example: Simple hashing