From d141fe9e91cb857b02439f2863893e842441769a Mon Sep 17 00:00:00 2001 From: furiozo Date: Tue, 31 May 2022 22:49:10 +0300 Subject: [PATCH 1/3] feat: add "rounds" possibility --- index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/index.js b/index.js index 6d9d436..d430f4b 100644 --- a/index.js +++ b/index.js @@ -1,6 +1,6 @@ 'use strict' const bindings = require('node-gyp-build')(__dirname) -bindings.verify = (passwd, hash, algo) => hash === bindings.encrypt(passwd, hash.split('$')[2], algo) +bindings.verify = (passwd, hash, algo) => hash === bindings.encrypt(passwd, hash.match(/\d\$(.+)\$/)[1], algo) const ENUM_ALGO = { '1': 'md5', From f578bb0e0840cf614c7da8a25fcb1093b8428354 Mon Sep 17 00:00:00 2001 From: jimmyolo Date: Tue, 17 Jan 2023 12:00:50 +0800 Subject: [PATCH 2/3] Add test --- test/crypt3.test.js | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/test/crypt3.test.js b/test/crypt3.test.js index ee88a7d..1793875 100644 --- a/test/crypt3.test.js +++ b/test/crypt3.test.js @@ -20,7 +20,8 @@ const data = { salt: 'salt/SHA512', algo: 'sha512', passwd: 'jimmy', - hashed: '$6$salt/SHA512$dfLSBj9voHuXzHhCmaaZvQmaIZ9f7UsOJUsoq4t3mOqKgz4ls.vKb9k6QvNND2Vwb86R1PV3rKmwBHIg4HtQj/' + hashed: '$6$salt/SHA512$dfLSBj9voHuXzHhCmaaZvQmaIZ9f7UsOJUsoq4t3mOqKgz4ls.vKb9k6QvNND2Vwb86R1PV3rKmwBHIg4HtQj/', + roundedHashed: '$6$rounds=5000$salt/SHA512$dfLSBj9voHuXzHhCmaaZvQmaIZ9f7UsOJUsoq4t3mOqKgz4ls.vKb9k6QvNND2Vwb86R1PV3rKmwBHIg4HtQj/' }, 'sha256': { openssl: 'openssl passwd -5 -salt salt/SHA256 jimmy', @@ -47,6 +48,13 @@ test('Encryption', (batch) => { t.ok(hashedPassword === data['sha512'].hashed) }) + batch.test('sha512 with round=5000', (t) => { + t.plan(1) + const hashedPassword = encrypt(password, 'rounds=5000$salt/SHA512', 'sha512') + console.log(hashedPassword) + t.ok(hashedPassword === data['sha512'].roundedHashed) + }) + batch.test('sha256', (t) => { t.plan(1) const hashedPassword = encrypt(password, 'salt/SHA256', 'sha256') From 1c488d7935d88c67db0b24dc9c8d392ce6afdfd1 Mon Sep 17 00:00:00 2001 From: jimmyolo Date: Tue, 17 Jan 2023 12:32:24 +0800 Subject: [PATCH 3/3] update doc --- README.md | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 92113ff..881cf23 100644 --- a/README.md +++ b/README.md @@ -11,13 +11,23 @@ $ npm install crypt3-passwd ## Usage #### encrypt(password: String, salt: String, algo: String) - password (required) - - salt (optional): if missing, will auto create random salt + - salt (optional): if missing, will auto create random salt (length=16) without rounds part - algo (optional): 'md5'|'sha256'|'sha512'(default) #### verify(password: String, hashed: String) ```js const crypt3 = require('crypt3-passwd') -crypt3.encrypt('jimmy', 'salt', 'sha512') // '$6$salt$n6uyPG4ghvr5KGSCwnvMIoR7415LScAxExYuSwntPu3nzYunXfOyoxGjztZipEmt72wJaBwALWuV24MscmUBe1' -crypt3.verify('jimmy', '$6$salt$n6uyPG4ghvr5KGSCwnvMIoR7415LScAxExYuSwntPu3nzYunXfOyoxGjztZipEmt72wJaBwALWuV24MscmUBe1') // true +crypt3.verify( + 'jimmy', + crypt3.encrypt('jimmy', 'salt', 'sha512') +) + +// user-supplied hashing rounds support as well +crypt3.verify( + 'jimmy', + crypt3.encrypt('jimmy', 'rounds=1000$salt') +) +// rounds number should be between `1000` and `999999999` +// see: https://man7.org/linux/man-pages/man3/crypt.3.html for more details ``` \ No newline at end of file