21.03.2013 Views

Problem - Kevin Tafuro

Problem - Kevin Tafuro

Problem - Kevin Tafuro

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

spc_wordlist_file = f;<br />

spc_wordlist_offsets = offsets;<br />

spc_wordlist_count = count;<br />

spc_wordlist_shortest = shortest - 1; /* shortest includes NULL terminator */<br />

return 1;<br />

}<br />

static int get_wordlist_word(unsigned int num, char *buf, size_t bufsz) {<br />

size_t end, length;<br />

if (num >= spc_wordlist_count) return -1;<br />

if (num = = spc_wordlist_count - 1) {<br />

fseek(spc_wordlist_file, 0, SEEK_END);<br />

end = ftell(spc_wordlist_file);<br />

} else end = spc_wordlist_offsets[num + 1];<br />

length = end - spc_wordlist_offsets[num]; /* includes NULL terminator */<br />

if (length > bufsz) return 0;<br />

if (fseek(spc_wordlist_file, spc_wordlist_offsets[num], SEEK_SET) = = -1)<br />

return -1;<br />

fread(buf, length, 1, spc_wordlist_file);<br />

buf[length - 1] = 0;<br />

return 1;<br />

}<br />

char *spc_generate_passphrase(char *buf, size_t bufsz) {<br />

int attempts = 0, rc;<br />

char *outp;<br />

size_t left, len;<br />

unsigned int idx;<br />

if (!spc_wordlist_file && !load_wordlist( )) return 0;<br />

outp = buf;<br />

left = bufsz - 1;<br />

while (left > spc_wordlist_shortest) {<br />

idx = spc_rand_range(0, spc_wordlist_count - 1);<br />

rc = get_wordlist_word(idx, outp, left + 1);<br />

if (rc = = -1) return 0;<br />

else if (!rc && ++attempts < 10) continue;<br />

else if (!rc) break;<br />

len = strlen(outp) + 1;<br />

*(outp + len - 1) = ' ';<br />

outp += len;<br />

left -= len;<br />

}<br />

*(outp - 1) = 0;<br />

return buf;<br />

}<br />

When spc_generate_passphrase( ) is called, it opens the data file containing the<br />

words to choose from and leaves it open. In addition, depending on the size of the<br />

390 | 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!