Skip to content

Instantly share code, notes, and snippets.

@byronhe byronhe/scrypt_demo.cpp
Last active Feb 25, 2016

Embed
What would you like to do?
#include <sodium.h>
#include <iostream>
#include <string>
#include <string.h>
using namespace std;
/*
* reference:
* https://download.libsodium.org/doc/password_hashing/index.html
* https://download.libsodium.org/libsodium/releases/
*/
int main() {
//进程启动时要调用一次
sodium_init();
char password[128] = {};
for (int s = 0; s < 32; ++s) {
password[s] = (randombytes_random()) % 26 + 'a';
}
sodium_mlock(&password[0], sizeof(password));
cout << "test password is: " << password << endl;
char hashed_password[crypto_pwhash_scryptsalsa208sha256_STRBYTES];
if (crypto_pwhash_scryptsalsa208sha256_str(
hashed_password, password, strlen(password),
crypto_pwhash_scryptsalsa208sha256_OPSLIMIT_INTERACTIVE * 2,
crypto_pwhash_scryptsalsa208sha256_MEMLIMIT_INTERACTIVE * 2) != 0) {
cout << "scrypt faild, out of memory." << endl;
} else {
cout << "hash ok. insert hash result:\"" << hashed_password
<< "\" to your database." << endl;
}
if (crypto_pwhash_scryptsalsa208sha256_str_verify(hashed_password, password,
strlen(password)) == 0) {
cout << "verify ok, correct password!" << endl;
} else {
cout << "verify failed, wrong password !" << endl;
}
sodium_munlock(&password[0], sizeof(password));
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.