@alwatr/hash-string 5.2.9 โ 5.2.11
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 +10 -0
- package/dist/main.cjs +1 -14
- package/dist/main.cjs.map +3 -3
- package/dist/main.d.ts +11 -2
- package/dist/main.d.ts.map +1 -1
- package/dist/main.mjs +2 -12
- package/dist/main.mjs.map +3 -3
- package/package.json +3 -3
- package/dist/djb2-hash.d.ts +0 -21
- package/dist/djb2-hash.d.ts.map +0 -1
- package/dist/nano-hash.d.ts +0 -12
- package/dist/nano-hash.d.ts.map +0 -1
- package/src/djb2-hash.test.js +0 -72
- /package/src/{nano-hash.test.js โ main.test.js} +0 -0
package/CHANGELOG.md
CHANGED
|
@@ -3,6 +3,16 @@
|
|
|
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.11](https://github.com/Alwatr/nanolib/compare/@alwatr/hash-string@5.2.10...@alwatr/hash-string@5.2.11) (2025-09-14)
|
|
7
|
+
|
|
8
|
+
### ๐จ Code Refactoring
|
|
9
|
+
|
|
10
|
+
* **hash:** remove djb2Hash implementation and tests ([140a1e1](https://github.com/Alwatr/nanolib/commit/140a1e191bbe864379e2621919f7fbc37f403afa))
|
|
11
|
+
|
|
12
|
+
## [5.2.10](https://github.com/Alwatr/nanolib/compare/@alwatr/hash-string@5.2.9...@alwatr/hash-string@5.2.10) (2025-09-13)
|
|
13
|
+
|
|
14
|
+
**Note:** Version bump only for package @alwatr/hash-string
|
|
15
|
+
|
|
6
16
|
## [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
17
|
|
|
8
18
|
### ๐งน Miscellaneous Chores
|
package/dist/main.cjs
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
/* @alwatr/hash-string v5.2.
|
|
1
|
+
/* @alwatr/hash-string v5.2.11 */
|
|
2
2
|
"use strict";
|
|
3
3
|
var __defProp = Object.defineProperty;
|
|
4
4
|
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
@@ -21,21 +21,9 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
|
|
|
21
21
|
// src/main.ts
|
|
22
22
|
var main_exports = {};
|
|
23
23
|
__export(main_exports, {
|
|
24
|
-
djb2Hash: () => djb2Hash,
|
|
25
24
|
nanoHash: () => nanoHash
|
|
26
25
|
});
|
|
27
26
|
module.exports = __toCommonJS(main_exports);
|
|
28
|
-
|
|
29
|
-
// src/djb2-hash.ts
|
|
30
|
-
function djb2Hash(str) {
|
|
31
|
-
let hashValue = 5381;
|
|
32
|
-
for (let i = str.length - 1; i >= 0; i--) {
|
|
33
|
-
hashValue = (hashValue << 5) + hashValue ^ str.charCodeAt(i);
|
|
34
|
-
}
|
|
35
|
-
return hashValue >>> 0;
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
// src/nano-hash.ts
|
|
39
27
|
function nanoHash(str, prefix, repeat = 1) {
|
|
40
28
|
if (repeat < 1) {
|
|
41
29
|
throw new Error("The repeat parameter must be greater than or equal to 1");
|
|
@@ -62,7 +50,6 @@ function nanoHash(str, prefix, repeat = 1) {
|
|
|
62
50
|
}
|
|
63
51
|
// Annotate the CommonJS export names for ESM import in node:
|
|
64
52
|
0 && (module.exports = {
|
|
65
|
-
djb2Hash,
|
|
66
53
|
nanoHash
|
|
67
54
|
});
|
|
68
55
|
//# sourceMappingURL=main.cjs.map
|
package/dist/main.cjs.map
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
|
-
"sources": ["../src/main.ts"
|
|
4
|
-
"sourcesContent": ["
|
|
5
|
-
"mappings": ";;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;
|
|
3
|
+
"sources": ["../src/main.ts"],
|
|
4
|
+
"sourcesContent": ["/**\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;AAUO,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
CHANGED
|
@@ -1,3 +1,12 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
/**
|
|
2
|
+
* Simple hash string for fast hashing (like md5).
|
|
3
|
+
* This function is not very secure and should not be used for security purposes.
|
|
4
|
+
* But it cannot be reversed easily and brute force can take up to years for fast computers.
|
|
5
|
+
*
|
|
6
|
+
* @param str - The string or number to hash
|
|
7
|
+
* @param prefix - A prefix to add to the beginning of the hash result
|
|
8
|
+
* @param repeat - Number of times to repeat the hashing process for increased complexity (default: 3)
|
|
9
|
+
* @returns A hashed string with the specified prefix
|
|
10
|
+
*/
|
|
11
|
+
export declare function nanoHash(str: string | number, prefix: string, repeat?: number): string;
|
|
3
12
|
//# sourceMappingURL=main.d.ts.map
|
package/dist/main.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"main.d.ts","sourceRoot":"","sources":["../src/main.ts"],"names":[],"mappings":"AAAA,
|
|
1
|
+
{"version":3,"file":"main.d.ts","sourceRoot":"","sources":["../src/main.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AACH,wBAAgB,QAAQ,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,SAAI,GAAG,MAAM,CA6BjF"}
|
package/dist/main.mjs
CHANGED
|
@@ -1,15 +1,6 @@
|
|
|
1
|
-
/* @alwatr/hash-string v5.2.
|
|
1
|
+
/* @alwatr/hash-string v5.2.11 */
|
|
2
2
|
|
|
3
|
-
// src/
|
|
4
|
-
function djb2Hash(str) {
|
|
5
|
-
let hashValue = 5381;
|
|
6
|
-
for (let i = str.length - 1; i >= 0; i--) {
|
|
7
|
-
hashValue = (hashValue << 5) + hashValue ^ str.charCodeAt(i);
|
|
8
|
-
}
|
|
9
|
-
return hashValue >>> 0;
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
// src/nano-hash.ts
|
|
3
|
+
// src/main.ts
|
|
13
4
|
function nanoHash(str, prefix, repeat = 1) {
|
|
14
5
|
if (repeat < 1) {
|
|
15
6
|
throw new Error("The repeat parameter must be greater than or equal to 1");
|
|
@@ -35,7 +26,6 @@ function nanoHash(str, prefix, repeat = 1) {
|
|
|
35
26
|
}
|
|
36
27
|
}
|
|
37
28
|
export {
|
|
38
|
-
djb2Hash,
|
|
39
29
|
nanoHash
|
|
40
30
|
};
|
|
41
31
|
//# sourceMappingURL=main.mjs.map
|
package/dist/main.mjs.map
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
|
-
"sources": ["../src/
|
|
4
|
-
"sourcesContent": ["/**\n *
|
|
5
|
-
"mappings": ";;;
|
|
3
|
+
"sources": ["../src/main.ts"],
|
|
4
|
+
"sourcesContent": ["/**\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": ";;;AAUO,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,11 +1,11 @@
|
|
|
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.11",
|
|
5
5
|
"author": "S. Ali Mihandoost <ali.mihandoost@gmail.com>",
|
|
6
6
|
"bugs": "https://github.com/Alwatr/nanolib/issues",
|
|
7
7
|
"devDependencies": {
|
|
8
|
-
"@alwatr/nano-build": "6.1.
|
|
8
|
+
"@alwatr/nano-build": "6.1.2",
|
|
9
9
|
"@alwatr/prettier-config": "5.0.3",
|
|
10
10
|
"@alwatr/tsconfig-base": "6.0.1",
|
|
11
11
|
"jest": "^30.1.3",
|
|
@@ -72,5 +72,5 @@
|
|
|
72
72
|
},
|
|
73
73
|
"type": "module",
|
|
74
74
|
"types": "./dist/main.d.ts",
|
|
75
|
-
"gitHead": "
|
|
75
|
+
"gitHead": "1d91d3b82939741f42655e6d9f9b4e8d00af4f85"
|
|
76
76
|
}
|
package/dist/djb2-hash.d.ts
DELETED
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* DJB2 hash algorithm but faster!
|
|
3
|
-
*
|
|
4
|
-
* This implementation is based on Daniel J. Bernstein's popular 'times 33' hash algorithm,
|
|
5
|
-
* commonly known as DJB2. It's known for its simplicity, speed, and good distribution properties
|
|
6
|
-
* for short strings, making it suitable for general purpose hashing needs.
|
|
7
|
-
*
|
|
8
|
-
* Performance improvements in this version:
|
|
9
|
-
* - Uses right-to-left iteration to avoid repeated length lookups
|
|
10
|
-
* - Employs bit shifting operations for faster computation
|
|
11
|
-
* - Final right shift ensures unsigned 32-bit integer output
|
|
12
|
-
*
|
|
13
|
-
* @param {string} str - The input string to be hashed
|
|
14
|
-
* @returns {number} A 32-bit unsigned integer hash value
|
|
15
|
-
*
|
|
16
|
-
* @example
|
|
17
|
-
* // Returns a numeric hash value
|
|
18
|
-
* const hashValue = djb2Hash("hello world");
|
|
19
|
-
*/
|
|
20
|
-
export declare function djb2Hash(str: string): number;
|
|
21
|
-
//# sourceMappingURL=djb2-hash.d.ts.map
|
package/dist/djb2-hash.d.ts.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"djb2-hash.d.ts","sourceRoot":"","sources":["../src/djb2-hash.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;GAkBG;AACH,wBAAgB,QAAQ,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,CAY5C"}
|
package/dist/nano-hash.d.ts
DELETED
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Simple hash string for fast hashing (like md5).
|
|
3
|
-
* This function is not very secure and should not be used for security purposes.
|
|
4
|
-
* But it cannot be reversed easily and brute force can take up to years for fast computers.
|
|
5
|
-
*
|
|
6
|
-
* @param str - The string or number to hash
|
|
7
|
-
* @param prefix - A prefix to add to the beginning of the hash result
|
|
8
|
-
* @param repeat - Number of times to repeat the hashing process for increased complexity (default: 3)
|
|
9
|
-
* @returns A hashed string with the specified prefix
|
|
10
|
-
*/
|
|
11
|
-
export declare function nanoHash(str: string | number, prefix: string, repeat?: number): string;
|
|
12
|
-
//# sourceMappingURL=nano-hash.d.ts.map
|
package/dist/nano-hash.d.ts.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"nano-hash.d.ts","sourceRoot":"","sources":["../src/nano-hash.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AACH,wBAAgB,QAAQ,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,SAAI,GAAG,MAAM,CA6BjF"}
|
package/src/djb2-hash.test.js
DELETED
|
@@ -1,72 +0,0 @@
|
|
|
1
|
-
import {djb2Hash} from '@alwatr/hash-string';
|
|
2
|
-
|
|
3
|
-
describe('djb2Hash', () => {
|
|
4
|
-
it('should generate numeric hash values for string inputs', () => {
|
|
5
|
-
const result = djb2Hash('test');
|
|
6
|
-
expect(typeof result).toBe('number');
|
|
7
|
-
expect(Number.isInteger(result)).toBe(true);
|
|
8
|
-
});
|
|
9
|
-
|
|
10
|
-
it('should return different hashes for different inputs', () => {
|
|
11
|
-
const hash1 = djb2Hash('test1');
|
|
12
|
-
const hash2 = djb2Hash('test2');
|
|
13
|
-
expect(hash1).not.toBe(hash2);
|
|
14
|
-
});
|
|
15
|
-
|
|
16
|
-
it('should generate consistent hashes for the same input', () => {
|
|
17
|
-
const input = 'consistencyTest';
|
|
18
|
-
const hash1 = djb2Hash(input);
|
|
19
|
-
const hash2 = djb2Hash(input);
|
|
20
|
-
expect(hash1).toBe(hash2);
|
|
21
|
-
});
|
|
22
|
-
|
|
23
|
-
it('should handle empty strings', () => {
|
|
24
|
-
const result = djb2Hash('');
|
|
25
|
-
expect(typeof result).toBe('number');
|
|
26
|
-
// The hash for an empty string should be the initial value right-shifted by 0
|
|
27
|
-
expect(result).toBe(5381 >>> 0);
|
|
28
|
-
});
|
|
29
|
-
|
|
30
|
-
it('should handle special characters', () => {
|
|
31
|
-
const result = djb2Hash('!@#$%^&*()');
|
|
32
|
-
expect(typeof result).toBe('number');
|
|
33
|
-
expect(Number.isInteger(result)).toBe(true);
|
|
34
|
-
});
|
|
35
|
-
|
|
36
|
-
it('should handle long strings', () => {
|
|
37
|
-
const longString = 'a'.repeat(1000);
|
|
38
|
-
const result = djb2Hash(longString);
|
|
39
|
-
expect(typeof result).toBe('number');
|
|
40
|
-
expect(Number.isInteger(result)).toBe(true);
|
|
41
|
-
});
|
|
42
|
-
|
|
43
|
-
it('should handle Unicode characters', () => {
|
|
44
|
-
const unicodeString = '๐๐๐';
|
|
45
|
-
const result = djb2Hash(unicodeString);
|
|
46
|
-
expect(typeof result).toBe('number');
|
|
47
|
-
expect(Number.isInteger(result)).toBe(true);
|
|
48
|
-
});
|
|
49
|
-
|
|
50
|
-
it('should produce 32-bit unsigned integer outputs', () => {
|
|
51
|
-
const inputs = ['test', 'hello world', 'unicode ๐', 'a'.repeat(1000)];
|
|
52
|
-
|
|
53
|
-
for (const input of inputs) {
|
|
54
|
-
const result = djb2Hash(input);
|
|
55
|
-
expect(result).toBeLessThanOrEqual(0xffffffff); // Max 32-bit unsigned int
|
|
56
|
-
expect(result).toBeGreaterThanOrEqual(0);
|
|
57
|
-
}
|
|
58
|
-
});
|
|
59
|
-
|
|
60
|
-
it('should produce deterministic results', () => {
|
|
61
|
-
// Test with known hash values
|
|
62
|
-
expect(djb2Hash('hello')).toBe(181380007);
|
|
63
|
-
expect(djb2Hash('world')).toBe(164394279);
|
|
64
|
-
expect(djb2Hash('hello world')).toBe(2616892229);
|
|
65
|
-
expect(djb2Hash('')).toBe(5381);
|
|
66
|
-
});
|
|
67
|
-
|
|
68
|
-
it('should handle case sensitivity', () => {
|
|
69
|
-
expect(djb2Hash('Hello')).not.toBe(djb2Hash('hello'));
|
|
70
|
-
expect(djb2Hash('WORLD')).not.toBe(djb2Hash('world'));
|
|
71
|
-
});
|
|
72
|
-
});
|
|
File without changes
|