From 23538c764df4df61c6f3bdf4fa3729fde6d8887f Mon Sep 17 00:00:00 2001 From: Glauber Portella Date: Wed, 7 Nov 2018 07:50:16 -0200 Subject: [PATCH] added example on README --- README.md | 51 ++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 50 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index adb0ba0..4680f1f 100644 --- a/README.md +++ b/README.md @@ -2,12 +2,61 @@ A port of Wordpress wp-includes/class-phpass.php class used to hash passwords. -See original PHP class at [https://www.openwall.com/phpass/]. +See original PHP class PHPass at (https://www.openwall.com/phpass/)[https://www.openwall.com/phpass/]. # Installation `npm install node-phpass` +# Password hash + +```js +const PasswordHash = require('node-phpass').PasswordHash; +const CRYPT_BLOWFISH = require('node-phpass').CRYPT_BLOWFISH; +const CRYPT_EXT_DES = require('node-phpass').CRYPT_EXT_DES; +// or +// const { PasswordHash, CRYPT_BLOWFISH, CRYPT_EXT_DES } = require('node-phpass') + +const len = 8; +const portable = true; +// major PHP version, 5 or 7, as it is a port of PHPass PHP class we rely +// on php version on gensalt_private() method, it is an optional constructor +// argument wich defaults to 7 +const phpversion = 7; + +const hasher = new PasswordHash(len, portable, phpversion); +console.log('Hashing 123456 string'); +hasher.HashPassword('123456').then(hash => console.log('Private hash: ', hash)); +hasher.HashPassword('123456', CRYPT_BLOWFISH).then(hash => console.log('BCrypt hash: ', hash)); +hasher.HashPassword('123456', CRYPT_EXT_DES).then(hash => console.log('DES hash: ', hash)); +``` + +# Verify a hash + +```js +const PasswordHash = require('node-phpass').PasswordHash; +// or +// const { PasswordHash } = require('node-phpass') + +const len = 8; +const portable = true; +// major PHP version, 5 or 7, as it is a port of PHPass PHP class we rely +// on php version on gensalt_private() method, it is an optional constructor +// argument wich defaults to 7 +const phpversion = 7; + +const hasher = new PasswordHash(len, portable, phpversion); + +const storedhash = '$P$BVaXtDXwf/ceSVp8VpLKx8bS2Y4O5F/'; +const storedhash2 = '$P$BVaXtDXwf/ceSVp8VpLKx8bS2Y4O5E/'; +const password = '123456'; +const valid = hasher.CheckPassword(password, storedhash); +const invalid = hasher.CheckPassword(password, storedhash2); + +console.log(valid ? 'OK' : 'INVALID'); +console.log(invalid ? 'OK' : 'INVALID'); +``` + # Contributing Any help on testing and improvements in this class is welcome. Fork the repo and send a PR.