+
+char *
+make_sha512_salt(int length)
+{
+ static char salt[21];
+ if(length > 16)
+ {
+ printf("SHA512 salt length too long\n");
+ exit(0);
+ }
+ salt[0] = '$';
+ salt[1] = '6';
+ salt[2] = '$';
+ generate_random_salt(&salt[3], length);
+ salt[length + 3] = '$';
+ salt[length + 4] = '\0';
+ return salt;
+}
+
+char *
+generate_poor_salt(char *salt, int length)
+{
+ int i;
+
+ srand(time(NULL));
+ for(i = 0; i < length; i++)
+ salt[i] = saltChars[rand() % 64];
+
+ return (salt);
+}
+
+char *
+generate_random_salt(char *salt, int length)
+{
+ int fd, i;
+
+ if((fd = open("/dev/urandom", O_RDONLY)) < 0)
+ return (generate_poor_salt(salt, length));
+
+ if(read(fd, salt, (size_t)length) != length)
+ {
+ close(fd);
+ return (generate_poor_salt(salt, length));
+ }
+
+ for(i = 0; i < length; i++)
+ salt[i] = saltChars[abs(salt[i]) % 64];
+
+ close(fd);
+ return (salt);
+}