Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Encrypt folder #9

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion AUTHORS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# cryptr+ Authors

## Current Maintainer
- Guillaume Archambault ([Gu1llaum-3](https://github.com/Gu1llaum-3))

## Original cryptr Authors and Contributors
- Justin Keller ([nodesocket](https://github.com/nodesocket))
- Manuel Wildauer ([int9h](https://github.com/int9h))
- Adam Daniels ([adam12](https://github.com/adam12))
- Nicolas Le Gall ([Darkitty](https://github.com/Darkitty))
- Gu1llaum-3 ([Gu1llaum-3](https://github.com/Gu1llaum-3))
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,16 @@
CHANGELOG
=========

## 3.0.0 - *1/8/2025*

- Fork from cryptr to create cryptr+
- Added support for directory encryption/decryption using tar.gz
- Updated all documentation and tests
- Improved shell completion support:
- Added zsh completion script
- Updated bash completion script
- Enhanced README.md with detailed completion setup instructions for both shells

## 2.3.0 - *7/30/2024*

- Prompt to confirm deleting original file when invoking encrypt.
Expand Down
105 changes: 64 additions & 41 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,23 +1,68 @@
# cryptr

#### A simple shell utility for encrypting and decrypting files using OpenSSL.
#### An enhanced shell utility for encrypting and decrypting files and directories using OpenSSL.

This is a fork of [cryptr](https://github.com/nodesocket/cryptr) by Justin Keller, with added support for directory encryption.

## Installation

```
git clone https://github.com/nodesocket/cryptr.git
git clone https://github.com/Gu1llaum-3/cryptr.git
ln -s "$PWD"/cryptr/cryptr.bash /usr/local/bin/cryptr
```

### Bash tab completion
### Shell Completion

cryptr supports command completion for both Bash and Zsh shells.

#### Bash Completion

There are two ways to enable bash completion:

1. **Temporary (current session):**
```bash
source tools/cryptr-bash-completion.bash
```

2. **Permanent:**
```bash
# Create completion directory if needed
mkdir -p ~/.bash_completion.d/
# Copy completion file
cp tools/cryptr-bash-completion.bash ~/.bash_completion.d/
# Add to ~/.bashrc or ~/.bash_profile
echo 'source ~/.bash_completion.d/cryptr-bash-completion.bash' >> ~/.bashrc
```

#### Zsh Completion

Similarly for zsh:

1. **Temporary (current session):**
```zsh
source tools/cryptr-zsh-completion.zsh
```

2. **Permanent:**
```zsh
# Create zsh directory if needed
mkdir -p ~/.zsh
# Copy completion file
cp tools/cryptr-zsh-completion.zsh ~/.zsh/
# Add to ~/.zshrc
echo 'source ~/.zsh/cryptr-zsh-completion.zsh' >> ~/.zshrc
```

Add `tools/cryptr-bash-completion.bash` to your tab completion file directory.
After enabling completion, you can use:
- TAB after 'cryptr' to see available commands (encrypt, decrypt, help, version)
- TAB after 'encrypt' to see all files and directories
- TAB after 'decrypt' to see only .aes encrypted files

## API/Commands

### encrypt

> encrypt \<file\> - Encryptes file with OpenSSL AES-256 cipher block chaining. Writes an encrypted file out *(ciphertext)* appending `.aes` extension.
> encrypt \<file|directory\> - Encrypts file or directory with OpenSSL AES-256 cipher block chaining. For files, writes an encrypted file out *(ciphertext)* appending `.aes` extension. For directories, creates a tar.gz archive first, then encrypts it with `.tar.gz.aes` extension.

```
➜ cryptr encrypt ./secret-file
Expand All @@ -26,9 +71,9 @@ Verifying - enter aes-256-cbc encryption password:
```

```
ls -alh
-rw-r--r-- 1 user group 1.0G Oct 1 13:33 secret-file
-rw-r--r-- 1 user group 1.0G Oct 1 13:34 secret-file.aes
cryptr encrypt ./secret-directory
enter aes-256-cbc encryption password:
Verifying - enter aes-256-cbc encryption password:
```

You may optionally define the password to use when encrypting using the `CRYPTR_PASSWORD` environment variable. This enables non-interactive/batch operations.
Expand All @@ -39,22 +84,17 @@ You may optionally define the password to use when encrypting using the `CRYPTR_

### decrypt

> decrypt \<file.aes\> - Decrypt encrypted file using OpenSSL AES-256 cipher block chaining. Writes a decrypted file out *(plaintext)* removing `.aes` extension.

```
➜ ls -alh
-rw-r--r-- 1 user group 1.0G Oct 1 13:34 secret-file.aes
```
> decrypt \<file.aes|directory.tar.gz.aes\> - Decrypt encrypted file or directory using OpenSSL AES-256 cipher block chaining. For files, writes a decrypted file out *(plaintext)* removing `.aes` extension. For directories, removes `.aes` extension and optionally extracts the tar.gz archive.

```
➜ cryptr decrypt ./secret-file.aes
enter aes-256-cbc decryption password:
```

```
ls -alh
-rw-r--r-- 1 user group 1.0G Oct 1 13:35 secret-file
-rw-r--r-- 1 user group 1.0G Oct 1 13:34 secret-file.aes
cryptr decrypt ./secret-directory.tar.gz.aes
enter aes-256-cbc decryption password:
Do you want to extract the decrypted archive? (y/N): y
```

You may optionally define the password to use when decrypting using the `CRYPTR_PASSWORD` environment variable. This enables non-interactive/batch operations.
Expand All @@ -71,11 +111,10 @@ You may optionally define the password to use when decrypting using the `CRYPTR_
➜ cryptr help
Usage: cryptr command <command-specific-options>

encrypt <file> Encrypt file
decrypt <file.aes> Decrypt encrypted file
encrypt <file|directory> Encrypt file or directory
decrypt <file.aes|directory.tar.gz.aes> Decrypt encrypted file or directory
help Displays help
version Displays the current version

```

### version
Expand All @@ -84,33 +123,16 @@ Usage: cryptr command <command-specific-options>

```
➜ cryptr version
cryptr 2.3.0
```

### default

> default - Displays the current version and help

```
➜ cryptr
cryptr 2.3.0

Usage: cryptr command <command-specific-options>

encrypt <file> Encrypt file
decrypt <file.aes> Decrypt encrypted file
help Displays help
version Displays the current version

cryptr 3.0.0
```

## Changelog

https://github.com/nodesocket/cryptr/blob/master/CHANGELOG.md
See CHANGELOG.md

## Support, Bugs, And Feature Requests

Create issues here in GitHub (https://github.com/nodesocket/cryptr/issues).
Create issues here in GitHub (https://github.com/Gu1llaum-3/cryptr/issues).

## Versioning

Expand All @@ -130,7 +152,8 @@ For more information on semantic versioning, visit http://semver.org/.

## License & Legal

Copyright 2024 Justin Keller
Copyright 2024 Guillaume Archambault
Based on cryptr by Justin Keller

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand Down
64 changes: 49 additions & 15 deletions cryptr.bash
100755 → 100644
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
#!/usr/bin/env bash

###############################################################################
# Copyright 2024 Justin Keller
# Copyright 2025 Guillaume Archambault
# Based on cryptr by Justin Keller
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand All @@ -18,7 +19,7 @@

set -eo pipefail; [[ $TRACE ]] && set -x

readonly VERSION="2.3.0"
readonly VERSION="3.0.0"
readonly OPENSSL_CIPHER_TYPE="aes-256-cbc"

cryptr_version() {
Expand All @@ -29,42 +30,61 @@ cryptr_help() {
echo "Usage: cryptr command <command-specific-options>"
echo
cat<<EOF | column -c2 -t -s,
encrypt <file>, Encrypt file
decrypt <file.aes>, Decrypt encrypted file
encrypt <file|directory>, Encrypt file or directory
decrypt <file.aes|directory.tar.gz.aes>, Decrypt encrypted file or directory
help, Displays help
version, Displays the current version
EOF
echo
}

cryptr_encrypt() {
local _file="$1"
if [[ ! -f "$_file" ]]; then
echo "File not found" 1>&2
local _path="$1"
local _is_directory=0
if [[ ! -e "$_path" ]]; then
echo "File or directory not found" 1>&2
exit 4
fi

if [[ -d "$_path" ]]; then
tar -czf "${_path}.tar.gz" -C "$(dirname "$_path")" "$(basename "$_path")"
_path="${_path}.tar.gz"
_is_directory=1
fi

if [[ ! -z "${CRYPTR_PASSWORD}" ]]; then
echo "[notice] using environment variable CRYPTR_PASSWORD for the password"
openssl $OPENSSL_CIPHER_TYPE -salt -pbkdf2 -in "$_file" -out "${_file}.aes" -pass env:CRYPTR_PASSWORD
openssl $OPENSSL_CIPHER_TYPE -salt -pbkdf2 -in "$_path" -out "${_path}.aes" -pass env:CRYPTR_PASSWORD
else
openssl $OPENSSL_CIPHER_TYPE -salt -pbkdf2 -in "$_file" -out "${_file}.aes"
openssl $OPENSSL_CIPHER_TYPE -salt -pbkdf2 -in "$_path" -out "${_path}.aes"
fi

if [[ $? -eq 0 ]]; then
read -rp "do you want to delete the original file? (y/N): " confirm
if [[ "$confirm" =~ ^[Yy]$ ]]; then
echo "[notice] deleting the original file"
rm -f "$_file"
if [[ $_is_directory -eq 1 ]]; then
rm -f "$_path"
fi

if [[ $_is_directory -eq 1 ]]; then
read -p "do you want to delete the original directory? (y/N): " confirm
if [[ "$confirm" =~ ^[Yy]$ ]]; then
echo "[notice] deleting the original directory"
rm -rf "${_path%.tar.gz}"
fi
else
read -p "do you want to delete the original file? (y/N): " confirm
if [[ "$confirm" =~ ^[Yy]$ ]]; then
echo "[notice] deleting the original file"
rm -f "$_path"
fi
fi
else
echo "[error] encryption failed, original file not deleted" 1>&2
echo "[error] encryption failed, original file/directory not deleted" 1>&2
exit 6
fi
}

cryptr_decrypt() {
local _file="$1"
local _file="$1"
if [[ ! -f "$_file" ]]; then
echo "File not found" 1>&2
exit 5
Expand All @@ -76,6 +96,20 @@ local _file="$1"
else
openssl $OPENSSL_CIPHER_TYPE -d -salt -pbkdf2 -in "$_file" -out "${_file%\.aes}"
fi

if [[ "${_file%\.aes}" == *.tar.gz ]]; then
read -p "Do you want to extract the decrypted archive? (y/N): " extract_confirm
if [[ "$extract_confirm" =~ ^[Yy]$ ]]; then
tar -xzf "${_file%\.aes}" -C "$(dirname "${_file%\.aes}")"
echo "[notice] archive extracted"

read -p "Do you want to delete the decrypted tar.gz file? (y/N): " delete_confirm
if [[ "$delete_confirm" =~ ^[Yy]$ ]]; then
rm -f "${_file%\.aes}"
echo "[notice] decrypted tar.gz file deleted"
fi
fi
fi
}

cryptr_main() {
Expand Down
33 changes: 33 additions & 0 deletions tests/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# Testing cryptr

The test suite verifies both file and directory encryption/decryption functionality.

## Running Tests

```bash
# Make the test script executable
chmod +x test.bash

# Run the tests
./test.bash
```

## What is Tested

The test script performs the following checks:

1. File encryption/decryption:
- Creates a random test file
- Encrypts it using cryptr
- Verifies the encrypted file exists
- Decrypts the file
- Verifies data integrity using SHA-256 hash

2. Directory encryption/decryption:
- Creates a test directory with multiple random files
- Encrypts the directory using cryptr
- Verifies the encrypted archive exists
- Decrypts and extracts the directory
- Verifies directory contents integrity using SHA-256 hashes

All temporary files and directories are automatically cleaned up after testing.
Loading