Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add salt and timing safe compare #17

Merged
merged 2 commits into from
Oct 26, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 13 additions & 10 deletions sample/simpleidp/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,10 @@ const pemEncodedPrivateKey =
'-----END RSA PRIVATE KEY-----'

const users = {
admin:
'377feab95ad6e891272d45ad717723d75acf2efd92cbf931d72d1052636635c831d8693c64c42790e877d0a7e3a83b452c960145175025fc853bec2145984885'
admin: {
hash: '377feab95ad6e891272d45ad717723d75acf2efd92cbf931d72d1052636635c831d8693c64c42790e877d0a7e3a83b452c960145175025fc853bec2145984885',
salt: 'd75acf2efd9'
}
}

const app = express()
Expand All @@ -38,18 +40,19 @@ app.use('/api/login', (req, res) => {
let password = req.body.password
let username = req.body.username

// Here we do a simple hashed password check
crypto.pbkdf2(password, 'salt', 100000, 64, 'sha512', (err, derivedKey) => {
let user = users[username]
if (!user) {
return res.sendStatus(403)
}

// Here we do a simple hashed password check
crypto.pbkdf2(password, user.salt, 100000, 64, 'sha512', (err, derivedKey) => {
if (err) {
return res.sendStatus(403)
}

let hashedPassword = users[username]
if (!hashedPassword) {
return res.sendStatus(403)
}

if (hashedPassword !== derivedKey.toString('hex')) {
let hashedPasswordBytes = Buffer.from(user.hash, 'hex')
if (!crypto.timingSafeEqual(hashedPasswordBytes, derivedKey)) {
return res.sendStatus(403)
}

Expand Down