-
Notifications
You must be signed in to change notification settings - Fork 80
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Noobaa Account: Replace bcrypt password hashing by crypto
As bcrypt is not under active maintenance, we need to replace it with node.js crypto hashing module. Signed-off-by: Ashish Pandey <aspandey@redhat.com>
- Loading branch information
Showing
3 changed files
with
55 additions
and
9 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
/* Copyright (C) 2024 NooBaa */ | ||
'use strict'; | ||
|
||
const CRYPTO_SALT_BUFFER_SIZE = 8; | ||
const CRYPTO_SALT_KEY_LENGTH = 32; | ||
const CRYPTO_PBKDF2_ITERATIONS = 100; | ||
|
||
const crypto = require('crypto'); | ||
|
||
function create_node_password_hash(password) { | ||
return new Promise((resolve, reject) => { | ||
const salt = crypto.randomBytes(CRYPTO_SALT_BUFFER_SIZE).toString('hex'); | ||
crypto.pbkdf2(password, salt, CRYPTO_PBKDF2_ITERATIONS, CRYPTO_SALT_KEY_LENGTH, 'sha512', (err, derivedKey) => { | ||
if (err) { | ||
reject(err); | ||
} | ||
resolve(salt + ':' + derivedKey.toString('hex')); | ||
}); | ||
}); | ||
} | ||
|
||
function verify_node_password_hash(password, hashedPassword) { | ||
return new Promise((resolve, reject) => { | ||
const [salt, key] = hashedPassword.split(':'); | ||
crypto.pbkdf2(password, salt, CRYPTO_PBKDF2_ITERATIONS, CRYPTO_SALT_KEY_LENGTH, 'sha512', (err, derivedKey) => { | ||
if (err) { | ||
reject(err); | ||
} | ||
resolve(key === derivedKey.toString('hex')); | ||
}); | ||
}); | ||
} | ||
|
||
|
||
exports.create_node_password_hash = create_node_password_hash; | ||
exports.verify_node_password_hash = verify_node_password_hash; |