Mercurial > hg > toybox
comparison lib/password.c @ 1090:7c62d5db4484 draft
Minor lib/password.c cleanup, described on the list. (Inline two functions.)
author | Rob Landley <rob@landley.net> |
---|---|
date | Wed, 16 Oct 2013 20:01:46 -0500 |
parents | 77235d224b1d |
children | bbed38cf7236 |
comparison
equal
deleted
inserted
replaced
1089:77235d224b1d | 1090:7c62d5db4484 |
---|---|
5 | 5 |
6 #include "toys.h" | 6 #include "toys.h" |
7 #include "xregcomp.h" | 7 #include "xregcomp.h" |
8 #include <time.h> | 8 #include <time.h> |
9 | 9 |
10 static unsigned int random_number_generator(int fd) | |
11 { | |
12 unsigned int randnum; | |
13 | |
14 xreadall(fd, &randnum, sizeof(randnum)); | |
15 return randnum; | |
16 } | |
17 | |
18 static char inttoc(int i) | |
19 { | |
20 // salt value uses 64 chracters in "./0-9a-zA-Z" | |
21 const char character_set[]="./0123456789abcdefghijklmnopqrstuvwxyz" | |
22 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; | |
23 | |
24 i &= 0x3f; // masking for using 10 bits only | |
25 return character_set[i]; | |
26 } | |
27 | |
28 int get_salt(char *salt, char *algo) | 10 int get_salt(char *salt, char *algo) |
29 { | 11 { |
30 int i, randfd, salt_length = 0, offset; | 12 int i, len = 0, offset = 0; |
31 | 13 char buf[12]; |
32 if (!strcmp(algo,"des")){ | 14 |
33 // 2 bytes salt value is used in des | 15 if (!strcmp(algo,"des")) len = 2; |
34 salt_length = 2; | 16 else { |
35 offset = 0; | |
36 } else { | |
37 *salt++ = '$'; | 17 *salt++ = '$'; |
38 if (!strcmp(algo,"md5")){ | 18 if (!strcmp(algo,"md5")) { |
39 *salt++ = '1'; | 19 *salt++ = '1'; |
40 // 8 bytes salt value is used in md5 | 20 len = 8; |
41 salt_length = 8; | 21 } else if (!strcmp(algo,"sha256")) { |
42 } else if (!strcmp(algo,"sha256")){ | |
43 *salt++ = '5'; | 22 *salt++ = '5'; |
44 // 16 bytes salt value is used in sha256 | 23 len = 16; |
45 salt_length = 16; | 24 } else if (!strcmp(algo,"sha512")) { |
46 } else if (!strcmp(algo,"sha512")){ | |
47 *salt++ = '6'; | 25 *salt++ = '6'; |
48 // 16 bytes salt value is used in sha512 | 26 len = 16; |
49 salt_length = 16; | |
50 } else return -1; | 27 } else return -1; |
51 | 28 |
52 *salt++ = '$'; | 29 *salt++ = '$'; |
53 offset = 3; | 30 offset = 3; |
54 } | 31 } |
55 | 32 |
56 randfd = xopen("/dev/urandom", O_RDONLY); | 33 // Read appropriate number of random bytes for salt |
57 for (i=0; i<salt_length; i++) | 34 i = xopen("/dev/urandom", O_RDONLY); |
58 salt[i] = inttoc(random_number_generator(randfd)); | 35 xreadall(i, buf, ((len*6)+7)/8); |
59 salt[salt_length+1] = '\0'; | 36 close(i); |
60 xclose(randfd); | 37 |
38 // Grab 6 bit chunks and convert to characters in ./0-9a-zA-Z | |
39 for (i=0; i<len; i++) { | |
40 int bitpos = i*6, bits = bitpos/8; | |
41 | |
42 bits = ((buf[i]+(buf[i+1]<<8)) >> (bitpos&7)) & 0x3f; | |
43 bits += 46; | |
44 if (bits > 57) bits += 8; | |
45 if (bits > 90) bits += 7; | |
46 | |
47 salt[i] = bits; | |
48 } | |
49 salt[i] = 0; | |
61 | 50 |
62 return offset; | 51 return offset; |
63 } | 52 } |
64 | 53 |
65 static void handle(int signo) | 54 static void handle(int signo) |