Imported Upstream version 0.63.0
[hcoop/debian/courier-authlib.git] / random128 / random128binary.c
1 /*
2 ** Copyright 2002 Double Precision, Inc.
3 ** See COPYING for distribution information.
4 */
5
6 #if HAVE_CONFIG_H
7 #include "config.h"
8 #endif
9
10 #if HAVE_UNISTD_H
11 #include <unistd.h>
12 #endif
13 #include <ctype.h>
14 #include <string.h>
15 #include "random128.h"
16
17 static const char rcsid[]="$Id: random128binary.c,v 1.2 2004/10/21 00:10:50 mrsam Exp $";
18
19 static int nyb(char c)
20 {
21 static const char xdigit[]="0123456789ABCDEF";
22
23 const char *p=strchr(xdigit, c);
24
25 if (p)
26 return (p-xdigit);
27 return 0;
28 }
29
30 void random128_binary(random128binbuf *bytes)
31 {
32 char randombuf[ 128 / 8 * 2 + 1];
33 int i;
34
35 strcpy(randombuf, random128());
36
37 for (i=0; i<128/8; i++)
38 (*bytes)[i]=(nyb(randombuf[i*2]) << 4) | nyb(randombuf[i*2+1]);
39 }