File tree 5 files changed +473
-19
lines changed
5 files changed +473
-19
lines changed Original file line number Diff line number Diff line change
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 ( ) ;
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change 36
36
"@types/sharp" : " ^0.31.0" ,
37
37
"@typescript-eslint/eslint-plugin" : " ^5.61.0" ,
38
38
"@typescript-eslint/parser" : " ^5.61.0" ,
39
+ "assert" : " ^2.0.0" ,
40
+ "crypto-browserify" : " ^3.12.0" ,
39
41
"eslint" : " ^8.42.0" ,
40
42
"eslint-plugin-unused-imports" : " ^2.0.0" ,
43
+ "events" : " ^3.1.0" ,
44
+ "os-browserify" : " ^0.3.0" ,
41
45
"parcel" : " ^2.9.3" ,
42
46
"parcel-reporter-static-files-copy" : " ^1.5.0" ,
43
47
"parcel-resolver-ts-base-url" : " ^1.3.1" ,
48
+ "path-browserify" : " ^1.0.0" ,
44
49
"posthtml-include" : " ^1.7.4" ,
45
50
"prettier" : " ^2.8.8" ,
46
51
"process" : " ^0.11.10" ,
52
+ "punycode" : " ^1.4.1" ,
47
53
"puppeteer" : " ^21.6.1" ,
54
+ "querystring-es3" : " ^0.2.1" ,
55
+ "stream-browserify" : " ^3.0.0" ,
48
56
"ts-node" : " ^10.9.1" ,
49
57
"typescript" : " ^5.1.3" ,
58
+ "url" : " ^0.11.0" ,
59
+ "util" : " ^0.12.3" ,
50
60
"workbox-cli" : " ^7.0.0"
51
61
},
52
62
"dependencies" : {
56
66
"@fortawesome/free-brands-svg-icons" : " ^6.4.0" ,
57
67
"@fortawesome/free-solid-svg-icons" : " ^6.4.0" ,
58
68
"@fortawesome/react-fontawesome" : " ^0.2.0" ,
69
+ "@types/bcryptjs" : " ^2.4.6" ,
59
70
"@types/cli-progress" : " ^3.11.0" ,
60
71
"adm-zip" : " ^0.5.10" ,
61
72
"axios" : " ^1.4.0" ,
73
+ "bcryptjs" : " ^2.4.3" ,
62
74
"classnames" : " ^2.3.2" ,
63
75
"cli-progress" : " ^3.12.0" ,
64
76
"commander" : " ^10.0.1" ,
Original file line number Diff line number Diff line change
1
+ import bcrypt from "bcryptjs" ;
2
+
1
3
const PASSWORD_HASH =
2
- "88e0b211e647f2a9f1d63720c3f1a7cef35a2940d28b5290fe12141ce446dbeb " ;
4
+ "$2a$10$1ES0fLT64vbFtjc98cN4zuXtyB5souy66c1wgUWeRYyGBuV/m7O.m " ;
3
5
4
6
export async function isCorrectPassword ( password : string ) : Promise < boolean > {
5
7
// quick check if there's no password attempt
6
8
if ( password == "" ) {
7
9
return false ;
8
10
}
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 ) ;
18
12
}
You can’t perform that action at this time.
0 commit comments