Skip to content

Latest commit

 

History

History
181 lines (139 loc) · 3.88 KB

hash.md

File metadata and controls

181 lines (139 loc) · 3.88 KB

Hash Functions

#include <maid/hash.h>

Internal Interface

struct maid_hash_def Type that defines a hash function

External Interface

maid_hash Opaque type that contains the state of a hash function
maid_hash *maid_hash_new(struct maid_hash_def def) Creates a hash function instance

Parameters

name description
def Algorithm definition

Return value

case description
Success maid_hash instance
Failure NULL
maid_hash *maid_hash_del(maid_hash *h) Deletes a hash function instance

Parameters

name description
h maid_hash instance

Return value

case description
Always NULL
void maid_hash_renew(maid_hash *h) Recreates a hash function instance

Parameters

name description
h maid_hash instance
void maid_hash_update(maid_hash *h, const u8 *buffer, size_t size) Updates the hash function state

Parameters

name description
h maid_hash instance
buffer Data to be read
size Size of the operation
void maid_hash_digest(maid_hash *h, u8 *output) Outputs the hash (One time, ending the hash function instance)

Parameters

name description
h maid_hash instance
output Block to be written on

External Algorithms

const struct maid_hash_def maid_sha224 SHA-2 224-bits hash (NIST)
const struct maid_hash_def maid_sha256 SHA-2 256-bits hash (NIST)
const struct maid_hash_def maid_sha384 SHA-2 384-bits hash (NIST)
const struct maid_hash_def maid_sha512 SHA-2 512-bits hash (NIST)
const struct maid_hash_def maid_sha512_244 SHA-2 512-bits hash, truncated to 224-bits (NIST)
const struct maid_hash_def maid_sha512_256 SHA-2 512-bits hash, truncated to 256-bits (NIST)

Example Code

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <maid/hash.h>

int main(void)
{
    maid_hash *h = maid_hash_new(maid_sha256);

    char *data = "abc";
    u8 output[32] = {0};
    if (h)
    {
        maid_hash_update(h, (u8*)data, strlen(data));
        maid_hash_digest(h, output);
    }

    maid_hash_del(h);

    for (size_t i = 0; i < sizeof(output); i++)
        printf("%02x", output[i]);
    printf("\n");

    return EXIT_SUCCESS;
}

Without installation:

cc -static -Iinclude example.c -Lbuild -lmaid

With installation:

cc example.c -lmaid