Yet another question regarding password hashing.
Hi,
I am developing a NodeJS back-end-server real-time-transfer module and authentication for a website that runs a single thread, there's a multitude of reasons for this. However as functions such as bcrypt/scrypt is heavily CPU intensive these could potentially cause thread blocking and in return bad end-user experience.
For many years I have used a function like this, for generating hashes and comparison.
const crypt = require("crypto");
let p = "some_random_password";
let s = salt();
let h = hash(p,s);
console.log("Hash :", h);
console.log("Salt :", s);
console.log("Password matched :", comparepass(p,h,s));
// Generate hash.
function hash(password,salt){
let r = crypt.createHash("sha256").update(password).digest("hex");
let s = crypt.createHash("sha256").update(salt).digest("hex");
for(i=0;i<10;i++)
r = crypt.createHash("sha256").update(r+s).digest("hex");
return r;
}
// Compare hash with a given password + salt.
function comparepass(password,hash,salt) {
let c = crypt.createHash("sha256").update(password).digest("hex");
let s = crypt.createHash("sha256").update(salt).digest("hex");
for(i=0;i<10;i++)
c = crypt.createHash("sha256").update(c+s).digest("hex");
return c == hash;
}
// Generate random string 42 characters long obviously.
function salt(length = 42) {
var r = '';
var characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
var cLen = characters.length;
for (var i = 0; i < length; i++)
r += characters.charAt(Math.floor(Math.random()* cLen));
return crypt.createHash("sha256").update(r).digest("hex");
}
Thought proses behind why I initially don't salt the password is that hash and salt is stored in same table. Hence attacker could potentially brute force and once string that includes salt is present know that attack was successful.
But is this safe? Or am I overlooking a obvious flaw by doing this?