Created
February 24, 2020 16:07
-
-
Save thedarkzeno/23d8b553b63978f846e3be9cab651c43 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
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