Skip to content

Commit a20e8ed

Browse files
committed
Use bcrypt for password
1 parent e15867c commit a20e8ed

File tree

5 files changed

+473
-19
lines changed

5 files changed

+473
-19
lines changed

hash_password/src/main.ts

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import bcrypt from "bcryptjs";
2+
import { Command } from "commander";
3+
4+
async function hashPassword(password: string): Promise<string> {
5+
const saltRounds = 10;
6+
7+
return new Promise((resolve, reject) => {
8+
bcrypt.hash(password, saltRounds, (err, hash) => {
9+
if (err) {
10+
reject(err);
11+
} else {
12+
resolve(hash);
13+
}
14+
});
15+
});
16+
}
17+
18+
async function main() {
19+
const program = new Command();
20+
program
21+
.version("0.1.0")
22+
.description("Hash password")
23+
.argument("<password>", "Password to hash")
24+
.action(async (password: string, _options) => {
25+
const hash = await hashPassword(password);
26+
console.log(hash);
27+
if (!(await bcrypt.compare(password, hash))) {
28+
console.error("password is not considered correct?!");
29+
}
30+
});
31+
program.parseAsync();
32+
}
33+
34+
main();

hash_password/tsconfig.json

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"compilerOptions": {
3+
"rootDir": "src",
4+
"outDir": "dist",
5+
"target": "es2016",
6+
"module": "NodeNext",
7+
"forceConsistentCasingInFileNames": true,
8+
"lib": ["es2021"],
9+
"sourceMap": true,
10+
"esModuleInterop": true,
11+
"moduleResolution": "nodenext",
12+
"strict": true
13+
}
14+
}

package.json

+12
Original file line numberDiff line numberDiff line change
@@ -36,17 +36,27 @@
3636
"@types/sharp": "^0.31.0",
3737
"@typescript-eslint/eslint-plugin": "^5.61.0",
3838
"@typescript-eslint/parser": "^5.61.0",
39+
"assert": "^2.0.0",
40+
"crypto-browserify": "^3.12.0",
3941
"eslint": "^8.42.0",
4042
"eslint-plugin-unused-imports": "^2.0.0",
43+
"events": "^3.1.0",
44+
"os-browserify": "^0.3.0",
4145
"parcel": "^2.9.3",
4246
"parcel-reporter-static-files-copy": "^1.5.0",
4347
"parcel-resolver-ts-base-url": "^1.3.1",
48+
"path-browserify": "^1.0.0",
4449
"posthtml-include": "^1.7.4",
4550
"prettier": "^2.8.8",
4651
"process": "^0.11.10",
52+
"punycode": "^1.4.1",
4753
"puppeteer": "^21.6.1",
54+
"querystring-es3": "^0.2.1",
55+
"stream-browserify": "^3.0.0",
4856
"ts-node": "^10.9.1",
4957
"typescript": "^5.1.3",
58+
"url": "^0.11.0",
59+
"util": "^0.12.3",
5060
"workbox-cli": "^7.0.0"
5161
},
5262
"dependencies": {
@@ -56,9 +66,11 @@
5666
"@fortawesome/free-brands-svg-icons": "^6.4.0",
5767
"@fortawesome/free-solid-svg-icons": "^6.4.0",
5868
"@fortawesome/react-fontawesome": "^0.2.0",
69+
"@types/bcryptjs": "^2.4.6",
5970
"@types/cli-progress": "^3.11.0",
6071
"adm-zip": "^0.5.10",
6172
"axios": "^1.4.0",
73+
"bcryptjs": "^2.4.3",
6274
"classnames": "^2.3.2",
6375
"cli-progress": "^3.12.0",
6476
"commander": "^10.0.1",

src/js/password.ts

+4-10
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,12 @@
1+
import bcrypt from "bcryptjs";
2+
13
const PASSWORD_HASH =
2-
"88e0b211e647f2a9f1d63720c3f1a7cef35a2940d28b5290fe12141ce446dbeb";
4+
"$2a$10$1ES0fLT64vbFtjc98cN4zuXtyB5souy66c1wgUWeRYyGBuV/m7O.m";
35

46
export async function isCorrectPassword(password: string): Promise<boolean> {
57
// quick check if there's no password attempt
68
if (password == "") {
79
return false;
810
}
9-
const hashBuffer = await crypto.subtle.digest(
10-
"SHA-256",
11-
new TextEncoder().encode(password),
12-
);
13-
const hashArray = Array.from(new Uint8Array(hashBuffer)); // convert buffer to byte array
14-
const hashHex = hashArray
15-
.map((b) => b.toString(16).padStart(2, "0"))
16-
.join(""); // convert bytes to hex string
17-
return hashHex == PASSWORD_HASH;
11+
return await bcrypt.compare(password, PASSWORD_HASH);
1812
}

0 commit comments

Comments
 (0)