@vlasky/shacrypt 0.2.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/CLAUDE.md +56 -0
- package/Makefile +1 -1
- package/README.md +66 -9
- package/package.json +7 -6
- package/shacrypt.js +45 -11
- package/src/sha256crypt.c +1 -1
- package/src/sha512crypt.c +1 -1
- package/src/shacrypt.cc +77 -4
- package/test/tests.js +96 -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/CLAUDE.md
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
# CLAUDE.md
|
|
2
|
+
|
|
3
|
+
This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository.
|
|
4
|
+
|
|
5
|
+
## Project Overview
|
|
6
|
+
|
|
7
|
+
shacrypt is a Node.js native addon that provides cross-platform support for SHA-256 crypt and SHA-512 crypt password hashing. It wraps around Ulrich Drepper's C implementation for high performance cryptographic operations.
|
|
8
|
+
|
|
9
|
+
## Architecture
|
|
10
|
+
|
|
11
|
+
- **Native C++ addon**: Built using node-gyp and NAN (Native Abstractions for Node.js)
|
|
12
|
+
- **Core C implementations**: `sha256crypt.c` and `sha512crypt.c` contain the actual cryptographic algorithms
|
|
13
|
+
- **Node.js wrapper**: `shacrypt.cc` provides the V8/Node.js bindings with both sync and async interfaces
|
|
14
|
+
- **JavaScript API**: `shacrypt.js` provides the public API with input validation and parameter handling
|
|
15
|
+
- **Dual execution modes**: Synchronous calls block the event loop, asynchronous calls use libuv thread pool
|
|
16
|
+
|
|
17
|
+
## Key Files
|
|
18
|
+
|
|
19
|
+
- `src/shacrypt.cc` - Main C++ addon code with NAN bindings
|
|
20
|
+
- `src/sha256crypt.c` and `src/sha512crypt.c` - Core cryptographic implementations
|
|
21
|
+
- `shacrypt.js` - JavaScript API wrapper with validation logic
|
|
22
|
+
- `binding.gyp` - Build configuration for the native addon
|
|
23
|
+
- `test/tests.js` - Comprehensive test vectors for both SHA-256 and SHA-512
|
|
24
|
+
|
|
25
|
+
## Development Commands
|
|
26
|
+
|
|
27
|
+
### Build the native addon
|
|
28
|
+
```bash
|
|
29
|
+
npm install
|
|
30
|
+
# or
|
|
31
|
+
make
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
### Run tests
|
|
35
|
+
```bash
|
|
36
|
+
make test
|
|
37
|
+
# or
|
|
38
|
+
npm test
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
The tests use Mocha with test vectors that validate against known good SHA crypt implementations.
|
|
42
|
+
|
|
43
|
+
### Clean build artifacts
|
|
44
|
+
```bash
|
|
45
|
+
make clean
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
## API Design
|
|
49
|
+
|
|
50
|
+
Both `sha256crypt()` and `sha512crypt()` support:
|
|
51
|
+
- Synchronous and asynchronous execution (callback as last parameter)
|
|
52
|
+
- Automatic salt generation if not provided
|
|
53
|
+
- Custom round counts (defaults to 5000, minimum 1000)
|
|
54
|
+
- Salt string validation and formatting
|
|
55
|
+
|
|
56
|
+
The JavaScript wrapper handles parameter validation and salt formatting before calling the native C++ functions.
|
package/Makefile
CHANGED
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": [
|
|
@@ -9,17 +9,18 @@
|
|
|
9
9
|
],
|
|
10
10
|
"main": "shacrypt",
|
|
11
11
|
"scripts": {
|
|
12
|
+
"install": "node-gyp rebuild",
|
|
12
13
|
"test": "make test"
|
|
13
14
|
},
|
|
14
15
|
"dependencies": {
|
|
15
|
-
"nan": "^2.
|
|
16
|
+
"nan": "^2.22.2"
|
|
16
17
|
},
|
|
17
18
|
"devDependencies": {
|
|
18
|
-
"mocha": "
|
|
19
|
-
"chai": "
|
|
19
|
+
"mocha": "^10.8.2",
|
|
20
|
+
"chai": "^4.5.0"
|
|
20
21
|
},
|
|
21
22
|
"engines": {
|
|
22
|
-
"node": ">= 0.
|
|
23
|
+
"node": ">= 16.0.0"
|
|
23
24
|
},
|
|
24
25
|
"bugs": {
|
|
25
26
|
"url": "https://github.com/vlasky/shacrypt/issues"
|
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/src/sha256crypt.c
CHANGED
|
@@ -449,7 +449,7 @@ sha256_crypt_r (const char *key, const char *salt, char *buffer, int buflen)
|
|
|
449
449
|
sha256_init_ctx (&alt_ctx);
|
|
450
450
|
|
|
451
451
|
/* For every character in the password add the entire password. */
|
|
452
|
-
for (cnt = 0; cnt < 16 + alt_result[0]; ++cnt)
|
|
452
|
+
for (cnt = 0; cnt < (size_t)(16 + alt_result[0]); ++cnt)
|
|
453
453
|
sha256_process_bytes (salt, salt_len, &alt_ctx);
|
|
454
454
|
|
|
455
455
|
/* Finish the digest. */
|
package/src/sha512crypt.c
CHANGED
|
@@ -479,7 +479,7 @@ sha512_crypt_r (const char *key, const char *salt, char *buffer, int buflen)
|
|
|
479
479
|
sha512_init_ctx (&alt_ctx);
|
|
480
480
|
|
|
481
481
|
/* For every character in the password add the entire password. */
|
|
482
|
-
for (cnt = 0; cnt < 16 + alt_result[0]; ++cnt)
|
|
482
|
+
for (cnt = 0; cnt < (size_t)(16 + alt_result[0]); ++cnt)
|
|
483
483
|
sha512_process_bytes (salt, salt_len, &alt_ctx);
|
|
484
484
|
|
|
485
485
|
/* Finish the digest. */
|
package/src/shacrypt.cc
CHANGED
|
@@ -19,17 +19,49 @@ using Nan::Null;
|
|
|
19
19
|
using Nan::To;
|
|
20
20
|
|
|
21
21
|
NAN_METHOD(sha256crypt) {
|
|
22
|
+
if (info.Length() < 2) {
|
|
23
|
+
Nan::ThrowError("Wrong number of arguments");
|
|
24
|
+
return;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
if (!info[0]->IsString() || !info[1]->IsString()) {
|
|
28
|
+
Nan::ThrowTypeError("Arguments must be strings");
|
|
29
|
+
return;
|
|
30
|
+
}
|
|
31
|
+
|
|
22
32
|
Nan::Utf8String key(info[0]);
|
|
23
33
|
Nan::Utf8String salt(info[1]);
|
|
24
34
|
|
|
25
|
-
|
|
35
|
+
const char* result = sha256_crypt(*key, *salt);
|
|
36
|
+
if (!result) {
|
|
37
|
+
Nan::ThrowError("sha256_crypt failed");
|
|
38
|
+
return;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
info.GetReturnValue().Set(Nan::New(result).ToLocalChecked());
|
|
26
42
|
}
|
|
27
43
|
|
|
28
44
|
NAN_METHOD(sha512crypt) {
|
|
45
|
+
if (info.Length() < 2) {
|
|
46
|
+
Nan::ThrowError("Wrong number of arguments");
|
|
47
|
+
return;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
if (!info[0]->IsString() || !info[1]->IsString()) {
|
|
51
|
+
Nan::ThrowTypeError("Arguments must be strings");
|
|
52
|
+
return;
|
|
53
|
+
}
|
|
54
|
+
|
|
29
55
|
Nan::Utf8String key(info[0]);
|
|
30
56
|
Nan::Utf8String salt(info[1]);
|
|
31
57
|
|
|
32
|
-
|
|
58
|
+
const char* result = sha512_crypt(*key, *salt);
|
|
59
|
+
if (!result) {
|
|
60
|
+
Nan::ThrowError("sha512_crypt failed");
|
|
61
|
+
return;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
info.GetReturnValue().Set(Nan::New(result).ToLocalChecked());
|
|
33
65
|
}
|
|
34
66
|
|
|
35
67
|
|
|
@@ -40,7 +72,12 @@ class SHA256CryptWorker : public AsyncWorker {
|
|
|
40
72
|
}
|
|
41
73
|
|
|
42
74
|
void Execute() {
|
|
43
|
-
|
|
75
|
+
const char* crypt_result = sha256_crypt(key.c_str(), salt.c_str());
|
|
76
|
+
if (!crypt_result) {
|
|
77
|
+
SetErrorMessage("sha256_crypt failed");
|
|
78
|
+
return;
|
|
79
|
+
}
|
|
80
|
+
result = crypt_result;
|
|
44
81
|
}
|
|
45
82
|
|
|
46
83
|
void HandleOKCallback() {
|
|
@@ -61,7 +98,12 @@ class SHA512CryptWorker : public AsyncWorker {
|
|
|
61
98
|
}
|
|
62
99
|
|
|
63
100
|
void Execute() {
|
|
64
|
-
|
|
101
|
+
const char* crypt_result = sha512_crypt(key.c_str(), salt.c_str());
|
|
102
|
+
if (!crypt_result) {
|
|
103
|
+
SetErrorMessage("sha512_crypt failed");
|
|
104
|
+
return;
|
|
105
|
+
}
|
|
106
|
+
result = crypt_result;
|
|
65
107
|
}
|
|
66
108
|
|
|
67
109
|
void HandleOKCallback() {
|
|
@@ -76,6 +118,21 @@ class SHA512CryptWorker : public AsyncWorker {
|
|
|
76
118
|
};
|
|
77
119
|
|
|
78
120
|
NAN_METHOD(sha256cryptasync) {
|
|
121
|
+
if (info.Length() < 3) {
|
|
122
|
+
Nan::ThrowError("Wrong number of arguments");
|
|
123
|
+
return;
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
if (!info[0]->IsString() || !info[1]->IsString()) {
|
|
127
|
+
Nan::ThrowTypeError("First two arguments must be strings");
|
|
128
|
+
return;
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
if (!info[2]->IsFunction()) {
|
|
132
|
+
Nan::ThrowTypeError("Third argument must be a function");
|
|
133
|
+
return;
|
|
134
|
+
}
|
|
135
|
+
|
|
79
136
|
Nan::Utf8String key(info[0]);
|
|
80
137
|
Nan::Utf8String salt(info[1]);
|
|
81
138
|
|
|
@@ -84,6 +141,21 @@ NAN_METHOD(sha256cryptasync) {
|
|
|
84
141
|
}
|
|
85
142
|
|
|
86
143
|
NAN_METHOD(sha512cryptasync) {
|
|
144
|
+
if (info.Length() < 3) {
|
|
145
|
+
Nan::ThrowError("Wrong number of arguments");
|
|
146
|
+
return;
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
if (!info[0]->IsString() || !info[1]->IsString()) {
|
|
150
|
+
Nan::ThrowTypeError("First two arguments must be strings");
|
|
151
|
+
return;
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
if (!info[2]->IsFunction()) {
|
|
155
|
+
Nan::ThrowTypeError("Third argument must be a function");
|
|
156
|
+
return;
|
|
157
|
+
}
|
|
158
|
+
|
|
87
159
|
Nan::Utf8String key(info[0]);
|
|
88
160
|
Nan::Utf8String salt(info[1]);
|
|
89
161
|
|
|
@@ -104,5 +176,6 @@ NAN_MODULE_INIT(init) {
|
|
|
104
176
|
Nan::GetFunction(Nan::New<FunctionTemplate>(sha512cryptasync)).ToLocalChecked());
|
|
105
177
|
}
|
|
106
178
|
|
|
179
|
+
|
|
107
180
|
NODE_MODULE(shacrypt, init);
|
|
108
181
|
|
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
|
],
|
|
@@ -97,3 +97,96 @@ tests512.forEach(function(test) {
|
|
|
97
97
|
});
|
|
98
98
|
});
|
|
99
99
|
});
|
|
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
|
+
|
|
117
|
+
describe('Input validation', function() {
|
|
118
|
+
it('should throw error for non-string password in sha256crypt', function() {
|
|
119
|
+
expect(function() {
|
|
120
|
+
shacrypt.sha256crypt(123, 'salt');
|
|
121
|
+
}).to.throw('password must be a String');
|
|
122
|
+
});
|
|
123
|
+
|
|
124
|
+
it('should throw error for non-string password in sha512crypt', function() {
|
|
125
|
+
expect(function() {
|
|
126
|
+
shacrypt.sha512crypt(null, 'salt');
|
|
127
|
+
}).to.throw('password must be a String');
|
|
128
|
+
});
|
|
129
|
+
|
|
130
|
+
it('should handle missing password gracefully', function() {
|
|
131
|
+
expect(function() {
|
|
132
|
+
shacrypt.sha256crypt();
|
|
133
|
+
}).to.throw('password must be a String');
|
|
134
|
+
});
|
|
135
|
+
|
|
136
|
+
it('should generate salt when not provided', function() {
|
|
137
|
+
var result = shacrypt.sha256crypt('password');
|
|
138
|
+
result.should.match(/^\$5\$/);
|
|
139
|
+
});
|
|
140
|
+
|
|
141
|
+
it('should handle async callback errors for invalid password', function(done) {
|
|
142
|
+
try {
|
|
143
|
+
shacrypt.sha256crypt(123, 'salt', function(err, result) {
|
|
144
|
+
// This callback should not be called
|
|
145
|
+
done(new Error('Expected synchronous error'));
|
|
146
|
+
});
|
|
147
|
+
} catch (e) {
|
|
148
|
+
e.message.should.equal('password must be a String');
|
|
149
|
+
done();
|
|
150
|
+
}
|
|
151
|
+
});
|
|
152
|
+
|
|
153
|
+
it('should handle async callback errors for invalid password in sha512', function(done) {
|
|
154
|
+
try {
|
|
155
|
+
shacrypt.sha512crypt(undefined, 'salt', function(err, result) {
|
|
156
|
+
// This callback should not be called
|
|
157
|
+
done(new Error('Expected synchronous error'));
|
|
158
|
+
});
|
|
159
|
+
} catch (e) {
|
|
160
|
+
e.message.should.equal('password must be a String');
|
|
161
|
+
done();
|
|
162
|
+
}
|
|
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
|
+
});
|
|
192
|
+
});
|