Skip to content

Commit

Permalink
Merge pull request #66 from furiozo-ga/rounds
Browse files Browse the repository at this point in the history
  • Loading branch information
jimmyolo authored Jan 17, 2023
2 parents bcbae75 + 1c488d7 commit 3d5a183
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 5 deletions.
16 changes: 13 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
```
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
@@ -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',
Expand Down
10 changes: 9 additions & 1 deletion test/crypt3.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -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')
Expand Down

0 comments on commit 3d5a183

Please sign in to comment.