A utility for encrypting and decrypting environment variables in frontend projects, inspired by Laravel's encryption methods.
npm install env-encryptionEnv Encryption is a command-line tool designed for encrypting and decrypting environment variables within frontend projects. Drawing inspiration from Laravel's encryption techniques, it offers a secure means of safeguarding sensitive environment configurations in client-side applications while enabling their safe inclusion in your version control system (Git).
You can install the env-encryption package globally using npm:
``bash`
npm install -g env-encryption
Alternatively, you can use it on the fly with npx, prefixing the commands with npx:
`bash`
npx env-encryption
To use Env Encryption, run it from the command line with the following options:
`bash`
env-encryption [command] --key
- [command]: Specify the operation to perform. Use either encrypt or decrypt.--key
- : Specify the encryption key. If not provided, a random key will be generated.--env
- : Specify the environment name.--filename
- : Specify the target filename for decrypting (optional).--force
- : Overwrite existing files if they already exist (optional).--cipher
- : Specify the encryption cipher algorithm. The default is aes-256-cbc (optional).
#### Encrypting Environment Variables
To encrypt environment variables, follow these steps:
1. Open your terminal.
2. Navigate to the root directory of your project.
3. Make sure a .env file is present for the env your encrypting (e.g., .env.production).
3. Run the following command, replacing the placeholders with your actual data:
`bash`
env-encryption encrypt --key
- : Specify the encryption key. If not provided, a random key will be generated.
- : Specify the environment name.--force
- (optional): Use this flag to overwrite existing encrypted files.--cipher
- (optional): Specify the encryption cipher algorithm (default is aes-256-cbc).
Example:
`bash`
env-encryption encrypt --key mySecretKey --env production --force
4. If you didn't specify an encryption key, the tool will generate a random key and display it in the terminal.
5. The encrypted environment file (e.g., .env.production.encrypted) will be created in your project directory.
#### Decrypting Environment Variables
To decrypt environment variables, follow these steps:
1. Open your terminal.
2. Navigate to the root directory of your project.
3. Make sure a .env file is present for the env your encrypting (e.g., .env.production.encrypted).
3. Run the following command, replacing the placeholders with your actual data:
`bash`
env-encryption decrypt --key
- : Specify the decryption key.
- : Specify the environment name.
- : Specify the target filename.--force
- (optional): Use this flag to overwrite existing files.--cipher
- (optional): Specify the encryption cipher algorithm (default is aes-256-cbc).
Example:
`bash`
env-encryption decrypt --key mySecretKey --env production --filename .env --force
4. The decrypted environment file (e.g., .env) will be created in your project directory.
To encrypt an environment file, use the encrypt command:
`bash`
env-encryption encrypt --key
- : The encryption key (mandatory).
- : The environment name (mandatory).--force
- : Overwrite existing encrypted files (optional).--cipher
- : Specify the encryption cipher algorithm. The default is aes-256-cbc (optional).
If you omit the --key option, a random key will be generated and displayed.
To decrypt an environment file, use the decrypt command:
`bash`
env-encryption decrypt --key
- : The decryption key (mandatory).
- : The environment name (mandatory).
- : The target filename where the content will be written (optional).--force
- : Overwrite existing files if they already exist (optional).--cipher
- : Specify the encryption cipher algorithm. The default is aes-256-cbc (optional).
The utility includes basic error handling to ensure data integrity and security:
- It checks for the existence of input and output files.
- It verifies the encryption key's format and handles base64 encoding.
- It provides warnings when overwriting existing files.
Logs are generated for each operation, including a timestamp and log level (INFO, SUCCESS, WARNING, ERROR). Logs are displayed in the terminal for monitoring and debugging.
The utility uses the following Node.js modules:
- optimist for command-line argument parsing.fs
- for file system operations.crypto
- for cryptographic functions.chalk` for colorized console output.
-
This project is licensed under the MIT License.
- Jelle Nijhof