Skip to content

Instantly share code, notes, and snippets.

@kierdavis
Created August 23, 2016 03:07
Show Gist options
  • Save kierdavis/baaf0165e2efadd0338a22678909940d to your computer and use it in GitHub Desktop.
Save kierdavis/baaf0165e2efadd0338a22678909940d to your computer and use it in GitHub Desktop.
#include <stdint.h>
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#define ITERATIONS 10000000
uint16_t get_password_hash(const char *password, int password_len) {
register uint16_t hash = 0;
register int char_index = password_len;
do {
hash ^= (uint16_t) password[--char_index];
// bit 15 is always zero, so don't need to worry about it
hash = (hash >> 14) | ((hash & 0x3FFF) << 1);
} while (char_index > 0);
return hash ^ ((uint16_t) password_len) ^ 0xCE4B;
}
volatile uint16_t output_hash;
int main() {
srand(time(NULL));
printf("Check: %04X == FEF1\n", get_password_hash("abcdefghij", 10));
clock_t total = 0;
char input[15];
for (int i = 0; i < ITERATIONS; i++) {
int length = 10 + (rand() % 6); // 10 to 15 inclusive
for (int j = 0; j < length; j++) {
input[j] = rand() % 256;
}
clock_t start = clock();
output_hash = get_password_hash(input, length);
clock_t end = clock();
total += end - start;
}
const double secs = ((double) total) / ((double) CLOCKS_PER_SEC);
const double secs_per_it = secs / ITERATIONS;
printf("Benchmark: %d ns per iteration\n", (int) (secs_per_it * 1e9));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment