Random Numbers and Hashing

Introduction

The C-Craft library contains basic random number generators and hashing.

Random Numbers

Random numbers may be generated like so.

#include <c-craft/rng.h>

MEM_SCOPE mem = sm_create(0);
RNG rng = rng_xor_create(mem)

unsigned long x = rng_next(rng);

This library provides two random number generators. They both follow the same function pattern and are interchangeable. Which one you choose depends on your needs.

Basic Standard

The basic UNIX C rand() function. This generates integers in the range 0 to INT_MAX. IE 31 bits on most modern computers.

#include <c-craft/rng.h>

MEM_SCOPE mem = sm_create(0);
RNG rng = rng_std_create(mem)

unsigned long x = rng_next(rng);

XOR

This is a simple but fast bit crunching algorithm based on a bitwise shift and XOR operation and generates numbers in the range 0 to ULONG_MAX. IE 64 bits on most modern computers.

#include <c-craft/rng.h>

MEM_SCOPE mem = sm_create(0);
RNG rng = rng_xor_create(mem)

unsigned long x = rng_next(rng);

Hashing

One-way Hashing

One way hashing means the original value cannot be directly recovered as the hash may resolve back to multiple values. The addition of a ‘seed’ or ‘salt’ improves security by making it more difficult to reverse engineer.

#include <c-craft/rng.h>

unsigned char data[] = { 0x11, 0x22, 0x33, ... };
int len = ARRAYLEN(data);

unsigned long secret = 0x15162427db35cf07;     /* Seed with a secret value */
unsigned long h = hash_simple(data, len, secret);