66 * This script will:
77 * 1. Ask you for your desired master password
88 * 2. Generate a bcrypt hash
9- * 3. Update the .env file automatically
10- * 4. Test that everything works correctly
9+ * 3. Base64 encode the hash for storage
10+ * 4. Update the .env file automatically
11+ * 5. Test that everything works correctly
1112 */
1213
1314import bcrypt from 'bcryptjs' ;
@@ -94,10 +95,16 @@ async function main() {
9495 const hash = await bcrypt . hash ( password , 10 ) ;
9596
9697 log ( '✓ Hash generated successfully!' , 'green' ) ;
97- log ( `\nGenerated hash: ${ colors . cyan } ${ hash } ${ colors . reset } \n ` ) ;
98+ log ( `\nGenerated bcrypt hash: ${ colors . cyan } ${ hash } ${ colors . reset } ` ) ;
9899
99- // Step 4: Update .env file
100- log ( 'Step 4: Updating .env file...' , 'bright' ) ;
100+ // Step 4: Base64 encode the hash
101+ log ( '\nStep 4: Encoding hash to base64...' , 'bright' ) ;
102+ const base64Hash = Buffer . from ( hash ) . toString ( 'base64' ) ;
103+ log ( '✓ Hash encoded successfully!' , 'green' ) ;
104+ log ( `\nBase64-encoded hash: ${ colors . cyan } ${ base64Hash } ${ colors . reset } \n` ) ;
105+
106+ // Step 5: Update .env file
107+ log ( 'Step 5: Updating .env file...' , 'bright' ) ;
101108
102109 const envPath = path . join ( __dirname , '..' , '.env' ) ;
103110
@@ -111,17 +118,14 @@ async function main() {
111118 // Read current .env file
112119 let envContent = fs . readFileSync ( envPath , 'utf8' ) ;
113120
114- // Escape backslashes and $ characters to prevent unintended interpolation in .env files
115- const escapedHash = hash . replace ( / \\ / g, '\\\\' ) . replace ( / \$ / g, '\\$' ) ;
116-
117- // Replace the hash line
121+ // Replace the hash line with base64-encoded hash
118122 const hashRegex = / ^ M A S T E R _ P A S S W O R D _ B C R Y P T _ H A S H = .* / m;
119123 if ( hashRegex . test ( envContent ) ) {
120- envContent = envContent . replace ( hashRegex , `MASTER_PASSWORD_BCRYPT_HASH=${ hash } ` ) ;
124+ envContent = envContent . replace ( hashRegex , `MASTER_PASSWORD_BCRYPT_HASH=${ base64Hash } ` ) ;
121125 log ( '✓ Found and updated existing MASTER_PASSWORD_BCRYPT_HASH' , 'green' ) ;
122126 } else {
123127 // Add it if it doesn't exist
124- envContent = `MASTER_PASSWORD_BCRYPT_HASH=${ hash } \n` + envContent ;
128+ envContent = `MASTER_PASSWORD_BCRYPT_HASH=${ base64Hash } \n` + envContent ;
125129 log ( '✓ Added MASTER_PASSWORD_BCRYPT_HASH to .env' , 'green' ) ;
126130 }
127131
0 commit comments