When I was designing a secure authentication system using express, I did some tests on bcryptjs package for hashing passwords. It generates a salt with salt = bcrypt.genSaltSync() and as instructions in official docs when I salted the password from hash = bcrypt.hashSync(pwd, salt), I found out that it actually salts the hash, but not the raw password. As example, when I used the password 'sidx',
the salt: $2a$10$4k/5/KcJq9FbdoY1gynpj.
the hash: $2a$10$4k/5/KcJq9FbdoY1gynpj.uxmftRhH4PW4TWYxcrXizPNUTDM6VwG
(you can notice the hash itself has been salted instead of the password.)
Now I have 2 questions.
- Instead of this can we first salt the raw password and then hash it?
- when comparing hash and raw password, we don't need to give the salt to compare function. But there is no way to identify till which character the hash is salted? but the express knows how to seperate the salt from hash exactly? How is express doing that?