21.03.2013 Views

Problem - Kevin Tafuro

Problem - Kevin Tafuro

Problem - Kevin Tafuro

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

#include <br />

#include <br />

#include <br />

#include <br />

static char *crypt64_encode(const unsigned char *buf) {<br />

int i;<br />

char *out, *ptr;<br />

unsigned long l;<br />

static char *crypt64_set = "./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"<br />

"abcdefghijklmnopqrstuvwxyz";<br />

if (!(out = ptr = (char *)malloc(23))) return 0;<br />

#define CRYPT64_ENCODE(x, y, z) \<br />

for (i = 0, l = (buf[(x)] = 6) *ptr++ = crypt64_set[l & 0x3F]<br />

CRYPT64_ENCODE(0, 6, 12); CRYPT64_ENCODE(1, 7, 13);<br />

CRYPT64_ENCODE(2, 8, 14); CRYPT64_ENCODE(3, 9, 15);<br />

CRYPT64_ENCODE(4, 10, 5);<br />

for (i = 0, l = buf[11]; i++ < 2; l >>= 6) *ptr++ = crypt64_set[l & 0x3F];<br />

*ptr = 0;<br />

#undef CRYPT64_ENCODE<br />

return out;<br />

}<br />

static void compute_hash(unsigned char *hash, const char *key,<br />

const char *salt, size_t salt_length) {<br />

int i, length;<br />

size_t key_length;<br />

MD5_CTX ctx, ctx1;<br />

key_length = strlen(key);<br />

MD5_Init(&ctx);<br />

MD5_Update(&ctx, key, key_length);<br />

MD5_Update(&ctx, salt, salt_length);<br />

MD5_Init(&ctx1);<br />

MD5_Update(&ctx1, key, key_length);<br />

MD5_Update(&ctx1, salt, salt_length);<br />

MD5_Update(&ctx1, key, key_length);<br />

MD5_Final(hash, &ctx1);<br />

for (length = key_length; length > 0; length -= 16)<br />

MD5_Update(&ctx, hash, (length > 16 ? 16 : length));<br />

memset(hash, 0, 16);<br />

for (i = key_length; i; i >>= 1)<br />

if (i & 1) MD5_Update(&ctx, hash, 1);<br />

else MD5_Update(&ctx, key, 1);<br />

MD5_Final(hash, &ctx);<br />

404 | Chapter 8: Authentication and Key Exchange<br />

This is the Title of the Book, eMatter Edition<br />

Copyright © 2007 O’Reilly & Associates, Inc. All rights reserved.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!