Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add "rounds" possibility #66

Merged
merged 3 commits into from
Jan 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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