- MD5Init(&ctx);
- MD5Update(&ctx, username, strlen(username));
- MD5Update(&ctx, password, n);
- MD5Final(md5pass, &ctx);
+ /* Gather some salt */
+ fd = open("/dev/urandom", O_RDONLY);
+ if (fd == -1) {
+ perror("Could not open /dev/urandom\n");
+ return 0;
+ }
+ read(fd, salt, SCRYPT_SALT_SIZE);
+ close(fd);
+
+ r = crypto_scrypt((const uint8_t *)password, n, salt, SCRYPT_SALT_SIZE, SCRYPT_N, SCRYPT_r, SCRYPT_p, pwhash, SCRYPT_OUTPUT_SIZE);
+ if (r != 0) {
+ fprintf(stderr, "Failure in scrypt for %s\n", username);
+ return 0;
+ }
+
+ /* Write the password */
+ snprintf(filename, FILENAME_MAX, "%s/%s/password", blergconf.data_path, username);
+ fd = open(filename, O_WRONLY | O_CREAT, 0600);
+ write(fd, pwhash, SCRYPT_OUTPUT_SIZE);
+ close(fd);
+
+ /* Write the salt */
+ snprintf(filename, FILENAME_MAX, "%s/%s/password_salt", blergconf.data_path, username);
+ fd = open(filename, O_WRONLY | O_CREAT, 0600);
+ write(fd, salt, SCRYPT_SALT_SIZE);
+ close(fd);