UNPKG

hidenv

Version:

Beautiful CLI tool to encrypt and decrypt .env files with AES-256-GCM

491 lines (353 loc) โ€ข 13.4 kB
# ๐Ÿ” HidEnv - Secure Environment Tool A beautiful CLI tool to encrypt and decrypt `.env` files using military-grade AES-256-GCM encryption. Keep your secrets safe! ๐Ÿ›ก๏ธ ![Version](https://img.shields.io/badge/version-1.0.0-blue.svg) ![Node](https://img.shields.io/badge/node-%3E%3D16-green.svg) ![License](https://img.shields.io/badge/license-MIT-yellow.svg) ## ๐Ÿš€ Quick Start ```bash # Install globally npm install -g hidenv # Quick test (try it now!) echo "TEST_VAR=hello_world" > .env hidenv --e mypassword # Encrypt hidenv --d mypassword # Decrypt cat .env # Verify: should show TEST_VAR=hello_world # That's it! Your .env is now safely encrypted as .env.enc ``` ### โšก One-Minute Test Want to see it in action right now? ```bash # Create test file echo "DEMO=this_is_a_test" > .env # Encrypt it hidenv --e demo123 # Remove original rm .env # Decrypt it back hidenv --d demo123 # Check it worked cat .env # Output: DEMO=this_is_a_test ``` ## ๐ŸŒŸ Features - ๐Ÿ”’ **Military-Grade Encryption**: AES-256-GCM with scrypt key derivation - ๐ŸŽจ **Beautiful Interface**: Interactive CLI with colors, spinners, and ASCII art - โšก **Lightning Fast**: Direct encryption/decryption with command arguments - ๐Ÿ”‘ **Secure Input**: Password masking and validation - ๐ŸŒ **Cross-Platform**: Works on Windows, macOS, and Linux - ๐Ÿ“ฆ **Zero Dependencies**: Only crypto built-ins, no external crypto libraries ## ๐Ÿš€ Installation ### Global Installation ```bash npm install -g secure-env-tool ``` ### Local Installation ```bash git clone <repository-url> cd secure-env-tool npm install npm link ``` ## ๐Ÿ“– Usage ### ๐ŸŽฎ Interactive Mode Launch the beautiful interactive interface: ```bash hidenv ``` **What you'll see:** ``` _ _ _ _ _____ _ ___ __ | | | (_) __| | ____| \ | \ \ / / | |_| | |/ _` | _| | \| |\ \ / / | _ | | (_| | |___| |\ | \ V / |_| |_|_|\__,_|_____|_| \_| \_/ ? What do you want to do? (Use arrow keys) โฏ ๐Ÿ”’ Encrypt .env ๐Ÿ”“ Decrypt .env ? Enter your secret key: [hidden] โ ‹ Encrypting... โœ” File encrypted as .env.enc ``` ### โšก Command Line Mode For automation and scripts: #### ๐Ÿ”’ Encrypt a .env file ```bash # Interactive password prompt hidenv --e # Output: โœ” File encrypted as .env.enc # Direct password (not recommended for production) hidenv --e mypassword ``` #### ๐Ÿ”“ Decrypt a .env.enc file ```bash # Interactive password prompt hidenv --d # Output: โœ” File decrypted as .env # Direct password (not recommended for production) hidenv --d mypassword ``` #### ๐Ÿ“š Show help ```bash hidenv --help ``` ## ๐Ÿงช Quick Test & Verification Want to verify everything works correctly? Follow these simple steps: ### ๐Ÿš€ Basic Test ```bash # 1. Create a test .env file echo "TEST_API_KEY=secret123" > .env echo "TEST_PASSWORD=mypassword" >> .env # 2. Encrypt with a test password hidenv --e testpass123 # 3. Verify .env.enc was created ls -la *.enc # 4. Test decryption hidenv --d testpass123 # 5. Verify content is restored cat .env # Should show: # TEST_API_KEY=secret123 # TEST_PASSWORD=mypassword ``` ### ๐Ÿ”’ Security Test ```bash # Test with wrong password (should fail) hidenv --d wrongpassword # Expected: โœ– Error: Failed to decrypt. Check your password. # Test with correct password (should work) hidenv --d testpass123 # Expected: โœ” File decrypted as .env ``` ### ๐ŸŽฎ Interactive Mode Test ```bash # Launch interactive mode hidenv # Follow the prompts: # 1. Choose "๐Ÿ”’ Encrypt .env" or "๐Ÿ”“ Decrypt .env" # 2. Enter your password when prompted # 3. Watch the beautiful spinner animation! ``` ## ๐Ÿ”ง How it Works ### ๐Ÿ“Š Visual Process Flow ``` โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” โ”‚ ENCRYPTION PROCESS โ”‚ โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค โ”‚ 1. Read .env file โ”‚ 2. Generate Salt โ”‚ 3. Derive Key โ”‚ โ”‚ โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” โ”‚ โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” โ”‚ โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” โ”‚ โ”‚ โ”‚ API_KEY=123 โ”‚ โ”‚ โ”‚ Random 16B โ”‚ โ”‚ โ”‚ scrypt(pwd) โ”‚ โ”‚ โ”‚ โ”‚ DB_PASS=xyz โ”‚ โ”€โ”€โ–ถโ”‚ โ”‚ Salt โ”‚โ”€โ”€โ–ถโ”‚ โ”‚ + salt โ”‚ โ”‚ โ”‚ โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ โ”‚ โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ โ”‚ โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ โ”‚ โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค โ”‚ 4. Generate IV โ”‚ 5. Encrypt Data โ”‚ 6. Save .env.enc โ”‚ โ”‚ โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” โ”‚ โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” โ”‚ โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” โ”‚ โ”‚ โ”‚ Random 16B โ”‚ โ”‚ โ”‚ AES-256-GCM โ”‚ โ”‚ โ”‚ Salt+IV+ โ”‚ โ”‚ โ”‚ โ”‚ IV โ”‚ โ”€โ”€โ–ถโ”‚ โ”‚ Encryption โ”‚โ”€โ”€โ–ถโ”‚ โ”‚ Tag+Data โ”‚ โ”‚ โ”‚ โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ โ”‚ โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ โ”‚ โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ โ”‚ โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ ``` ### Encryption Process 1. ๐Ÿ“ **Read**: Parses your `.env` file preserving comments and formatting 2. ๐Ÿง‚ **Salt**: Generates a cryptographically random 16-byte salt 3. ๐Ÿ”‘ **Key Derivation**: Uses scrypt with high cost parameters (N=16384, r=8, p=1) 4. ๐ŸŽฒ **IV Generation**: Creates a random 16-byte initialization vector 5. ๐Ÿ”’ **Encrypt**: Uses AES-256-GCM for authenticated encryption 6. ๐Ÿ“ฆ **Package**: Combines all components into a binary `.env.enc` file ### Decryption Process 1. ๐Ÿ“– **Read**: Opens and validates the `.env.enc` file format 2. ๐Ÿ” **Extract**: Separates salt, IV, auth tag, and encrypted data 3. ๐Ÿ”‘ **Key Derivation**: Recreates the key using your password and extracted salt 4. ๐Ÿ”“ **Decrypt**: Uses AES-256-GCM to decrypt and verify integrity 5. โœ… **Verify**: Validates authentication tag to ensure no tampering 6. ๐Ÿ’พ **Save**: Writes the decrypted content back to `.env` ## ๐Ÿ›ก๏ธ Security Features - **AES-256-GCM**: Industry-standard authenticated encryption - **scrypt**: Memory-hard key derivation function (resistant to brute-force) - **Random Salt**: Prevents rainbow table attacks - **Random IV**: Ensures unique ciphertext even with same plaintext - **Authentication Tag**: Prevents tampering and ensures data integrity ## ๐Ÿ”’ Why Encrypt .env Files? Environment files often contain sensitive information like: - API keys - Database passwords - Secret tokens - Configuration secrets By encrypting these files, you can: - โœ… Store them safely in version control - โœ… Share them securely with team members - โœ… Backup sensitive configurations - โœ… Prevent accidental exposure ## โš ๏ธ Important Notes - **Remember your password**: Without it, your encrypted data cannot be recovered - **Backup strategy**: Keep secure backups of both encrypted files and passwords - **Version control**: Add `.env` to `.gitignore`, commit `.env.enc` instead - **Team sharing**: Share passwords through secure channels only ### ๐Ÿš€ Production Workflow 1. **Initial setup** with sensitive `.env`: ```bash # Create your .env file echo "API_KEY=secret123" > .env echo "DB_PASSWORD=supersecret" >> .env # Encrypt it hidenv --e mypassword # Add encrypted version to git git add .env.enc git commit -m "Add encrypted environment variables" ``` 2. **Team member setup**: ```bash # Clone repository git clone <repo> cd <repo> # Decrypt (password shared securely) hidenv --d mypassword # Now you have the .env file ready ``` 3. **Updating secrets**: ```bash # Edit .env file nano .env # Re-encrypt hidenv --e mypassword # Commit updated encrypted file git add .env.enc git commit -m "Update environment variables" ``` ## ๐ŸŽฏ Real-World Examples ### Example 1: Node.js API Project **Before (insecure):** ```bash # .env file in your repository (โŒ DANGEROUS) API_KEY=sk-abc123def456 DATABASE_URL=postgresql://user:pass@host:5432/db JWT_SECRET=my-super-secret-key STRIPE_SECRET=sk_test_123456789 ``` **After (secure):** ```bash # 1. Encrypt your secrets hidenv --e # 2. Only commit the encrypted version git add .env.enc git commit -m "Add encrypted environment variables" # 3. Add .env to .gitignore echo ".env" >> .gitignore ``` ### Example 2: Team Collaboration **Team Lead:** ```bash # Share the encrypted file via git git add .env.enc git commit -m "Add team environment variables" git push origin main # Share password securely (Slack DM, encrypted message, etc.) # Password: "MyTeamSecurePass2024!" ``` **Team Member:** ```bash # Clone and decrypt git clone https://github.com/company/project.git cd project hidenv --d # Enter shared password npm install npm start # Environment ready! ๐Ÿš€ ``` ### Example 3: Multiple Environments ```bash # Development environment cp .env.dev .env hidenv --e devpassword mv .env.enc .env.dev.enc # Production environment cp .env.prod .env hidenv --e prodpassword mv .env.enc .env.prod.enc # Deploy to production hidenv --d prodpassword # Creates .env from .env.enc docker build -t myapp . ``` ## ๐Ÿ” File Format Deep Dive ### Binary Structure of .env.enc ``` โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” โ”‚ .env.enc File Structure โ”‚ โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค โ”‚ Magic Header โ”‚ Version โ”‚ Salt (16B) โ”‚ Length โ”‚ Encrypted โ”‚ โ”‚ "hidenv" โ”‚ 0x01 โ”‚ Random โ”‚ 4B โ”‚ Content โ”‚ โ”‚ (4 bytes) โ”‚ (1 byte)โ”‚ โ”‚ โ”‚ (Variable) โ”‚ โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ ``` **Why this format?** - ๐Ÿ” **Magic Header**: Identifies the file type instantly - ๐Ÿ“Š **Version**: Allows future format updates - ๐Ÿง‚ **Salt**: Each encryption uses a unique salt - ๐Ÿ“ **Length**: Prevents buffer overflow attacks - ๐Ÿ”’ **Content**: Your encrypted environment variables ## ๐Ÿšจ Security Best Practices ### โœ… DO's - โœ… Use strong, unique passwords (12+ characters) - โœ… Store `.env.enc` in version control - โœ… Share passwords through secure channels - โœ… Backup both encrypted files and passwords - โœ… Rotate passwords periodically - โœ… Use different passwords for different environments ## ๐Ÿ› Troubleshooting ### Common Issues **โ“ "File not found" error** ```bash # Make sure you're in the right directory ls -la | grep env # Check if file exists hidenv --help # Should show available options ``` **โ“ "Invalid password" error** ```bash # Password is case-sensitive, try again carefully hidenv --d # Check if .env.enc is corrupted file .env.enc # Should show "data" ``` **โ“ "Permission denied" error** ```bash # Fix file permissions chmod 644 .env.enc chmod 600 .env # After decryption ``` ### ๐Ÿงช Debug Commands ```bash # Verify the tool is working correctly node bin/cli.js --help # Test with a simple example echo "DEBUG_TEST=working" > .env node bin/cli.js --e debug123 node bin/cli.js --d debug123 cat .env # Should show: DEBUG_TEST=working # Check file formats ls -la .env* # Should show both .env and .env.enc files # Test error handling node bin/cli.js --d wrongpassword # Should show: โœ– Error: Failed to decrypt. Check your password. ``` ### Getting Help - ๐Ÿ“– Run `hidenv --help` for usage information - ๐Ÿ› Check file permissions and current directory - ๐Ÿ”‘ Verify password is correct (case-sensitive) - ๐Ÿ“ Ensure `.env` or `.env.enc` files exist ### Contributing We welcome contributions! Here's how you can help: - ๐Ÿ› **Report bugs** on GitHub Issues - ๐Ÿ’ก **Suggest features** via GitHub Discussions - ๐Ÿ”ง **Submit pull requests** with improvements - ๐Ÿ“– **Improve documentation** and examples - โญ **Star the project** to show support ## ๏ฟฝ๐Ÿ“„ License MIT License - see LICENSE file for details ## ๐Ÿค Contributing Contributions are welcome! Please feel free to submit a Pull Request. --- <div align="center"> **Made with โค๏ธ by erik** _Keep your secrets safe! ๐Ÿ”_ [![GitHub stars](https://img.shields.io/github/stars/username/secure-env-tool.svg?style=social&label=Star)](https://github.com/xErik444x/hidenv) </div>