@quicore/hash 1.0.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/.babelrc +9 -0
- package/LICENSE +21 -0
- package/README.md +2 -0
- package/dist/index.js +166 -0
- package/package.json +32 -0
package/.babelrc
ADDED
package/LICENSE
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
MIT License
|
2
|
+
|
3
|
+
Copyright (c) 2025 quicore
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
13
|
+
copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
package/dist/index.js
ADDED
@@ -0,0 +1,166 @@
|
|
1
|
+
"use strict";
|
2
|
+
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
4
|
+
value: true
|
5
|
+
});
|
6
|
+
exports.GenerateHash = void 0;
|
7
|
+
var _crypto = _interopRequireDefault(require("crypto"));
|
8
|
+
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
|
9
|
+
/**
|
10
|
+
* Provides a faster and unified interface to commonly used cryptographic hash functions.
|
11
|
+
* Includes support for SHA families, BLAKE2, PBKDF2, RIPEMD, and more.
|
12
|
+
* Ideal for generating IDs, digital fingerprints, and secure hashes in Node.js applications.
|
13
|
+
*/
|
14
|
+
class GenerateHash {
|
15
|
+
/** Base62 character set used for compact encoding */
|
16
|
+
static BASE62_CHARS = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
|
17
|
+
|
18
|
+
/** Default length for generated Base62-encoded IDs */
|
19
|
+
static DEFAULT_ID_LENGTH = 22;
|
20
|
+
|
21
|
+
/** Minimum allowed length for Base62-encoded IDs */
|
22
|
+
static MIN_ID_LENGTH = 10;
|
23
|
+
|
24
|
+
/** Digest size (in bytes) for BLAKE2b hashes used in compact ID generation */
|
25
|
+
static BLAKE2B_DIGEST_SIZE = 20;
|
26
|
+
|
27
|
+
/**
|
28
|
+
* Converts a binary buffer to a Base62-encoded string.
|
29
|
+
*
|
30
|
+
* @param {Buffer} buffer - The binary data to encode.
|
31
|
+
* @param {number} [length=GenerateHash.DEFAULT_ID_LENGTH] - Desired output length. Must be ≥ MIN_ID_LENGTH.
|
32
|
+
* @returns {string} Base62-encoded string.
|
33
|
+
* @throws {Error} If the requested length is below the minimum threshold.
|
34
|
+
*/
|
35
|
+
static toBase62(buffer, length = this.DEFAULT_ID_LENGTH) {
|
36
|
+
if (length < this.MIN_ID_LENGTH) {
|
37
|
+
throw new Error(`ID length must be at least ${this.MIN_ID_LENGTH} characters`);
|
38
|
+
}
|
39
|
+
let num = BigInt('0x' + buffer.toString('hex'));
|
40
|
+
let result = '';
|
41
|
+
const base = BigInt(this.BASE62_CHARS.length);
|
42
|
+
do {
|
43
|
+
const remainder = Number(num % base);
|
44
|
+
result = this.BASE62_CHARS[remainder] + result;
|
45
|
+
num = num / base;
|
46
|
+
} while (num > 0n);
|
47
|
+
return result.length > length ? result.substring(0, length) : result.padStart(length, '0');
|
48
|
+
}
|
49
|
+
|
50
|
+
/**
|
51
|
+
* Generates a BLAKE2b hash from the input string.
|
52
|
+
*
|
53
|
+
* @param {string | Buffer} input - The input data to hash.
|
54
|
+
* @param {number} [digestSize=GenerateHash.BLAKE2B_DIGEST_SIZE] - Length of the digest in bytes.
|
55
|
+
* @returns {Buffer} Hash output as a buffer.
|
56
|
+
*/
|
57
|
+
static blake2b(input, digestSize = this.BLAKE2B_DIGEST_SIZE) {
|
58
|
+
return _crypto.default.createHash('blake2b', {
|
59
|
+
digestLength: digestSize
|
60
|
+
}).update(input).digest();
|
61
|
+
}
|
62
|
+
|
63
|
+
/**
|
64
|
+
* Generates a compact, MongoDB-friendly hash string using BLAKE2b and Base62 encoding.
|
65
|
+
*
|
66
|
+
* @param {string} input - Input string to hash.
|
67
|
+
* @param {number} [length=GenerateHash.DEFAULT_ID_LENGTH] - Desired output length of the encoded hash.
|
68
|
+
* @returns {string} Base62-encoded BLAKE2b hash string.
|
69
|
+
*/
|
70
|
+
static compactHash(input, length = this.DEFAULT_ID_LENGTH) {
|
71
|
+
const hash = this.blake2b(input);
|
72
|
+
return this.toBase62(hash, length);
|
73
|
+
}
|
74
|
+
|
75
|
+
/**
|
76
|
+
* Generates a SHA-256 hash of the input.
|
77
|
+
*
|
78
|
+
* @param {string} input - The input string to hash.
|
79
|
+
* @returns {string} Hexadecimal representation of the SHA-256 hash.
|
80
|
+
*/
|
81
|
+
static SHA256(input) {
|
82
|
+
return _crypto.default.createHash('sha256').update(input).digest('hex');
|
83
|
+
}
|
84
|
+
|
85
|
+
/**
|
86
|
+
* Generates an MD5 hash of the input.
|
87
|
+
*
|
88
|
+
* @deprecated MD5 is not secure for cryptographic or security-sensitive operations.
|
89
|
+
* @param {string} input - The input string to hash.
|
90
|
+
* @returns {string} Hexadecimal representation of the MD5 hash.
|
91
|
+
*/
|
92
|
+
static MD5(input) {
|
93
|
+
return _crypto.default.createHash('md5').update(input).digest('hex');
|
94
|
+
}
|
95
|
+
|
96
|
+
/**
|
97
|
+
* Generates a SHA-1 hash of the input.
|
98
|
+
*
|
99
|
+
* @deprecated SHA-1 is not secure for cryptographic or security-sensitive operations.
|
100
|
+
* @param {string} input - The input string to hash.
|
101
|
+
* @returns {string} Hexadecimal representation of the SHA-1 hash.
|
102
|
+
*/
|
103
|
+
static SHA1(input) {
|
104
|
+
return _crypto.default.createHash('sha1').update(input).digest('hex');
|
105
|
+
}
|
106
|
+
|
107
|
+
/**
|
108
|
+
* Generates a SHA3-256 hash of the input.
|
109
|
+
*
|
110
|
+
* @param {string} input - The input string to hash.
|
111
|
+
* @returns {string} Hexadecimal representation of the SHA3-256 hash.
|
112
|
+
*/
|
113
|
+
static SHA3_256(input) {
|
114
|
+
return _crypto.default.createHash('sha3-256').update(input).digest('hex');
|
115
|
+
}
|
116
|
+
|
117
|
+
/**
|
118
|
+
* Generates a PBKDF2 hash using SHA-512.
|
119
|
+
*
|
120
|
+
* @param {string} password - The password to hash.
|
121
|
+
* @param {string} salt - The salt to use in the hashing process.
|
122
|
+
* @returns {string} Hexadecimal representation of the derived key.
|
123
|
+
*/
|
124
|
+
static PBKDF2(password, salt) {
|
125
|
+
const iterations = 100000;
|
126
|
+
const keyLength = 64;
|
127
|
+
return _crypto.default.pbkdf2Sync(password, salt, iterations, keyLength, 'sha512').toString('hex');
|
128
|
+
}
|
129
|
+
|
130
|
+
/**
|
131
|
+
* Generates a SHA-512 hash.
|
132
|
+
* @param {string} input - Input string to hash.
|
133
|
+
* @returns {string} Hexadecimal SHA-512 hash.
|
134
|
+
*/
|
135
|
+
static SHA512(input) {
|
136
|
+
return _crypto.default.createHash('sha512').update(input).digest('hex');
|
137
|
+
}
|
138
|
+
|
139
|
+
/**
|
140
|
+
* Generates a SHA3-512 hash.
|
141
|
+
* @param {string} input - Input string to hash.
|
142
|
+
* @returns {string} Hexadecimal SHA3-512 hash.
|
143
|
+
*/
|
144
|
+
static SHA3_512(input) {
|
145
|
+
return _crypto.default.createHash('sha3-512').update(input).digest('hex');
|
146
|
+
}
|
147
|
+
|
148
|
+
/**
|
149
|
+
* Generates a RIPEMD-160 hash.
|
150
|
+
* @param {string} input - Input string to hash.
|
151
|
+
* @returns {string} Hexadecimal RIPEMD-160 hash.
|
152
|
+
*/
|
153
|
+
static RIPEMD160(input) {
|
154
|
+
return _crypto.default.createHash('ripemd160').update(input).digest('hex');
|
155
|
+
}
|
156
|
+
|
157
|
+
/**
|
158
|
+
* Generates a BLAKE2s256 hash.
|
159
|
+
* @param {string} input - Input string to hash.
|
160
|
+
* @returns {string} Hexadecimal BLAKE2s256 hash.
|
161
|
+
*/
|
162
|
+
static BLAKE2s256(input) {
|
163
|
+
return _crypto.default.createHash('blake2s256').update(input).digest('hex');
|
164
|
+
}
|
165
|
+
}
|
166
|
+
exports.GenerateHash = GenerateHash;
|
package/package.json
ADDED
@@ -0,0 +1,32 @@
|
|
1
|
+
{
|
2
|
+
"name": "@quicore/hash",
|
3
|
+
"version": "1.0.0",
|
4
|
+
"description": "A lightweight utility for fast access to the most commonly used hashing algorithms including SHA, Blake2, and PBKDF2",
|
5
|
+
"main": "dist/index.js",
|
6
|
+
"scripts": {
|
7
|
+
"build": "babel src -d dist"
|
8
|
+
},
|
9
|
+
"keywords": [
|
10
|
+
"hash",
|
11
|
+
"crypto",
|
12
|
+
"blake2",
|
13
|
+
"sha256",
|
14
|
+
"sha3",
|
15
|
+
"pbkdf2",
|
16
|
+
"ripemd160",
|
17
|
+
"hashing",
|
18
|
+
"base62",
|
19
|
+
"compact-hash",
|
20
|
+
"nodejs"
|
21
|
+
],
|
22
|
+
"author": "quicore",
|
23
|
+
"license": "MIT",
|
24
|
+
"devDependencies": {
|
25
|
+
"@babel/core": "^7.26.10",
|
26
|
+
"@babel/cli": "^7.27.0",
|
27
|
+
"@babel/preset-env": "^7.26.9"
|
28
|
+
},
|
29
|
+
"engines": {
|
30
|
+
"node": ">=18.0.0"
|
31
|
+
}
|
32
|
+
}
|