forked from mirrors/amnezia-wg-easy
		
	fix: lint errors
This commit is contained in:
		
							parent
							
								
									a9c798deda
								
							
						
					
					
						commit
						f5d93f6c5a
					
				
					 1 changed files with 41 additions and 36 deletions
				
			
		
							
								
								
									
										77
									
								
								src/wgpw.mjs
									
										
									
									
									
								
							
							
						
						
									
										77
									
								
								src/wgpw.mjs
									
										
									
									
									
								
							| 
						 | 
					@ -1,49 +1,54 @@
 | 
				
			||||||
 | 
					'use strict';
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// Import needed libraries
 | 
					// Import needed libraries
 | 
				
			||||||
import bcrypt from 'bcryptjs';
 | 
					import bcrypt from 'bcryptjs';
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// Function to generate hash
 | 
					// Function to generate hash
 | 
				
			||||||
const generateHash = async (password) => {
 | 
					const generateHash = async (password) => {
 | 
				
			||||||
    try {
 | 
					  try {
 | 
				
			||||||
        const salt = await bcrypt.genSalt(12);
 | 
					    const salt = await bcrypt.genSalt(12);
 | 
				
			||||||
        const hash = await bcrypt.hash(password, salt);
 | 
					    const hash = await bcrypt.hash(password, salt);
 | 
				
			||||||
        console.log(`PASSWORD_HASH='${hash}'`);
 | 
					    // eslint-disable-next-line no-console
 | 
				
			||||||
    } catch (error) {
 | 
					    console.log(`PASSWORD_HASH='${hash}'`);
 | 
				
			||||||
        throw new Error(`Failed to generate hash : ${error}`);
 | 
					  } catch (error) {
 | 
				
			||||||
    }
 | 
					    throw new Error(`Failed to generate hash : ${error}`);
 | 
				
			||||||
}
 | 
					  }
 | 
				
			||||||
 | 
					};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// Function to compare password with hash
 | 
					// Function to compare password with hash
 | 
				
			||||||
const comparePassword = async (password, hash) => {
 | 
					const comparePassword = async (password, hash) => {
 | 
				
			||||||
    try {
 | 
					  try {
 | 
				
			||||||
        const match = await bcrypt.compare(password, hash);
 | 
					    const match = await bcrypt.compare(password, hash);
 | 
				
			||||||
        if (match) {
 | 
					    if (match) {
 | 
				
			||||||
            console.log('Password matches the hash !');
 | 
					    // eslint-disable-next-line no-console
 | 
				
			||||||
        } else {
 | 
					      console.log('Password matches the hash !');
 | 
				
			||||||
            console.log('Password does not match the hash.');
 | 
					    } else {
 | 
				
			||||||
        }
 | 
					    // eslint-disable-next-line no-console
 | 
				
			||||||
    } catch (error) {
 | 
					      console.log('Password does not match the hash.');
 | 
				
			||||||
        throw new Error(`Failed to compare password and hash : ${error}`);
 | 
					 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
}
 | 
					  } catch (error) {
 | 
				
			||||||
 | 
					    throw new Error(`Failed to compare password and hash : ${error}`);
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
(async () => {
 | 
					(async () => {
 | 
				
			||||||
    try {
 | 
					  try {
 | 
				
			||||||
        // Retrieve command line arguments
 | 
					    // Retrieve command line arguments
 | 
				
			||||||
        const args = process.argv.slice(2); // Ignore the first two arguments
 | 
					    const args = process.argv.slice(2); // Ignore the first two arguments
 | 
				
			||||||
        if (args.length > 2) {
 | 
					    if (args.length > 2) {
 | 
				
			||||||
            throw new Error('Usage : wgpw YOUR_PASSWORD [HASH]');
 | 
					      throw new Error('Usage : wgpw YOUR_PASSWORD [HASH]');
 | 
				
			||||||
        }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        const [password, hash] = args;
 | 
					 | 
				
			||||||
        if (password && hash) {
 | 
					 | 
				
			||||||
            await comparePassword(password, hash);
 | 
					 | 
				
			||||||
        } else if (password) {
 | 
					 | 
				
			||||||
            await generateHash(password);
 | 
					 | 
				
			||||||
        }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        process.exit(0);
 | 
					 | 
				
			||||||
    } catch (error) {
 | 
					 | 
				
			||||||
        console.error(error);
 | 
					 | 
				
			||||||
        process.exit(1);
 | 
					 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    const [password, hash] = args;
 | 
				
			||||||
 | 
					    if (password && hash) {
 | 
				
			||||||
 | 
					      await comparePassword(password, hash);
 | 
				
			||||||
 | 
					    } else if (password) {
 | 
				
			||||||
 | 
					      await generateHash(password);
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					  } catch (error) {
 | 
				
			||||||
 | 
					    // eslint-disable-next-line no-console
 | 
				
			||||||
 | 
					    console.error(error);
 | 
				
			||||||
 | 
					    // eslint-disable-next-line no-process-exit
 | 
				
			||||||
 | 
					    process.exit(1);
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
})();
 | 
					})();
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue