This repository has been archived by the owner on Mar 12, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 496
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #346 from manolodd/master
Updated to use another pbkdf2 library
- Loading branch information
Showing
4 changed files
with
77 additions
and
35 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
mosquitto-pbkdf2 | ||
================ | ||
[![NPM](https://nodei.co/npm/mosquitto-pbkdf2.png?downloads=true&downloadRank=true&stars=true)](https://nodei.co/npm/mosquitto-pbkdf2/) | ||
|
||
A small module to generate/validate PBKDF2-sha256 passwords as required by mosquitto-auth-plug in node.js | ||
|
||
Usage | ||
===== | ||
|
||
|
||
createPasswordAsync(password, callback); | ||
|
||
- password: Desired password (plain) | ||
- callback: To be called after PBKDF2 hash creation. Expects a string parameter that is the PBKDF2 hash generated. | ||
|
||
|
||
|
||
verifyCredentials(password, PBKDF2Hash, callback); | ||
|
||
- password: Desired password (plain) | ||
- PBKDF2Hash: A PBKDF2 in mosquitto-auth-plug format to be compared to plain 'password'. | ||
- callback: To be called after password-PBKDF2Hash verification. Expects a boolean parameter (true=Password ok, false=Password does not match). | ||
|
||
|
||
|
||
See test.js for a more detailed example. | ||
|
||
License | ||
======= | ||
|
||
MIT :-) |
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 |
---|---|---|
|
@@ -10,9 +10,9 @@ | |
* ================================================================ | ||
*/ | ||
|
||
var pbkdf2 = require('pbkdf2-sha256'); | ||
var pbkdf2 = require('pbkdf2'); | ||
var crypto = require('crypto'); | ||
// All events are managed by this event manager. | ||
// All events are managed by this event manager. | ||
var EventEmitter = require('events').EventEmitter; | ||
var localEventEmitter = new EventEmitter(); | ||
localEventEmitter.on('passwordCreationStarted', createNewHash); | ||
|
@@ -75,12 +75,12 @@ function createNewHash(plainPassword, onNewPassword) { | |
var newHash; | ||
try { | ||
newSalt = crypto.randomBytes(saltLen).toString('base64'); | ||
newHash = pbkdf2(pw, newSalt, iterations, keyLen).toString('base64'); | ||
newHash = pbkdf2.pbkdf2Sync(pw, newSalt, iterations, keyLen, algorithm).toString('base64'); | ||
newPasswordForMySQL = tag + separator + algorithm + separator + iterations + separator + newSalt + separator + newHash; | ||
} catch (err) { | ||
console.log('Not enough entropy to generate random salt'); | ||
newSalt = 'NewDefaultSalt16'; | ||
newHash = pbkdf2(pw, newSalt, iterations, keyLen).toString('base64'); | ||
newHash = pbkdf2.pbkdf2Sync(pw, newSalt, iterations, keyLen, algorithm).toString('base64'); | ||
newPasswordForMySQL = tag + separator + algorithm + separator + iterations + separator + newSalt + separator + newHash; | ||
} finally { | ||
localEventEmitter.emit('newPasswordGenerated', newPasswordForMySQL, onNewPassword); | ||
|
@@ -110,15 +110,15 @@ function onNewPasswordCreated(pbkdf2Password, onNewPassword) { | |
|
||
/** | ||
* ================================================================ | ||
* This function start the process of verifying if a given plain | ||
* password match a PBKDF2 hash (in mosquitto-auth-plug format), | ||
* This function start the process of verifying if a given plain | ||
* password match a PBKDF2 hash (in mosquitto-auth-plug format), | ||
* and returns the result of such verification. | ||
* @description Verify if plain password match the supplied PBKDF2 | ||
* hash. | ||
* @param plainPassword The plain password to be checked. | ||
* @param pbkdf2Password A PBKDF2 hash (in mosquitto-auth-plug | ||
* format) to be compared with the plain password supplied. | ||
* @param onVerificationFinished Callback function to be called | ||
* @param onVerificationFinished Callback function to be called | ||
* once the verificaation process has finished. It is passed in to | ||
* the next callback until it is necessary. | ||
* @author Manuel Domínguez-Dorado <[email protected]> | ||
|
@@ -133,7 +133,7 @@ function verifyCredentials(plainPassword, pbkdf2Password, onVerificationFinished | |
* ================================================================ | ||
* This function is a callback that is launched when a | ||
* 'passwordRecreationStarted' event is triggered. Creates a new | ||
* PBKDF2 password using the values specified as parameters and | ||
* PBKDF2 password using the values specified as parameters and | ||
* those oncluded in a previous PBKDF2 hash. It runs asynchronously | ||
* and uses the format required by mosquitto-auth-plug. On success, | ||
* it emits a 'passwordVerificationFinished' event to notify other | ||
|
@@ -143,7 +143,7 @@ function verifyCredentials(plainPassword, pbkdf2Password, onVerificationFinished | |
* @param plainPassword The plain password to be checked. | ||
* @param pbkdf2Password A PBKDF2 hash (in mosquitto-auth-plug | ||
* format) to be compared with the plain password supplied. | ||
* @param onVerificationFinished Callback function to be called | ||
* @param onVerificationFinished Callback function to be called | ||
* once the verificaation process has finished. It is passed in to | ||
* the next callback until it is necessary. | ||
* @author Manuel Domínguez-Dorado <[email protected]> | ||
|
@@ -155,11 +155,11 @@ function recreateExistingHash(plainPassword, pbkdf2Password, onVerificationFinis | |
var pw = plainPassword; | ||
var fields = []; | ||
fields = pbkdf2Password.split(separator); | ||
var storedIterations = fields[2]; | ||
var storedIterations = Number(fields[2]); | ||
var storedSalt = fields[3]; | ||
var recreatedPasswordForMySQL; | ||
var recreatedHash; | ||
recreatedHash = pbkdf2(pw, storedSalt, storedIterations, keyLen).toString('base64'); | ||
recreatedHash = pbkdf2.pbkdf2Sync(pw, storedSalt, storedIterations, keyLen, algorithm).toString('base64'); | ||
recreatedPasswordForMySQL = tag + separator + algorithm + separator + iterations + separator + storedSalt + separator + recreatedHash; | ||
if (pbkdf2Password === recreatedPasswordForMySQL) { | ||
localEventEmitter.emit('passwordVerificationFinished', true, onVerificationFinished); | ||
|
@@ -190,4 +190,4 @@ function onPasswordVerificationFinished(validPassword, onVerificationFinished) { | |
} | ||
|
||
exports.createPasswordAsync = createPasswordAsync; | ||
exports.verifyCredentials = verifyCredentials; | ||
exports.verifyCredentials = verifyCredentials; |
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 |
---|---|---|
@@ -1,22 +1,35 @@ | ||
{ | ||
"name": "mosquitto-pbkdf2", | ||
"version": "0.1", | ||
"description": "Small module to generate/verify PBKDF2 as needed by mosquitto-auth-plug", | ||
"main": "./mosquitto_pbkdf2.js", | ||
"author": { | ||
"name": "Manuel Domínguez", | ||
"email": "[email protected]" | ||
}, | ||
"license": "MIT", | ||
"devDependencies": { | ||
"pbkdf2-sha256": "~1.0.1", | ||
"prompt": "~0.2.14" | ||
}, | ||
"keywords": [ | ||
"pbkdf2", | ||
"sha256", | ||
"mosquitto", | ||
"mosquitto-auth-plug" | ||
] | ||
} | ||
"name": "mosquitto-pbkdf2", | ||
"version": "0.2.0", | ||
"description": "Small module to generate/verify PBKDF2 as needed by mosquitto-auth-plug", | ||
"main": "mosquitto_pbkdf2.js", | ||
"scripts": { | ||
"test": "node test.js", | ||
"pwd_compare": "node np.js" | ||
}, | ||
"author": "Manuel Domínguez Dorado", | ||
"email": "[email protected]", | ||
"keywords": [ | ||
"pbkdf2", | ||
"mosquitto", | ||
"password", | ||
"hash", | ||
"mosquitto-auth-plug", | ||
"authentication" | ||
], | ||
"contributors": [ | ||
{ | ||
"name": "Daniel Jimenez Jerez", | ||
"email": "[email protected]", | ||
"url": "https://github.com/djimenezjerez" | ||
} | ||
], | ||
"repository": "https://github.com/manolodd/mosquitto-pbkdf2", | ||
"dependencies": { | ||
"pbkdf2": "3.0.14", | ||
"prompt": "1.0.0" | ||
}, | ||
"engines": { | ||
"node": ">= 0.10.0" | ||
} | ||
} |