aboutsummaryrefslogtreecommitdiff
path: root/lib/password.c
diff options
context:
space:
mode:
authorRob Landley <rob@landley.net>2014-06-25 22:54:59 -0500
committerRob Landley <rob@landley.net>2014-06-25 22:54:59 -0500
commit6d15f0d33fbc422689f92fbbf4dc65d3ab1fb970 (patch)
tree3b0ebc8603e933bde1c2a465972a1201928e30ec /lib/password.c
parent6ad3207b65438870dfc9f76a3dcf4bb04096297e (diff)
downloadtoybox-6d15f0d33fbc422689f92fbbf4dc65d3ab1fb970.tar.gz
Cleanup pass on mkpasswd.c
Diffstat (limited to 'lib/password.c')
-rw-r--r--lib/password.c76
1 files changed, 38 insertions, 38 deletions
diff --git a/lib/password.c b/lib/password.c
index 930e9c36..76f00436 100644
--- a/lib/password.c
+++ b/lib/password.c
@@ -6,48 +6,48 @@
#include "toys.h"
#include <time.h>
+// generate appropriate random salt string for given encryption algorithm.
int get_salt(char *salt, char *algo)
{
- int i, len = 0, offset = 0;
- char buf[12];
-
- if (!strcmp(algo,"des")) len = 2;
- else {
- *salt++ = '$';
- if (!strcmp(algo,"md5")) {
- *salt++ = '1';
- len = 8;
- } else if (!strcmp(algo,"sha256")) {
- *salt++ = '5';
- len = 16;
- } else if (!strcmp(algo,"sha512")) {
- *salt++ = '6';
- len = 16;
- } else return -1;
-
- *salt++ = '$';
- offset = 3;
- }
-
- // Read appropriate number of random bytes for salt
- i = xopen("/dev/urandom", O_RDONLY);
- xreadall(i, buf, ((len*6)+7)/8);
- close(i);
-
- // Grab 6 bit chunks and convert to characters in ./0-9a-zA-Z
- for (i=0; i<len; i++) {
- int bitpos = i*6, bits = bitpos/8;
-
- bits = ((buf[i]+(buf[i+1]<<8)) >> (bitpos&7)) & 0x3f;
- bits += 46;
- if (bits > 57) bits += 7;
- if (bits > 90) bits += 6;
-
- salt[i] = bits;
+ struct {
+ char *type, id, len;
+ } al[] = {{"des", 0, 2}, {"md5", 1, 8}, {"sha256", 5, 16}, {"sha512", 6, 16}};
+ int i;
+
+ for (i = 0; i < ARRAY_LEN(al); i++) {
+ if (!strcmp(algo, al[i].type)) {
+ int len = al[i].len;
+ char *s = salt;
+
+ if (al[i].id) {
+ *s++ = '$';
+ *s++ = '0'+al[i].id;
+ }
+ *s++ = '$';
+
+ // Read appropriate number of random bytes for salt
+ i = xopen("/dev/urandom", O_RDONLY);
+ xreadall(i, libbuf, ((len*6)+7)/8);
+ close(i);
+
+ // Grab 6 bit chunks and convert to characters in ./0-9a-zA-Z
+ for (i=0; i<len; i++) {
+ int bitpos = i*6, bits = bitpos/8;
+
+ bits = ((libbuf[i]+(libbuf[i+1]<<8)) >> (bitpos&7)) & 0x3f;
+ bits += 46;
+ if (bits > 57) bits += 7;
+ if (bits > 90) bits += 6;
+
+ s[i] = bits;
+ }
+ salt[len] = 0;
+
+ return s-salt;
+ }
}
- salt[i] = 0;
- return offset;
+ return -1;
}
static void handle(int signo)