|
| 1 | +import * as bcrypt from 'bcrypt'; |
| 2 | +import argon2 from 'argon2'; |
| 3 | + |
| 4 | +const password = process.argv[2]; |
| 5 | +const hashType = process.argv[3] || 'argon2'; |
| 6 | + |
| 7 | +if (!password) { |
| 8 | + console.error('Usage: bun/node run gen-hash <password> [type]'); |
| 9 | + console.error('\tExample: bun/node run gen-hash mySecurePassword123 argon2'); |
| 10 | + console.error('\tExample: bun/node run gen-hash mySecurePassword123 bcrypt'); |
| 11 | + console.error('\nSupported types: argon2 (default), bcrypt'); |
| 12 | + process.exit(1); |
| 13 | +} |
| 14 | + |
| 15 | +if (hashType !== 'argon2' && hashType !== 'bcrypt') { |
| 16 | + console.error(`Error: Invalid hash type "${hashType}"`); |
| 17 | + console.error('Supported types: argon2, bcrypt'); |
| 18 | + process.exit(1); |
| 19 | +} |
| 20 | + |
| 21 | +let hash: string; |
| 22 | + |
| 23 | +if (hashType === 'argon2') { |
| 24 | + hash = await argon2.hash(password, { |
| 25 | + type: argon2.argon2id, |
| 26 | + memoryCost: 65536, // 64 MB |
| 27 | + timeCost: 3, |
| 28 | + parallelism: 4 |
| 29 | + }); |
| 30 | + console.log('\n✅ Argon2 hash generated successfully!\n'); |
| 31 | +} else { |
| 32 | + const saltRounds = 10; |
| 33 | + hash = bcrypt.hashSync(password, saltRounds); |
| 34 | + console.log('\n✅ Bcrypt hash generated successfully!\n'); |
| 35 | +} |
| 36 | + |
| 37 | +const escapedHash = hash.replace(/\$/g, '\\$'); |
| 38 | +console.log('Add this to your .env file:'); |
| 39 | +console.log(`SERVER_PASSWORD = "${escapedHash}"`); |
| 40 | +console.log('\nRaw hash:'); |
| 41 | +console.log(escapedHash); |
0 commit comments