inslash 1.0.2

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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Inslash
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
@@ -0,0 +1,59 @@
1
+ # inslash
2
+
3
+ A modern, upgradeable, and secure password hashing utility for Node.js.
4
+ Features passport encoding, hash ancestry, salt, pepper, and automatic upgrade support.
5
+
6
+ ## Features
7
+
8
+ - Secure password hashing with salt and pepper
9
+ - Passport encoding (all hash info in one string)
10
+ - Hash ancestry/history for upgrades and audits
11
+ - Automatic upgrade detection and rehashing
12
+ - Async API
13
+
14
+ ## Installation
15
+
16
+ ```sh
17
+ npm install inslash
18
+ ```
19
+
20
+ ## Usage
21
+
22
+ ```js
23
+ const { hash, verify } = require("inslash");
24
+
25
+ const secret = "your-secret-key";
26
+
27
+ // Hash a password
28
+ const result = await hash("myPassword", secret);
29
+
30
+ // Store result.passport in your database
31
+
32
+ // Verify a password
33
+ const verifyResult = await verify("myPassword", result.passport, secret);
34
+
35
+ console.log(verifyResult.valid); // true or false
36
+ ```
37
+
38
+ ## API
39
+
40
+ ### `async hash(value, secret, options?)`
41
+ - `value` (string): The value to hash.
42
+ - `secret` (string): Secret key for HMAC.
43
+ - `options` (object): Optional. Override defaults (`iterations`, `algorithm`, etc).
44
+ - **Returns:** `{ passport, algorithm, iterations, saltLength, hashLength, salt, hash, history }`
45
+
46
+ ### `async verify(value, passport, secret, options?)`
47
+ - `value` (string): Value to verify.
48
+ - `passport` (string): Encoded hash passport.
49
+ - `secret` (string): Secret key for HMAC.
50
+ - `options` (object): Optional. Override defaults.
51
+ - **Returns:** `{ valid, needsUpgrade, upgradedPassport }`
52
+
53
+ ### Environment Variables
54
+
55
+ - `HASH_PEPPER`: Optional. Adds a global pepper to all hashes.
56
+
57
+ ## License
58
+
59
+ MIT
package/cstest.js ADDED
@@ -0,0 +1,49 @@
1
+ const { hash, verify } = require("./script");
2
+
3
+ const SECRET_KEY = "supersecret";
4
+
5
+ // 1. Rainbow Table Attack
6
+ (async () => {
7
+ const a = await hash("password123", SECRET_KEY);
8
+ const b = await hash("password123", SECRET_KEY);
9
+ console.log("Rainbow Table Test:", a.hash !== b.hash ? "PASS" : "FAIL");
10
+
11
+ // 2. Brute Force Attack (timing)
12
+ console.time("Low Iterations");
13
+ await hash("password123", SECRET_KEY, { iterations: 1000 });
14
+ console.timeEnd("Low Iterations");
15
+
16
+ console.time("High Iterations");
17
+ await hash("password123", SECRET_KEY, { iterations: 200_000 });
18
+ console.timeEnd("High Iterations");
19
+
20
+ // 3. Timing Attack (should use timingSafeEqual)
21
+ const v = await verify("password123", a.passport, SECRET_KEY);
22
+ console.log("Timing Safe Equal Test:", v.valid ? "PASS" : "FAIL");
23
+
24
+ // 4. Salt Storage
25
+ console.log("Salt Unique Test:", a.salt !== b.salt ? "PASS" : "FAIL");
26
+
27
+ // 5. Pepper Security
28
+ process.env.HASH_PEPPER = "pepper";
29
+ const withPepper = await hash("password123", SECRET_KEY);
30
+ process.env.HASH_PEPPER = "";
31
+ const vPepper = await verify("password123", withPepper.passport, SECRET_KEY);
32
+ console.log("Pepper Security Test:", vPepper.valid ? "FAIL" : "PASS");
33
+
34
+ // 6. Upgrade Path
35
+ const vUpgrade = await verify("password123", a.passport, SECRET_KEY, { iterations: 200_000 });
36
+ console.log("Upgrade Path Test:", vUpgrade.needsUpgrade ? "PASS" : "FAIL");
37
+
38
+ // 7. Input Validation
39
+ try {
40
+ await hash(null, SECRET_KEY);
41
+ console.log("Null Input Test: FAIL");
42
+ } catch {
43
+ console.log("Null Input Test: PASS");
44
+ }
45
+
46
+ // 8. Collision Resistance
47
+ const c = await hash("passwordABC", SECRET_KEY);
48
+ console.log("Collision Resistance Test:", a.hash !== c.hash ? "PASS" : "FAIL");
49
+ })();
package/package.json ADDED
@@ -0,0 +1,15 @@
1
+ {
2
+ "name": "inslash",
3
+ "version": "1.0.2",
4
+ "main": "index.js",
5
+ "scripts": {
6
+ "test": "echo \"Error: no test specified\" && exit 1"
7
+ },
8
+ "keywords": [],
9
+ "author": "Reshuk Sapkota",
10
+ "license": "MIT",
11
+ "description": "",
12
+ "dependencies": {
13
+ "crypto": "^1.0.1"
14
+ }
15
+ }
package/script.js ADDED
@@ -0,0 +1,124 @@
1
+ const crypto = require("crypto");
2
+
3
+ const DEFAULTS = {
4
+ saltLength: 16,
5
+ hashLength: 32,
6
+ iterations: 100_000,
7
+ algorithm: "sha256"
8
+ };
9
+
10
+ const createSalt = (length) => crypto.randomBytes(length).toString("hex");
11
+
12
+ const hashWithSalt = async (value, salt, secret, options) => {
13
+ const { iterations, hashLength, algorithm } = options;
14
+ let data = value + salt;
15
+ let digest = Buffer.from(data);
16
+
17
+ for (let i = 0; i < iterations; i++) {
18
+ digest = crypto.createHmac(algorithm, secret).update(digest).digest();
19
+ }
20
+
21
+ return digest.toString("hex").slice(0, hashLength);
22
+ };
23
+
24
+ const encodePassport = (meta) => {
25
+ const history = Buffer.from(JSON.stringify(meta.history || [])).toString("base64");
26
+ return [
27
+ "$inslash",
28
+ meta.algorithm,
29
+ meta.iterations,
30
+ meta.saltLength,
31
+ meta.hashLength,
32
+ meta.salt,
33
+ meta.hash,
34
+ history
35
+ ].join("$");
36
+ };
37
+
38
+ const decodePassport = (passport) => {
39
+ const parts = passport.split("$");
40
+ if (parts[1] !== "inslash") throw new Error("Invalid passport format");
41
+ const [ , , algorithm, iterations, saltLength, hashLength, salt, hash, history ] = parts;
42
+ return {
43
+ algorithm,
44
+ iterations: Number(iterations),
45
+ saltLength: Number(saltLength),
46
+ hashLength: Number(hashLength),
47
+ salt,
48
+ hash,
49
+ history: JSON.parse(Buffer.from(history, "base64").toString())
50
+ };
51
+ };
52
+
53
+ const hash = async (value, secret, opts = {}) => {
54
+ if (!secret) throw new Error("Secret key is required");
55
+ if (typeof value !== "string" || !value) throw new Error("Value to hash must be a non-empty string");
56
+ const options = { ...DEFAULTS, ...opts };
57
+ const salt = createSalt(options.saltLength);
58
+ const pepper = process.env.HASH_PEPPER || "";
59
+ const valueWithPepper = value + pepper;
60
+ const hashed = await hashWithSalt(valueWithPepper, salt, secret, options);
61
+
62
+ const meta = {
63
+ algorithm: options.algorithm,
64
+ iterations: options.iterations,
65
+ saltLength: options.saltLength,
66
+ hashLength: options.hashLength,
67
+ salt,
68
+ hash: hashed,
69
+ history: [
70
+ {
71
+ date: new Date().toISOString(),
72
+ algorithm: options.algorithm,
73
+ iterations: options.iterations
74
+ }
75
+ ]
76
+ };
77
+
78
+ return {
79
+ passport: encodePassport(meta),
80
+ ...meta
81
+ };
82
+ };
83
+
84
+ const verify = async (value, passport, secret, opts = {}) => {
85
+ const meta = decodePassport(passport);
86
+ const options = {
87
+ algorithm: meta.algorithm,
88
+ iterations: meta.iterations,
89
+ saltLength: meta.saltLength,
90
+ hashLength: meta.hashLength,
91
+ ...opts
92
+ };
93
+ const pepper = process.env.HASH_PEPPER || "";
94
+ const valueWithPepper = value + pepper;
95
+ const computed = await hashWithSalt(valueWithPepper, meta.salt, secret, options);
96
+ const valid = crypto.timingSafeEqual(Buffer.from(computed), Buffer.from(meta.hash));
97
+ let needsUpgrade = false;
98
+ if (opts.iterations && opts.iterations > meta.iterations) needsUpgrade = true;
99
+ if (opts.algorithm && opts.algorithm !== meta.algorithm) needsUpgrade = true;
100
+ let upgradedPassport = null;
101
+ if (valid && needsUpgrade) {
102
+ const newMeta = { ...meta, ...opts };
103
+ newMeta.history = meta.history.concat([
104
+ {
105
+ date: new Date().toISOString(),
106
+ algorithm: opts.algorithm || meta.algorithm,
107
+ iterations: opts.iterations || meta.iterations
108
+ }
109
+ ]);
110
+ const newSalt = createSalt(newMeta.saltLength);
111
+ const newHash = await hashWithSalt(valueWithPepper, newSalt, secret, newMeta);
112
+ newMeta.salt = newSalt;
113
+ newMeta.hash = newHash;
114
+ upgradedPassport = encodePassport(newMeta);
115
+ }
116
+ return { valid, needsUpgrade, upgradedPassport };
117
+ };
118
+
119
+ module.exports = {
120
+ hash,
121
+ verify,
122
+ encodePassport,
123
+ decodePassport
124
+ };
package/test.js ADDED
@@ -0,0 +1,22 @@
1
+ const { hash, verify } = require("./script");
2
+
3
+ const SECRET_KEY = process.env.HASH_SECRET || "abcd";
4
+
5
+ // create hash
6
+ (async () => {
7
+ const result = await hash("Happy", SECRET_KEY, {
8
+ iterations: 150_000
9
+ });
10
+
11
+ console.log(result);
12
+
13
+ // verify
14
+ const verifyResult = await verify(
15
+ "Happy",
16
+ result.passport, // <-- use passport, not salt/hash
17
+ SECRET_KEY,
18
+ { iterations: result.iterations }
19
+ );
20
+
21
+ console.log(verifyResult); // { valid: true, needsUpgrade: false, upgradedPassport: null }
22
+ })();