@alwatr/hash-string 5.2.7 → 5.2.9
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 +12 -0
- package/README.md +9 -9
- package/dist/djb2-hash.d.ts +2 -2
- package/dist/main.cjs +1 -5
- package/dist/main.cjs.map +2 -2
- package/dist/main.d.ts.map +1 -1
- package/dist/main.mjs +1 -7
- package/dist/main.mjs.map +3 -3
- package/package.json +5 -8
- package/src/djb2-hash.test.js +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -3,6 +3,18 @@
|
|
|
3
3
|
All notable changes to this project will be documented in this file.
|
|
4
4
|
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
|
|
5
5
|
|
|
6
|
+
## [5.2.9](https://github.com/Alwatr/nanolib/compare/@alwatr/hash-string@5.2.8...@alwatr/hash-string@5.2.9) (2025-09-13)
|
|
7
|
+
|
|
8
|
+
### 🧹 Miscellaneous Chores
|
|
9
|
+
|
|
10
|
+
* remove package-tracer dependency and related code from fetch package ([96fe4e9](https://github.com/Alwatr/nanolib/commit/96fe4e9552a205f218ceed187c55e4e904a07089))
|
|
11
|
+
|
|
12
|
+
## [5.2.8](https://github.com/Alwatr/nanolib/compare/@alwatr/hash-string@5.2.7...@alwatr/hash-string@5.2.8) (2025-09-09)
|
|
13
|
+
|
|
14
|
+
### 🧹 Miscellaneous Chores
|
|
15
|
+
|
|
16
|
+
* remove trailing newlines from contributing sections in README files ([e8ab1bc](https://github.com/Alwatr/nanolib/commit/e8ab1bc43e0addea5ccd4c897c2cec597cb9e15f))
|
|
17
|
+
|
|
6
18
|
## [5.2.7](https://github.com/Alwatr/nanolib/compare/@alwatr/hash-string@5.2.6...@alwatr/hash-string@5.2.7) (2025-09-06)
|
|
7
19
|
|
|
8
20
|
**Note:** Version bump only for package @alwatr/hash-string
|
package/README.md
CHANGED
|
@@ -17,14 +17,14 @@ npm install @alwatr/hash-string
|
|
|
17
17
|
import {nanoHash} from '@alwatr/hash-string';
|
|
18
18
|
|
|
19
19
|
// Hash a string with a prefix
|
|
20
|
-
nanoHash('test', 'prefix-');
|
|
20
|
+
nanoHash('test', 'prefix-'); // => 'prefix-j26j3d4'
|
|
21
21
|
|
|
22
22
|
// Hash a number
|
|
23
|
-
nanoHash(12345, 'num-');
|
|
23
|
+
nanoHash(12345, 'num-'); // => 'num-8hu3f2l'
|
|
24
24
|
|
|
25
25
|
// Adjust complexity with repeat parameter
|
|
26
|
-
nanoHash('test', 'p-', 1);
|
|
27
|
-
nanoHash('test', 'p-', 5);
|
|
26
|
+
nanoHash('test', 'p-', 1); // => 'p-7ba2n3y' (faster, less complex)
|
|
27
|
+
nanoHash('test', 'p-', 5); // => 'p-3f72h9b' (slower, more complex)
|
|
28
28
|
```
|
|
29
29
|
|
|
30
30
|
## API
|
|
@@ -68,16 +68,16 @@ For security-critical applications, use established cryptographic hash functions
|
|
|
68
68
|
|
|
69
69
|
```typescript
|
|
70
70
|
// Generate a hash for a string
|
|
71
|
-
nanoHash('hello world', 'msg-');
|
|
71
|
+
nanoHash('hello world', 'msg-'); // => 'msg-k7f2h9d'
|
|
72
72
|
|
|
73
73
|
// Generate a hash for a number
|
|
74
|
-
nanoHash(42, 'id-');
|
|
74
|
+
nanoHash(42, 'id-'); // => 'id-p83b2e4'
|
|
75
75
|
|
|
76
76
|
// Same input produces the same output
|
|
77
|
-
nanoHash('test', 'x-') === nanoHash('test', 'x-');
|
|
77
|
+
nanoHash('test', 'x-') === nanoHash('test', 'x-'); // => true
|
|
78
78
|
|
|
79
79
|
// Different inputs produce different outputs
|
|
80
|
-
nanoHash('test1', 'x-') !== nanoHash('test2', 'x-');
|
|
80
|
+
nanoHash('test1', 'x-') !== nanoHash('test2', 'x-'); // => true
|
|
81
81
|
```
|
|
82
82
|
|
|
83
83
|
### Controlling Complexity
|
|
@@ -117,7 +117,7 @@ The package includes the DJB2 hash algorithm, a fast and efficient string hashin
|
|
|
117
117
|
// Generate a numeric hash value
|
|
118
118
|
import {djb2Hash} from '@alwatr/hash-string';
|
|
119
119
|
|
|
120
|
-
const hashValue = djb2Hash(
|
|
120
|
+
const hashValue = djb2Hash('hello world'); // Returns a 32-bit unsigned integer
|
|
121
121
|
```
|
|
122
122
|
|
|
123
123
|
Key features of djb2Hash:
|
package/dist/djb2-hash.d.ts
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* DJB2
|
|
2
|
+
* DJB2 hash algorithm but faster!
|
|
3
3
|
*
|
|
4
4
|
* This implementation is based on Daniel J. Bernstein's popular 'times 33' hash algorithm,
|
|
5
5
|
* commonly known as DJB2. It's known for its simplicity, speed, and good distribution properties
|
|
6
6
|
* for short strings, making it suitable for general purpose hashing needs.
|
|
7
7
|
*
|
|
8
|
-
* Performance
|
|
8
|
+
* Performance improvements in this version:
|
|
9
9
|
* - Uses right-to-left iteration to avoid repeated length lookups
|
|
10
10
|
* - Employs bit shifting operations for faster computation
|
|
11
11
|
* - Final right shift ensures unsigned 32-bit integer output
|
package/dist/main.cjs
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
/* @alwatr/hash-string v5.2.
|
|
1
|
+
/* @alwatr/hash-string v5.2.9 */
|
|
2
2
|
"use strict";
|
|
3
3
|
var __defProp = Object.defineProperty;
|
|
4
4
|
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
@@ -25,7 +25,6 @@ __export(main_exports, {
|
|
|
25
25
|
nanoHash: () => nanoHash
|
|
26
26
|
});
|
|
27
27
|
module.exports = __toCommonJS(main_exports);
|
|
28
|
-
var import_package_tracer = require("@alwatr/package-tracer");
|
|
29
28
|
|
|
30
29
|
// src/djb2-hash.ts
|
|
31
30
|
function djb2Hash(str) {
|
|
@@ -61,9 +60,6 @@ function nanoHash(str, prefix, repeat = 1) {
|
|
|
61
60
|
return nanoHash(result, prefix, repeat - 1);
|
|
62
61
|
}
|
|
63
62
|
}
|
|
64
|
-
|
|
65
|
-
// src/main.ts
|
|
66
|
-
__dev_mode__: import_package_tracer.packageTracer.add("@alwatr/hash-string", "5.2.7");
|
|
67
63
|
// Annotate the CommonJS export names for ESM import in node:
|
|
68
64
|
0 && (module.exports = {
|
|
69
65
|
djb2Hash,
|
package/dist/main.cjs.map
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../src/main.ts", "../src/djb2-hash.ts", "../src/nano-hash.ts"],
|
|
4
|
-
"sourcesContent": ["
|
|
5
|
-
"mappings": ";;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA
|
|
4
|
+
"sourcesContent": ["export * from './djb2-hash.js';\nexport * from './nano-hash.js';\n", "/**\n * DJB2 hash algorithm but faster!\n *\n * This implementation is based on Daniel J. Bernstein's popular 'times 33' hash algorithm,\n * commonly known as DJB2. It's known for its simplicity, speed, and good distribution properties\n * for short strings, making it suitable for general purpose hashing needs.\n *\n * Performance improvements in this version:\n * - Uses right-to-left iteration to avoid repeated length lookups\n * - Employs bit shifting operations for faster computation\n * - Final right shift ensures unsigned 32-bit integer output\n *\n * @param {string} str - The input string to be hashed\n * @returns {number} A 32-bit unsigned integer hash value\n *\n * @example\n * // Returns a numeric hash value\n * const hashValue = djb2Hash(\"hello world\");\n */\nexport function djb2Hash(str: string): number {\n // 5381 is a prime number used as initial value in the DJB2 algorithm\n let hashValue = 5381;\n\n // Reverse loop for better performance - avoids repeated length property lookup\n for (let i = str.length - 1; i >= 0; i--) {\n // Using left shift (*2) and addition instead of multiplication by 33\n // (hash * 33) is equivalent to ((hash << 5) + hash)\n hashValue = ((hashValue << 5) + hashValue) ^ str.charCodeAt(i);\n }\n\n return hashValue >>> 0;\n}\n", "/**\n * Simple hash string for fast hashing (like md5).\n * This function is not very secure and should not be used for security purposes.\n * But it cannot be reversed easily and brute force can take up to years for fast computers.\n *\n * @param str - The string or number to hash\n * @param prefix - A prefix to add to the beginning of the hash result\n * @param repeat - Number of times to repeat the hashing process for increased complexity (default: 3)\n * @returns A hashed string with the specified prefix\n */\nexport function nanoHash(str: string | number, prefix: string, repeat = 1): string {\n if (repeat < 1) {\n throw new Error('The repeat parameter must be greater than or equal to 1');\n }\n\n let hash1 = 0xdeadbeef;\n let hash2 = 0x41c6ce57;\n\n if (typeof str === 'number') {\n str = str.toString();\n }\n\n const len = str.length;\n for (let i = 0; i < len; i++) {\n const char = str.charCodeAt(i);\n hash1 = Math.imul(hash1 ^ char, 2654435761);\n hash2 = Math.imul(hash2 ^ char, 1597334677);\n }\n\n hash1 = Math.imul(hash1 ^ (hash1 >>> 16), 2246822507) ^ Math.imul(hash2 ^ (hash2 >>> 13), 3266489909);\n hash2 = Math.imul(hash2 ^ (hash2 >>> 16), 2246822507) ^ Math.imul(hash1 ^ (hash1 >>> 13), 3266489909);\n\n const result = prefix + (hash1 >>> 0).toString(36) + (hash2 >>> 0).toString(36);\n if (repeat === 1) {\n return result;\n }\n else {\n return nanoHash(result, prefix, repeat - 1);\n }\n}\n"],
|
|
5
|
+
"mappings": ";;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACmBO,SAAS,SAAS,KAAqB;AAE5C,MAAI,YAAY;AAGhB,WAAS,IAAI,IAAI,SAAS,GAAG,KAAK,GAAG,KAAK;AAGxC,iBAAc,aAAa,KAAK,YAAa,IAAI,WAAW,CAAC;AAAA,EAC/D;AAEA,SAAO,cAAc;AACvB;;;ACrBO,SAAS,SAAS,KAAsB,QAAgB,SAAS,GAAW;AACjF,MAAI,SAAS,GAAG;AACd,UAAM,IAAI,MAAM,yDAAyD;AAAA,EAC3E;AAEA,MAAI,QAAQ;AACZ,MAAI,QAAQ;AAEZ,MAAI,OAAO,QAAQ,UAAU;AAC3B,UAAM,IAAI,SAAS;AAAA,EACrB;AAEA,QAAM,MAAM,IAAI;AAChB,WAAS,IAAI,GAAG,IAAI,KAAK,KAAK;AAC5B,UAAM,OAAO,IAAI,WAAW,CAAC;AAC7B,YAAQ,KAAK,KAAK,QAAQ,MAAM,UAAU;AAC1C,YAAQ,KAAK,KAAK,QAAQ,MAAM,UAAU;AAAA,EAC5C;AAEA,UAAQ,KAAK,KAAK,QAAS,UAAU,IAAK,UAAU,IAAI,KAAK,KAAK,QAAS,UAAU,IAAK,UAAU;AACpG,UAAQ,KAAK,KAAK,QAAS,UAAU,IAAK,UAAU,IAAI,KAAK,KAAK,QAAS,UAAU,IAAK,UAAU;AAEpG,QAAM,SAAS,UAAU,UAAU,GAAG,SAAS,EAAE,KAAK,UAAU,GAAG,SAAS,EAAE;AAC9E,MAAI,WAAW,GAAG;AAChB,WAAO;AAAA,EACT,OACK;AACH,WAAO,SAAS,QAAQ,QAAQ,SAAS,CAAC;AAAA,EAC5C;AACF;",
|
|
6
6
|
"names": []
|
|
7
7
|
}
|
package/dist/main.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"main.d.ts","sourceRoot":"","sources":["../src/main.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"main.d.ts","sourceRoot":"","sources":["../src/main.ts"],"names":[],"mappings":"AAAA,cAAc,gBAAgB,CAAC;AAC/B,cAAc,gBAAgB,CAAC"}
|
package/dist/main.mjs
CHANGED
|
@@ -1,7 +1,4 @@
|
|
|
1
|
-
/* @alwatr/hash-string v5.2.
|
|
2
|
-
|
|
3
|
-
// src/main.ts
|
|
4
|
-
import { packageTracer } from "@alwatr/package-tracer";
|
|
1
|
+
/* @alwatr/hash-string v5.2.9 */
|
|
5
2
|
|
|
6
3
|
// src/djb2-hash.ts
|
|
7
4
|
function djb2Hash(str) {
|
|
@@ -37,9 +34,6 @@ function nanoHash(str, prefix, repeat = 1) {
|
|
|
37
34
|
return nanoHash(result, prefix, repeat - 1);
|
|
38
35
|
}
|
|
39
36
|
}
|
|
40
|
-
|
|
41
|
-
// src/main.ts
|
|
42
|
-
__dev_mode__: packageTracer.add("@alwatr/hash-string", "5.2.7");
|
|
43
37
|
export {
|
|
44
38
|
djb2Hash,
|
|
45
39
|
nanoHash
|
package/dist/main.mjs.map
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
|
-
"sources": ["../src/
|
|
4
|
-
"sourcesContent": ["
|
|
5
|
-
"mappings": ";;;
|
|
3
|
+
"sources": ["../src/djb2-hash.ts", "../src/nano-hash.ts"],
|
|
4
|
+
"sourcesContent": ["/**\n * DJB2 hash algorithm but faster!\n *\n * This implementation is based on Daniel J. Bernstein's popular 'times 33' hash algorithm,\n * commonly known as DJB2. It's known for its simplicity, speed, and good distribution properties\n * for short strings, making it suitable for general purpose hashing needs.\n *\n * Performance improvements in this version:\n * - Uses right-to-left iteration to avoid repeated length lookups\n * - Employs bit shifting operations for faster computation\n * - Final right shift ensures unsigned 32-bit integer output\n *\n * @param {string} str - The input string to be hashed\n * @returns {number} A 32-bit unsigned integer hash value\n *\n * @example\n * // Returns a numeric hash value\n * const hashValue = djb2Hash(\"hello world\");\n */\nexport function djb2Hash(str: string): number {\n // 5381 is a prime number used as initial value in the DJB2 algorithm\n let hashValue = 5381;\n\n // Reverse loop for better performance - avoids repeated length property lookup\n for (let i = str.length - 1; i >= 0; i--) {\n // Using left shift (*2) and addition instead of multiplication by 33\n // (hash * 33) is equivalent to ((hash << 5) + hash)\n hashValue = ((hashValue << 5) + hashValue) ^ str.charCodeAt(i);\n }\n\n return hashValue >>> 0;\n}\n", "/**\n * Simple hash string for fast hashing (like md5).\n * This function is not very secure and should not be used for security purposes.\n * But it cannot be reversed easily and brute force can take up to years for fast computers.\n *\n * @param str - The string or number to hash\n * @param prefix - A prefix to add to the beginning of the hash result\n * @param repeat - Number of times to repeat the hashing process for increased complexity (default: 3)\n * @returns A hashed string with the specified prefix\n */\nexport function nanoHash(str: string | number, prefix: string, repeat = 1): string {\n if (repeat < 1) {\n throw new Error('The repeat parameter must be greater than or equal to 1');\n }\n\n let hash1 = 0xdeadbeef;\n let hash2 = 0x41c6ce57;\n\n if (typeof str === 'number') {\n str = str.toString();\n }\n\n const len = str.length;\n for (let i = 0; i < len; i++) {\n const char = str.charCodeAt(i);\n hash1 = Math.imul(hash1 ^ char, 2654435761);\n hash2 = Math.imul(hash2 ^ char, 1597334677);\n }\n\n hash1 = Math.imul(hash1 ^ (hash1 >>> 16), 2246822507) ^ Math.imul(hash2 ^ (hash2 >>> 13), 3266489909);\n hash2 = Math.imul(hash2 ^ (hash2 >>> 16), 2246822507) ^ Math.imul(hash1 ^ (hash1 >>> 13), 3266489909);\n\n const result = prefix + (hash1 >>> 0).toString(36) + (hash2 >>> 0).toString(36);\n if (repeat === 1) {\n return result;\n }\n else {\n return nanoHash(result, prefix, repeat - 1);\n }\n}\n"],
|
|
5
|
+
"mappings": ";;;AAmBO,SAAS,SAAS,KAAqB;AAE5C,MAAI,YAAY;AAGhB,WAAS,IAAI,IAAI,SAAS,GAAG,KAAK,GAAG,KAAK;AAGxC,iBAAc,aAAa,KAAK,YAAa,IAAI,WAAW,CAAC;AAAA,EAC/D;AAEA,SAAO,cAAc;AACvB;;;ACrBO,SAAS,SAAS,KAAsB,QAAgB,SAAS,GAAW;AACjF,MAAI,SAAS,GAAG;AACd,UAAM,IAAI,MAAM,yDAAyD;AAAA,EAC3E;AAEA,MAAI,QAAQ;AACZ,MAAI,QAAQ;AAEZ,MAAI,OAAO,QAAQ,UAAU;AAC3B,UAAM,IAAI,SAAS;AAAA,EACrB;AAEA,QAAM,MAAM,IAAI;AAChB,WAAS,IAAI,GAAG,IAAI,KAAK,KAAK;AAC5B,UAAM,OAAO,IAAI,WAAW,CAAC;AAC7B,YAAQ,KAAK,KAAK,QAAQ,MAAM,UAAU;AAC1C,YAAQ,KAAK,KAAK,QAAQ,MAAM,UAAU;AAAA,EAC5C;AAEA,UAAQ,KAAK,KAAK,QAAS,UAAU,IAAK,UAAU,IAAI,KAAK,KAAK,QAAS,UAAU,IAAK,UAAU;AACpG,UAAQ,KAAK,KAAK,QAAS,UAAU,IAAK,UAAU,IAAI,KAAK,KAAK,QAAS,UAAU,IAAK,UAAU;AAEpG,QAAM,SAAS,UAAU,UAAU,GAAG,SAAS,EAAE,KAAK,UAAU,GAAG,SAAS,EAAE;AAC9E,MAAI,WAAW,GAAG;AAChB,WAAO;AAAA,EACT,OACK;AACH,WAAO,SAAS,QAAQ,QAAQ,SAAS,CAAC;AAAA,EAC5C;AACF;",
|
|
6
6
|
"names": []
|
|
7
7
|
}
|
package/package.json
CHANGED
|
@@ -1,16 +1,13 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@alwatr/hash-string",
|
|
3
3
|
"description": "A simple utility to generate a hash string.",
|
|
4
|
-
"version": "5.2.
|
|
4
|
+
"version": "5.2.9",
|
|
5
5
|
"author": "S. Ali Mihandoost <ali.mihandoost@gmail.com>",
|
|
6
6
|
"bugs": "https://github.com/Alwatr/nanolib/issues",
|
|
7
|
-
"dependencies": {
|
|
8
|
-
"@alwatr/package-tracer": "5.5.9"
|
|
9
|
-
},
|
|
10
7
|
"devDependencies": {
|
|
11
|
-
"@alwatr/nano-build": "6.1.
|
|
12
|
-
"@alwatr/prettier-config": "5.0.
|
|
13
|
-
"@alwatr/tsconfig-base": "6.0.
|
|
8
|
+
"@alwatr/nano-build": "6.1.1",
|
|
9
|
+
"@alwatr/prettier-config": "5.0.3",
|
|
10
|
+
"@alwatr/tsconfig-base": "6.0.1",
|
|
14
11
|
"jest": "^30.1.3",
|
|
15
12
|
"typescript": "^5.9.2"
|
|
16
13
|
},
|
|
@@ -75,5 +72,5 @@
|
|
|
75
72
|
},
|
|
76
73
|
"type": "module",
|
|
77
74
|
"types": "./dist/main.d.ts",
|
|
78
|
-
"gitHead": "
|
|
75
|
+
"gitHead": "0555c6f5ac11e7a911deee9bc90cfa44b6fe0b48"
|
|
79
76
|
}
|
package/src/djb2-hash.test.js
CHANGED
|
@@ -52,7 +52,7 @@ describe('djb2Hash', () => {
|
|
|
52
52
|
|
|
53
53
|
for (const input of inputs) {
|
|
54
54
|
const result = djb2Hash(input);
|
|
55
|
-
expect(result).toBeLessThanOrEqual(
|
|
55
|
+
expect(result).toBeLessThanOrEqual(0xffffffff); // Max 32-bit unsigned int
|
|
56
56
|
expect(result).toBeGreaterThanOrEqual(0);
|
|
57
57
|
}
|
|
58
58
|
});
|