Last active
February 18, 2020 12:39
-
-
Save marty1885/39a4d7a77684b1b5b0c9efbc080222bb to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <string> | |
#include <iostream> | |
#include <iomanip> | |
#include <random> | |
#include <openssl/sha.h> | |
inline std::string sha256(const std::string str) | |
{ | |
unsigned char hash[SHA256_DIGEST_LENGTH+1]; | |
SHA256_CTX sha256; | |
SHA256_Init(&sha256); | |
SHA256_Update(&sha256, str.c_str(), str.size()); | |
SHA256_Final(hash, &sha256); | |
std::stringstream ss; | |
for(int i=0;i<SHA256_DIGEST_LENGTH; i++) | |
ss << std::hex << std::setw(2) << std::setfill('0') << (int)hash[i]; | |
return ss.str(); | |
} | |
inline std::string generate_salt(size_t characters = 8) | |
{ | |
// We'll trust C++ to be secure for now | |
std::random_device rd{}; | |
std::mt19937 rng(rd()); | |
const std::string alphabet="0123456789abcdfghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"; | |
std::uniform_int_distribution<int> dist(0, alphabet.size()-1); | |
std::string res; | |
res.resize(characters); | |
for(size_t i=0;i<characters;i++) | |
res += alphabet[dist(rng)]; | |
return res; | |
} | |
inline std::string gen_password(const std::string& password) | |
{ | |
std::string salt = generate_salt(); | |
std::string hash = sha256(salt+password); | |
std::cout << salt + password << " " << hash << std::endl; | |
return "SHA256:"+salt+":"+hash; | |
} | |
int main() | |
{ | |
std::cout << gen_password("zxcvbn") << std::endl; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment