@n0uur/wordpress-hash-node 2.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/README.md ADDED
@@ -0,0 +1,58 @@
1
+ # @n0uur/wordpress-hash-node
2
+
3
+ Wordpress hashes (phpass library portable hashes) port for Node.js with TypeScript support.
4
+
5
+ Based on phpass for PHP 5 or greater.
6
+
7
+ > **Original Author Credit:** This package is a TypeScript port of [wordpress-hash-node](https://github.com/AlexAlbala/wordpress-hash-node) by Alex Albalà.
8
+
9
+ ## Installation
10
+
11
+ ```bash
12
+ npm install @n0uur/wordpress-hash-node
13
+ ```
14
+
15
+ ## Usage
16
+
17
+ ### JavaScript (CommonJS)
18
+
19
+ ```javascript
20
+ const { HashPassword, CheckPassword } = require("@n0uur/wordpress-hash-node");
21
+
22
+ const password = "testPassword";
23
+ const hash = HashPassword(password);
24
+ const checked = CheckPassword(password, hash); // This will return true
25
+ ```
26
+
27
+ ### TypeScript
28
+
29
+ ```typescript
30
+ import { HashPassword, CheckPassword } from "@n0uur/wordpress-hash-node";
31
+
32
+ const password = "testPassword";
33
+ const hash: string = HashPassword(password);
34
+ const checked: boolean = CheckPassword(password, hash); // This will return true
35
+ ```
36
+
37
+ ## API
38
+
39
+ ### `HashPassword(password: string): string`
40
+
41
+ Generates a WordPress-compatible hash for the given password.
42
+
43
+ - **Parameters:**
44
+ - `password` (string): The password to hash
45
+ - **Returns:** The hashed password string
46
+
47
+ ### `CheckPassword(password: string, stored_hash: string): boolean`
48
+
49
+ Verifies if a password matches the stored hash.
50
+
51
+ - **Parameters:**
52
+ - `password` (string): The password to verify
53
+ - `stored_hash` (string): The hash to compare against
54
+ - **Returns:** `true` if the password matches, `false` otherwise
55
+
56
+ ## License
57
+
58
+ GPL
package/dist/hash.d.ts ADDED
@@ -0,0 +1,13 @@
1
+ /**
2
+ * Hashes a password using WordPress's phpass implementation
3
+ * @param password - The password to hash
4
+ * @returns The hashed password
5
+ */
6
+ export declare function HashPassword(password: string): string;
7
+ /**
8
+ * Checks if a password matches the stored hash
9
+ * @param password - The password to check
10
+ * @param stored_hash - The stored hash to compare against
11
+ * @returns True if the password matches the hash
12
+ */
13
+ export declare function CheckPassword(password: string, stored_hash: string): boolean;
package/dist/hash.js ADDED
@@ -0,0 +1,108 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || (function () {
19
+ var ownKeys = function(o) {
20
+ ownKeys = Object.getOwnPropertyNames || function (o) {
21
+ var ar = [];
22
+ for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
23
+ return ar;
24
+ };
25
+ return ownKeys(o);
26
+ };
27
+ return function (mod) {
28
+ if (mod && mod.__esModule) return mod;
29
+ var result = {};
30
+ if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
31
+ __setModuleDefault(result, mod);
32
+ return result;
33
+ };
34
+ })();
35
+ Object.defineProperty(exports, "__esModule", { value: true });
36
+ exports.HashPassword = HashPassword;
37
+ exports.CheckPassword = CheckPassword;
38
+ const phpfunctions = __importStar(require("./phpfunctions"));
39
+ const itoa64 = "./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
40
+ const iteration_count_log2 = 8;
41
+ function crypt_private(password, setting) {
42
+ let output = "*0";
43
+ if (phpfunctions.substr(setting, 0, 2) === output)
44
+ output = "*1";
45
+ if (phpfunctions.substr(setting, 0, 3) !== "$P$")
46
+ return output;
47
+ const count_log2 = phpfunctions.strpos(itoa64, setting[3]);
48
+ if (count_log2 < 7 || count_log2 > 30)
49
+ return output;
50
+ const count = 1 << count_log2;
51
+ const salt = phpfunctions.substr(setting, 4, 8);
52
+ if (phpfunctions.strlen(salt) !== 8)
53
+ return output;
54
+ let hash = phpfunctions.md5(salt + "" + password, true);
55
+ let remaining = count;
56
+ do {
57
+ hash = phpfunctions.md5(hash + "" + password, true);
58
+ } while (--remaining);
59
+ output = phpfunctions.substr(setting, 0, 12);
60
+ output += encode64(hash, 16);
61
+ return output;
62
+ }
63
+ function gensalt_private(input) {
64
+ let output = "$P$";
65
+ output += itoa64[Math.min(iteration_count_log2 + 5, 30)];
66
+ output += encode64(input, 6);
67
+ return output;
68
+ }
69
+ function encode64(input, count) {
70
+ let output = "";
71
+ let i = 0;
72
+ do {
73
+ let value = phpfunctions.ord(input[i++]);
74
+ output += itoa64[value & 0x3f];
75
+ if (i < count)
76
+ value |= phpfunctions.ord(input[i]) << 8;
77
+ output += itoa64[(value >> 6) & 0x3f];
78
+ if (i++ >= count)
79
+ break;
80
+ if (i < count)
81
+ value |= phpfunctions.ord(input[i]) << 16;
82
+ output += itoa64[(value >> 12) & 0x3f];
83
+ if (i++ >= count)
84
+ break;
85
+ output += itoa64[(value >> 18) & 0x3f];
86
+ } while (i < count);
87
+ return output;
88
+ }
89
+ /**
90
+ * Hashes a password using WordPress's phpass implementation
91
+ * @param password - The password to hash
92
+ * @returns The hashed password
93
+ */
94
+ function HashPassword(password) {
95
+ const salt = gensalt_private(phpfunctions.sixCharRandom());
96
+ const hash = crypt_private(password, salt);
97
+ return hash;
98
+ }
99
+ /**
100
+ * Checks if a password matches the stored hash
101
+ * @param password - The password to check
102
+ * @param stored_hash - The stored hash to compare against
103
+ * @returns True if the password matches the hash
104
+ */
105
+ function CheckPassword(password, stored_hash) {
106
+ const hash = crypt_private(password, stored_hash);
107
+ return hash === stored_hash;
108
+ }
@@ -0,0 +1 @@
1
+ export { HashPassword, CheckPassword } from "./hash";
package/dist/index.js ADDED
@@ -0,0 +1,6 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.CheckPassword = exports.HashPassword = void 0;
4
+ var hash_1 = require("./hash");
5
+ Object.defineProperty(exports, "HashPassword", { enumerable: true, get: function () { return hash_1.HashPassword; } });
6
+ Object.defineProperty(exports, "CheckPassword", { enumerable: true, get: function () { return hash_1.CheckPassword; } });
@@ -0,0 +1,6 @@
1
+ export declare function strlen(str: string): number;
2
+ export declare function strpos(string: string, find: string): number;
3
+ export declare function md5(string: string, raw?: boolean): string;
4
+ export declare function sixCharRandom(): string;
5
+ export declare function substr(string: string, start: number, count: number): string;
6
+ export declare function ord(input: string): number;
@@ -0,0 +1,43 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.strlen = strlen;
7
+ exports.strpos = strpos;
8
+ exports.md5 = md5;
9
+ exports.sixCharRandom = sixCharRandom;
10
+ exports.substr = substr;
11
+ exports.ord = ord;
12
+ const crypto_1 = __importDefault(require("crypto"));
13
+ function strlen(str) {
14
+ return str.length;
15
+ }
16
+ function strpos(string, find) {
17
+ return string.indexOf(find);
18
+ }
19
+ function md5(string, raw) {
20
+ const hash = crypto_1.default.createHash("md5");
21
+ hash.update(string, "binary");
22
+ if (raw) {
23
+ return hash.digest("binary");
24
+ }
25
+ else {
26
+ return hash.digest("hex");
27
+ }
28
+ }
29
+ function sixCharRandom() {
30
+ let text = "";
31
+ const possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
32
+ for (let i = 0; i < 6; i++) {
33
+ text += possible.charAt(Math.floor(Math.random() * possible.length));
34
+ }
35
+ return text;
36
+ }
37
+ function substr(string, start, count) {
38
+ return string.substring(start, start + count);
39
+ }
40
+ function ord(input) {
41
+ const r = input.charCodeAt(0);
42
+ return r;
43
+ }
package/package.json ADDED
@@ -0,0 +1,45 @@
1
+ {
2
+ "name": "@n0uur/wordpress-hash-node",
3
+ "description": "A phpass portable hashing function compatible with wordpress passwords (TypeScript port)",
4
+ "author": "n0uur",
5
+ "contributors": [
6
+ {
7
+ "name": "Alex Albalà",
8
+ "email": "alexalbala88@gmail.com",
9
+ "url": "https://github.com/AlexAlbala/wordpress-hash-node"
10
+ }
11
+ ],
12
+ "dependencies": {},
13
+ "devDependencies": {
14
+ "@types/node": "^20.11.0",
15
+ "typescript": "^5.3.3"
16
+ },
17
+ "version": "2.0.0",
18
+ "main": "dist/index.js",
19
+ "types": "dist/index.d.ts",
20
+ "files": [
21
+ "dist"
22
+ ],
23
+ "scripts": {
24
+ "build": "tsc",
25
+ "prepublishOnly": "npm run build",
26
+ "test": "echo \"Error: no test specified\" && exit 1"
27
+ },
28
+ "repository": {
29
+ "type": "git",
30
+ "url": "https://github.com/n0uur/wordpress-hash-node"
31
+ },
32
+ "keywords": [
33
+ "wordpress",
34
+ "phpass",
35
+ "hash",
36
+ "typescript",
37
+ "password",
38
+ "hashing"
39
+ ],
40
+ "license": "GPL",
41
+ "bugs": {
42
+ "url": "https://github.com/n0uur/wordpress-hash-node/issues"
43
+ },
44
+ "homepage": "https://github.com/n0uur/wordpress-hash-node"
45
+ }