Created
August 23, 2016 03:36
-
-
Save kierdavis/4060816cf687c64dea80608c4cffaf75 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdint.h> | |
#include <stdlib.h> | |
#include <stdio.h> | |
#include <time.h> | |
#define ITERATIONS 1000000 | |
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) ((uint8_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; | |
} | |
int reverse_hash(uint16_t hash, char *password_out, int password_len) { | |
hash ^= ((uint16_t) password_len) ^ 0xCE4B; | |
if (hash & 0x8000) return 0; | |
for (int char_index = 0; char_index < password_len; char_index++) { | |
hash = (hash >> 1) | ((hash & 0x0001) << 14); | |
uint16_t c = hash & 0x007F; | |
password_out[char_index] = (char) ((uint8_t) c); | |
hash ^= c; | |
} | |
return hash == 0; | |
} | |
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)); | |
*/ | |
uint16_t input_hash = 0xFEED; | |
printf("input hash: %04X\n", input_hash); | |
const int n = 8; | |
char rev_password[n]; | |
if (!reverse_hash(input_hash, rev_password, n)) { | |
printf("reverse_hash failed (try adjusting n).\n"); | |
return 0; | |
} | |
printf("reversed password: "); | |
for (int i = 0; i < n; i++) { | |
printf("%02X", (uint8_t) rev_password[i]); | |
} | |
printf("\n"); | |
printf("hash of reversed password: %04X\n", get_password_hash(rev_password, n)); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment