Skip to content

Instantly share code, notes, and snippets.

@thedarkzeno
Created February 24, 2020 16:07
Show Gist options
  • Save thedarkzeno/23d8b553b63978f846e3be9cab651c43 to your computer and use it in GitHub Desktop.
Save thedarkzeno/23d8b553b63978f846e3be9cab651c43 to your computer and use it in GitHub Desktop.
const crypto = require("crypto");
function generateSalt(size = 32) {
const salt = crypto.randomBytes(size).toString("hex");
return salt;
}
function hashString(variable) {
return crypto
.createHash("sha256")
.update(variable)
.digest("hex");
}
function generateHash(password) {
const salt = generateSalt();
const HashPass = hashString(salt + password);
return salt + HashPass;
}
function verifyPass(hash, password, saltSize = 64) {
const salt = hash.substring(0, saltSize);
const hashedPass = salt + hashString(salt + password);
if (hash === hashedPass) {
return true;
}
return false;
}
const hash = generateHash("12345");
console.log(hash);
const verify = verifyPass(hash, "12345");
console.log(verify);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment