hidenv
Version:
Beautiful CLI tool to encrypt and decrypt .env files with AES-256-GCM
491 lines (353 loc) โข 13.4 kB
Markdown
# ๐ 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! ๐ก๏ธ



## ๐ 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! ๐_
[](https://github.com/xErik444x/hidenv)
</div>