Skip to content
This repository has been archived by the owner on Mar 12, 2019. It is now read-only.

Commit

Permalink
Merge pull request #346 from manolodd/master
Browse files Browse the repository at this point in the history
Updated to use another pbkdf2 library
  • Loading branch information
jpmens authored May 10, 2018
2 parents e76d58a + a27f436 commit 8a88406
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 35 deletions.
31 changes: 31 additions & 0 deletions contrib/nodejs/README.md
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 :-)
24 changes: 12 additions & 12 deletions contrib/nodejs/mosquitto_pbkdf2.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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]>
Expand All @@ -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
Expand All @@ -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]>
Expand All @@ -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);
Expand Down Expand Up @@ -190,4 +190,4 @@ function onPasswordVerificationFinished(validPassword, onVerificationFinished) {
}

exports.createPasswordAsync = createPasswordAsync;
exports.verifyCredentials = verifyCredentials;
exports.verifyCredentials = verifyCredentials;
4 changes: 1 addition & 3 deletions contrib/nodejs/np.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

var mosquittoPBKDF2 = require('./mosquitto_pbkdf2');
var prompt = require('prompt');

Expand All @@ -23,9 +22,8 @@ prompt.get(schema, function (err, result) {
console.log('Passwords do not match!');
} else {
mosquittoPBKDF2.createPasswordAsync(result.pwd1, function(newPBKDF2Password){
console.log('New PBKDF2 hash: '+newPBKDF2Password);
console.log('New PBKDF2 hash: '+newPBKDF2Password);
});
}
}
});

53 changes: 33 additions & 20 deletions contrib/nodejs/package.json
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"
}
}

0 comments on commit 8a88406

Please sign in to comment.