summaryrefslogtreecommitdiff
path: root/lib
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
parent6ad3207b65438870dfc9f76a3dcf4bb04096297e (diff)
downloadtoybox-6d15f0d33fbc422689f92fbbf4dc65d3ab1fb970.tar.gz
toybox-6d15f0d33fbc422689f92fbbf4dc65d3ab1fb970.tar.bz2
toybox-6d15f0d33fbc422689f92fbbf4dc65d3ab1fb970.zip
Cleanup pass on mkpasswd.c
Diffstat (limited to 'lib')
-rw-r--r--lib/lib.h3
-rw-r--r--lib/password.c76
-rw-r--r--lib/pending.h1
3 files changed, 41 insertions, 39 deletions
diff --git a/lib/lib.h b/lib/lib.h
index dfbe5e1..9deacf2 100644
--- a/lib/lib.h
+++ b/lib/lib.h
@@ -165,6 +165,9 @@ int human_readable(char *buf, unsigned long long num);
// net.c
int xsocket(int domain, int type, int protocol);
+// password.c
+int get_salt(char *salt, char * algo);
+
// getmountlist.c
struct mtab_list {
struct mtab_list *next, *prev;
diff --git a/lib/password.c b/lib/password.c
index 930e9c3..76f0043 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)
diff --git a/lib/pending.h b/lib/pending.h
index aa31ea9..c99a9f1 100644
--- a/lib/pending.h
+++ b/lib/pending.h
@@ -4,7 +4,6 @@
#define MAX_SALT_LEN 20 //3 for id, 16 for key, 1 for '\0'
#define SYS_FIRST_ID 100
#define SYS_LAST_ID 999
-int get_salt(char *salt, char * algo);
void is_valid_username(const char *name);
int read_password(char * buff, int buflen, char* mesg);
int update_password(char *filename, char* username, char* encrypted);