@vlasky/shacrypt 0.3.0 → 0.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +32 -0
- package/README.md +66 -9
- package/package.json +2 -2
- package/shacrypt.js +45 -11
- package/test/tests.js +47 -3
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
# Changelog
|
|
2
|
+
|
|
3
|
+
## [0.4.0] - 2025-06-27
|
|
4
|
+
|
|
5
|
+
### Added
|
|
6
|
+
- New ES6 async/await compatible functions: `sha256cryptAsync()` and `sha512cryptAsync()`
|
|
7
|
+
- These functions return Promises and can be used with modern async/await syntax
|
|
8
|
+
- Full test coverage for the new async functions
|
|
9
|
+
|
|
10
|
+
### Changed
|
|
11
|
+
- Updated documentation to include examples of the new async/await API
|
|
12
|
+
|
|
13
|
+
## [0.3.0] - 2025-06-14
|
|
14
|
+
|
|
15
|
+
### Added
|
|
16
|
+
- Input validation and error handling to prevent crashes
|
|
17
|
+
- Support for Node.js 16-23
|
|
18
|
+
|
|
19
|
+
## [0.2.0] - 2019-12-16
|
|
20
|
+
|
|
21
|
+
### Added
|
|
22
|
+
- Asynchronous support through optional callback functions
|
|
23
|
+
- Windows build compatibility improvements
|
|
24
|
+
- Cross-platform portability enhancements
|
|
25
|
+
|
|
26
|
+
### Changed
|
|
27
|
+
- Updated nan to latest version 2.14.0 to make it work under Node.js v12
|
|
28
|
+
- Replaced endian.h with portable_endian.h for better cross-platform support
|
|
29
|
+
|
|
30
|
+
## Previous versions
|
|
31
|
+
|
|
32
|
+
See git history for older changes.
|
package/README.md
CHANGED
|
@@ -4,9 +4,11 @@
|
|
|
4
4
|
|
|
5
5
|
shacrypt provides cross-platform support for SHA-256 crypt and SHA-512 crypt in Node.js. It does not use the Node.js crypto API. Instead, it is implemented as a Node.js addon that wraps around the [C implementation programmed by Ulrich Drepper](http://www.akkadia.org/drepper/SHA-crypt.txt). This provides significantly increased performance.
|
|
6
6
|
|
|
7
|
-
shacrypt provides
|
|
7
|
+
shacrypt provides four functions:
|
|
8
|
+
- `sha256crypt()` and `sha512crypt()` - can be used both synchronously and asynchronously via callbacks
|
|
9
|
+
- `sha256cryptAsync()` and `sha512cryptAsync()` - Promise-based async functions that support ES6 async/await
|
|
8
10
|
|
|
9
|
-
Asynchronous mode is especially useful in that computation is performed in Node.js's libuv thread pool. This avoids blocking the event loop which results in better app responsiveness.
|
|
11
|
+
Asynchronous mode is especially useful in that computation is performed in Node.js's libuv thread pool. This avoids blocking the event loop which results in better app responsiveness. The callback-based functions follow Node.js convention with the callback function provided as the final argument with the signature `function(error, result){}`. The async/await functions return Promises and can be used with modern async/await syntax.
|
|
10
12
|
|
|
11
13
|
### Installation
|
|
12
14
|
|
|
@@ -17,35 +19,90 @@ npm install @vlasky/shacrypt
|
|
|
17
19
|
NOTE: You will need to have C++ build tools installed on your system to successfully install the package. If you are running under Windows, you can download Microsoft's [Build Tools for Visual Studio 2017](https://www.visualstudio.com/downloads/#build-tools-for-visual-studio-2017).
|
|
18
20
|
|
|
19
21
|
### Usage
|
|
20
|
-
*
|
|
22
|
+
* Import the package:
|
|
21
23
|
|
|
22
24
|
```javascript
|
|
23
|
-
|
|
25
|
+
// CommonJS
|
|
26
|
+
const shacrypt = require('@vlasky/shacrypt');
|
|
27
|
+
|
|
28
|
+
// ES6 modules
|
|
29
|
+
import * as shacrypt from '@vlasky/shacrypt';
|
|
24
30
|
```
|
|
25
31
|
* Generate password hash:
|
|
26
32
|
|
|
27
33
|
```javascript
|
|
28
|
-
|
|
34
|
+
const hash = shacrypt.sha256crypt('super password');
|
|
29
35
|
// hash = $5$rounds=5000$3a1afb28e54a0391$0d6RupbpABtxCaH8WWOemYwEcToDVZXX/tHpIy6O1U3
|
|
30
36
|
```
|
|
31
37
|
* Validate password:
|
|
32
38
|
|
|
33
39
|
```javascript
|
|
34
|
-
console.log(hash
|
|
40
|
+
console.log(hash === shacrypt.sha256crypt('super password', hash));
|
|
35
41
|
// true
|
|
36
42
|
```
|
|
37
43
|
* Specify number of rounds (default is 5000):
|
|
38
44
|
|
|
39
45
|
```javascript
|
|
40
|
-
|
|
46
|
+
const hash = shacrypt.sha256crypt('super password', 10000);
|
|
41
47
|
// hash = $5$rounds=10000$b2c0a3ef466b2ec7$poVvVeQAQSE.yec0LBFddzQ9kZ4UxzA5VtsZQShAyt8
|
|
42
48
|
```
|
|
43
49
|
* Provide your own SALT:
|
|
44
50
|
|
|
45
51
|
```javascript
|
|
46
|
-
|
|
52
|
+
const hash = shacrypt.sha256crypt('super password', 'my-salt');
|
|
47
53
|
// or with iterations=10000
|
|
48
|
-
|
|
54
|
+
const hash = shacrypt.sha256crypt('super password', 'my-salt', 10000);
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
### Async/Await Usage
|
|
58
|
+
|
|
59
|
+
The Promise-based async functions can be used with ES6 async/await syntax:
|
|
60
|
+
|
|
61
|
+
* Basic usage with async/await:
|
|
62
|
+
|
|
63
|
+
```javascript
|
|
64
|
+
// CommonJS
|
|
65
|
+
const shacrypt = require('@vlasky/shacrypt');
|
|
66
|
+
|
|
67
|
+
// ES6 modules
|
|
68
|
+
import * as shacrypt from '@vlasky/shacrypt';
|
|
69
|
+
|
|
70
|
+
async function hashPassword() {
|
|
71
|
+
const hash = await shacrypt.sha256cryptAsync('super password');
|
|
72
|
+
console.log(hash);
|
|
73
|
+
// hash = $5$rounds=5000$3a1afb28e54a0391$0d6RupbpABtxCaH8WWOemYwEcToDVZXX/tHpIy6O1U3
|
|
74
|
+
}
|
|
75
|
+
```
|
|
76
|
+
|
|
77
|
+
* Validate password with async/await:
|
|
78
|
+
|
|
79
|
+
```javascript
|
|
80
|
+
async function validatePassword(password, hash) {
|
|
81
|
+
const computedHash = await shacrypt.sha256cryptAsync(password, hash);
|
|
82
|
+
return hash === computedHash;
|
|
83
|
+
}
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
* Using SHA-512 with custom rounds:
|
|
87
|
+
|
|
88
|
+
```javascript
|
|
89
|
+
async function strongHash() {
|
|
90
|
+
const hash = await shacrypt.sha512cryptAsync('super password', 'my-salt', 10000);
|
|
91
|
+
console.log(hash);
|
|
92
|
+
}
|
|
93
|
+
```
|
|
94
|
+
|
|
95
|
+
* Error handling:
|
|
96
|
+
|
|
97
|
+
```javascript
|
|
98
|
+
async function hashWithErrorHandling() {
|
|
99
|
+
try {
|
|
100
|
+
const hash = await shacrypt.sha512cryptAsync('password');
|
|
101
|
+
return hash;
|
|
102
|
+
} catch (error) {
|
|
103
|
+
console.error('Hashing failed:', error);
|
|
104
|
+
}
|
|
105
|
+
}
|
|
49
106
|
```
|
|
50
107
|
|
|
51
108
|
## Authors
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@vlasky/shacrypt",
|
|
3
|
-
"description": "
|
|
4
|
-
"version": "0.
|
|
3
|
+
"description": "Node.js wrapper over sha256-crypt and sha512-crypt functions originally created by Ulrich Drepper https://www.akkadia.org/drepper/SHA-crypt.txt",
|
|
4
|
+
"version": "0.4.0",
|
|
5
5
|
"homepage": "https://github.com/vlasky/shacrypt",
|
|
6
6
|
"author": "Vlad Lasky <github@vladlasky.com> (https://github.com/vlasky/)",
|
|
7
7
|
"contributors": [
|
package/shacrypt.js
CHANGED
|
@@ -1,20 +1,24 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
3
|
+
const shacrypt = require('./build/Release/shacrypt');
|
|
4
|
+
const crypto = require('crypto');
|
|
5
|
+
const util = require('util');
|
|
5
6
|
|
|
6
|
-
|
|
7
|
-
|
|
7
|
+
const sha256cryptAsync = util.promisify(shacrypt.sha256cryptasync);
|
|
8
|
+
const sha512cryptAsync = util.promisify(shacrypt.sha512cryptasync);
|
|
9
|
+
|
|
10
|
+
const isString = (obj) => {
|
|
11
|
+
return Object.prototype.toString.call(obj) === '[object String]';
|
|
8
12
|
};
|
|
9
13
|
|
|
10
|
-
|
|
11
|
-
return Object.prototype.toString.call(obj)
|
|
14
|
+
const isNumber = (obj) => {
|
|
15
|
+
return Object.prototype.toString.call(obj) === '[object Number]';
|
|
12
16
|
};
|
|
13
17
|
|
|
14
18
|
function validate(prefix, password, salt, rounds) {
|
|
15
19
|
|
|
16
|
-
|
|
17
|
-
|
|
20
|
+
const _rounds = rounds || 5000;
|
|
21
|
+
let _salt = salt;
|
|
18
22
|
|
|
19
23
|
if (!isString(password)) {
|
|
20
24
|
throw new Error('password must be a String');
|
|
@@ -24,12 +28,12 @@ function validate(prefix, password, salt, rounds) {
|
|
|
24
28
|
_salt = crypto.randomBytes(16).toString('hex');
|
|
25
29
|
|
|
26
30
|
if (isNumber(salt)) {
|
|
27
|
-
_salt = prefix
|
|
31
|
+
_salt = `${prefix}rounds=${salt}$${_salt}`;
|
|
28
32
|
}
|
|
29
33
|
}
|
|
30
34
|
|
|
31
|
-
if (isNumber(_rounds) && _salt.indexOf(prefix)
|
|
32
|
-
_salt = prefix
|
|
35
|
+
if (isNumber(_rounds) && _salt.indexOf(prefix) === -1) {
|
|
36
|
+
_salt = `${prefix}rounds=${_rounds}$${_salt}`;
|
|
33
37
|
}
|
|
34
38
|
|
|
35
39
|
return _salt;
|
|
@@ -74,3 +78,33 @@ exports.sha512crypt = function(password, salt, rounds, callback) {
|
|
|
74
78
|
return shacrypt.sha512crypt(password, salt);
|
|
75
79
|
}
|
|
76
80
|
};
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* Generate SHA256-CRYPT hash (async/await compatible)
|
|
84
|
+
*
|
|
85
|
+
* @param {String} password
|
|
86
|
+
* @param {String} [salt]
|
|
87
|
+
* @param {Number} [rounds]
|
|
88
|
+
* @return {Promise<String>}
|
|
89
|
+
*/
|
|
90
|
+
exports.sha256cryptAsync = async function(password, salt, rounds) {
|
|
91
|
+
|
|
92
|
+
salt = validate('$5$', password, salt, rounds);
|
|
93
|
+
|
|
94
|
+
return sha256cryptAsync(password, salt);
|
|
95
|
+
};
|
|
96
|
+
|
|
97
|
+
/**
|
|
98
|
+
* Generate SHA512-CRYPT hash (async/await compatible)
|
|
99
|
+
*
|
|
100
|
+
* @param {String} password
|
|
101
|
+
* @param {String} [salt]
|
|
102
|
+
* @param {Number} [rounds]
|
|
103
|
+
* @return {Promise<String>}
|
|
104
|
+
*/
|
|
105
|
+
exports.sha512cryptAsync = async function(password, salt, rounds) {
|
|
106
|
+
|
|
107
|
+
salt = validate('$6$', password, salt, rounds);
|
|
108
|
+
|
|
109
|
+
return sha512cryptAsync(password, salt);
|
|
110
|
+
};
|
package/test/tests.js
CHANGED
|
@@ -2,9 +2,9 @@
|
|
|
2
2
|
|
|
3
3
|
/* global it */
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
const shacrypt = require('../shacrypt');
|
|
6
6
|
|
|
7
|
-
|
|
7
|
+
const tests256 = [
|
|
8
8
|
["$5$saltstring", "Hello world!",
|
|
9
9
|
"$5$saltstring$5B8vYYiY.CVt1RlTTf8KbXBH3hsxY/GNooZaBBGWEc5"
|
|
10
10
|
],
|
|
@@ -36,7 +36,7 @@ var tests256 = [
|
|
|
36
36
|
],
|
|
37
37
|
];
|
|
38
38
|
|
|
39
|
-
|
|
39
|
+
const tests512 = [
|
|
40
40
|
["$6$saltstring", "Hello world!",
|
|
41
41
|
"$6$saltstring$svn8UoSVapNtMuq1ukKS4tPQd8iKwSMHWjl/O817G3uBnIFNjnQJuesI68u4OTLiBFdcbYEdFCoEOfaS35inz1"
|
|
42
42
|
],
|
|
@@ -98,6 +98,22 @@ tests512.forEach(function(test) {
|
|
|
98
98
|
});
|
|
99
99
|
});
|
|
100
100
|
|
|
101
|
+
describe('Async/await functions', function() {
|
|
102
|
+
tests256.forEach(function(test) {
|
|
103
|
+
it(test[1] + ' (async/await)', async function() {
|
|
104
|
+
const result = await shacrypt.sha256cryptAsync(test[1], test[0]);
|
|
105
|
+
result.should.be.eql(test[2]);
|
|
106
|
+
});
|
|
107
|
+
});
|
|
108
|
+
|
|
109
|
+
tests512.forEach(function(test) {
|
|
110
|
+
it(test[1] + ' (async/await)', async function() {
|
|
111
|
+
const result = await shacrypt.sha512cryptAsync(test[1], test[0]);
|
|
112
|
+
result.should.be.eql(test[2]);
|
|
113
|
+
});
|
|
114
|
+
});
|
|
115
|
+
});
|
|
116
|
+
|
|
101
117
|
describe('Input validation', function() {
|
|
102
118
|
it('should throw error for non-string password in sha256crypt', function() {
|
|
103
119
|
expect(function() {
|
|
@@ -145,4 +161,32 @@ describe('Input validation', function() {
|
|
|
145
161
|
done();
|
|
146
162
|
}
|
|
147
163
|
});
|
|
164
|
+
|
|
165
|
+
it('should reject with error for non-string password in sha256cryptAsync', async function() {
|
|
166
|
+
try {
|
|
167
|
+
await shacrypt.sha256cryptAsync(123, 'salt');
|
|
168
|
+
throw new Error('Expected error to be thrown');
|
|
169
|
+
} catch (e) {
|
|
170
|
+
e.message.should.equal('password must be a String');
|
|
171
|
+
}
|
|
172
|
+
});
|
|
173
|
+
|
|
174
|
+
it('should reject with error for non-string password in sha512cryptAsync', async function() {
|
|
175
|
+
try {
|
|
176
|
+
await shacrypt.sha512cryptAsync(null, 'salt');
|
|
177
|
+
throw new Error('Expected error to be thrown');
|
|
178
|
+
} catch (e) {
|
|
179
|
+
e.message.should.equal('password must be a String');
|
|
180
|
+
}
|
|
181
|
+
});
|
|
182
|
+
|
|
183
|
+
it('should generate salt when not provided in sha256cryptAsync', async function() {
|
|
184
|
+
const result = await shacrypt.sha256cryptAsync('password');
|
|
185
|
+
result.should.match(/^\$5\$/);
|
|
186
|
+
});
|
|
187
|
+
|
|
188
|
+
it('should generate salt when not provided in sha512cryptAsync', async function() {
|
|
189
|
+
const result = await shacrypt.sha512cryptAsync('password');
|
|
190
|
+
result.should.match(/^\$6\$/);
|
|
191
|
+
});
|
|
148
192
|
});
|