@vlasky/shacrypt 0.3.0 → 0.5.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 ADDED
@@ -0,0 +1,44 @@
1
+ # Changelog
2
+
3
+ ## [0.5.0] - 2026-01-28
4
+
5
+ ### Changed
6
+ - **Breaking:** Minimum Node.js version is now 18.0.0
7
+ - Updated NAN to 2.25.0 for Node.js 25 compatibility
8
+ - Updated mocha to 11.3.0 and chai to 6.2.2
9
+ - Replaced `NODE_MODULE` with `NAN_MODULE_WORKER_ENABLED` for proper module registration
10
+ - Fixed null pointer subtraction warnings in sha256crypt.c and sha512crypt.c using `uintptr_t`
11
+
12
+ ### Added
13
+ - Support for Node.js 25
14
+
15
+ ## [0.4.0] - 2025-06-27
16
+
17
+ ### Added
18
+ - New ES6 async/await compatible functions: `sha256cryptAsync()` and `sha512cryptAsync()`
19
+ - These functions return Promises and can be used with modern async/await syntax
20
+ - Full test coverage for the new async functions
21
+
22
+ ### Changed
23
+ - Updated documentation to include examples of the new async/await API
24
+
25
+ ## [0.3.0] - 2025-06-14
26
+
27
+ ### Added
28
+ - Input validation and error handling to prevent crashes
29
+ - Support for Node.js 16-23
30
+
31
+ ## [0.2.0] - 2019-12-16
32
+
33
+ ### Added
34
+ - Asynchronous support through optional callback functions
35
+ - Windows build compatibility improvements
36
+ - Cross-platform portability enhancements
37
+
38
+ ### Changed
39
+ - Updated nan to latest version 2.14.0 to make it work under Node.js v12
40
+ - Replaced endian.h with portable_endian.h for better cross-platform support
41
+
42
+ ## Previous versions
43
+
44
+ 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 two functions `sha256crypt()` and `sha512crypt()` that can be used both synchronously and asynchronously via callbacks.
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. As per Node.js convention, the callback function is provided as the final argument with the signature `function(error, result){}`.
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
- * Prerequisite
22
+ * Import the package:
21
23
 
22
24
  ```javascript
23
- var shacrypt = require('shacrypt');
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
- var hash = shacrypt.sha256crypt('super password');
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 == shacrypt.sha256crypt('super password', 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
- var hash = shacrypt.sha256crypt('super password', 10000);
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
- var hash = shacrypt.sha256crypt('super password', 'my-salt');
52
+ const hash = shacrypt.sha256crypt('super password', 'my-salt');
47
53
  // or with iterations=10000
48
- var hash = shacrypt.sha256crypt('super password', 'my-salt', 10000);
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": "Wrapper over sha256-crypt and sha512-crypt functions originally created by Ulrich Drepper https://www.akkadia.org/drepper/SHA-crypt.txt",
4
- "version": "0.3.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.5.0",
5
5
  "homepage": "https://github.com/vlasky/shacrypt",
6
6
  "author": "Vlad Lasky <github@vladlasky.com> (https://github.com/vlasky/)",
7
7
  "contributors": [
@@ -13,21 +13,21 @@
13
13
  "test": "make test"
14
14
  },
15
15
  "dependencies": {
16
- "nan": "^2.22.2"
16
+ "nan": "^2.25.0"
17
17
  },
18
18
  "devDependencies": {
19
- "mocha": "^10.8.2",
20
- "chai": "^4.5.0"
19
+ "chai": "^6.2.2",
20
+ "mocha": "^11.3.0"
21
21
  },
22
22
  "engines": {
23
- "node": ">= 16.0.0"
23
+ "node": ">= 18.0.0"
24
24
  },
25
25
  "bugs": {
26
26
  "url": "https://github.com/vlasky/shacrypt/issues"
27
27
  },
28
28
  "repository": {
29
- "type": "git",
30
- "url": "git://github.com/vlasky/shacrypt.git"
29
+ "type": "git",
30
+ "url": "git://github.com/vlasky/shacrypt.git"
31
31
  },
32
32
  "gypfile": true,
33
33
  "readmeFilename": "README.md",
package/shacrypt.js CHANGED
@@ -1,20 +1,24 @@
1
1
  "use strict";
2
2
 
3
- var shacrypt = require('./build/Release/shacrypt');
4
- var crypto = require('crypto');
3
+ const shacrypt = require('./build/Release/shacrypt');
4
+ const crypto = require('crypto');
5
+ const util = require('util');
5
6
 
6
- var isString = function(obj) {
7
- return Object.prototype.toString.call(obj) == '[object String]';
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
- var isNumber = function(obj) {
11
- return Object.prototype.toString.call(obj) == '[object Number]';
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
- var _salt = salt,
17
- _rounds = rounds || 5000;
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 + 'rounds=' + salt + '$' + _salt;
31
+ _salt = `${prefix}rounds=${salt}$${_salt}`;
28
32
  }
29
33
  }
30
34
 
31
- if (isNumber(_rounds) && _salt.indexOf(prefix) == -1) {
32
- _salt = prefix + 'rounds=' + _rounds + '$' + _salt;
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/src/sha256crypt.c CHANGED
@@ -364,21 +364,21 @@ sha256_crypt_r (const char *key, const char *salt, char *buffer, int buflen)
364
364
  salt_len = MIN (strcspn (salt, "$"), SALT_LEN_MAX);
365
365
  key_len = strlen (key);
366
366
 
367
- if ((key - (char *) 0) % __alignof__ (uint32_t) != 0)
367
+ if ((uintptr_t)key % __alignof__ (uint32_t) != 0)
368
368
  {
369
369
  char *tmp = (char *) alloca (key_len + __alignof__ (uint32_t));
370
370
  key = copied_key = (char *)
371
371
  memcpy (tmp + __alignof__ (uint32_t)
372
- - (tmp - (char *) 0) % __alignof__ (uint32_t),
372
+ - (uintptr_t)tmp % __alignof__ (uint32_t),
373
373
  key, key_len);
374
374
  }
375
375
 
376
- if ((salt - (char *) 0) % __alignof__ (uint32_t) != 0)
376
+ if ((uintptr_t)salt % __alignof__ (uint32_t) != 0)
377
377
  {
378
378
  char *tmp = (char *) alloca (salt_len + __alignof__ (uint32_t));
379
379
  salt = copied_salt = (char *)
380
380
  memcpy (tmp + __alignof__ (uint32_t)
381
- - (tmp - (char *) 0) % __alignof__ (uint32_t),
381
+ - (uintptr_t)tmp % __alignof__ (uint32_t),
382
382
  salt, salt_len);
383
383
  }
384
384
 
package/src/sha512crypt.c CHANGED
@@ -394,21 +394,21 @@ sha512_crypt_r (const char *key, const char *salt, char *buffer, int buflen)
394
394
  salt_len = MIN (strcspn (salt, "$"), SALT_LEN_MAX);
395
395
  key_len = strlen (key);
396
396
 
397
- if ((key - (char *) 0) % __alignof__ (uint64_t) != 0)
397
+ if ((uintptr_t)key % __alignof__ (uint64_t) != 0)
398
398
  {
399
399
  char *tmp = (char *) alloca (key_len + __alignof__ (uint64_t));
400
400
  key = copied_key = (char *)
401
401
  memcpy (tmp + __alignof__ (uint64_t)
402
- - (tmp - (char *) 0) % __alignof__ (uint64_t),
402
+ - (uintptr_t)tmp % __alignof__ (uint64_t),
403
403
  key, key_len);
404
404
  }
405
405
 
406
- if ((salt - (char *) 0) % __alignof__ (uint64_t) != 0)
406
+ if ((uintptr_t)salt % __alignof__ (uint64_t) != 0)
407
407
  {
408
408
  char *tmp = (char *) alloca (salt_len + __alignof__ (uint64_t));
409
409
  salt = copied_salt = (char *)
410
410
  memcpy (tmp + __alignof__ (uint64_t)
411
- - (tmp - (char *) 0) % __alignof__ (uint64_t),
411
+ - (uintptr_t)tmp % __alignof__ (uint64_t),
412
412
  salt, salt_len);
413
413
  }
414
414
 
package/src/shacrypt.cc CHANGED
@@ -177,5 +177,5 @@ NAN_MODULE_INIT(init) {
177
177
  }
178
178
 
179
179
 
180
- NODE_MODULE(shacrypt, init);
180
+ NAN_MODULE_WORKER_ENABLED(shacrypt, init)
181
181
 
package/test/tests.js CHANGED
@@ -2,9 +2,9 @@
2
2
 
3
3
  /* global it */
4
4
 
5
- var shacrypt = require('../shacrypt');
5
+ const shacrypt = require('../shacrypt');
6
6
 
7
- var tests256 = [
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
- var tests512 = [
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
  });