cryp

package module
v0.0.0-...-76ba4fe Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 28, 2015 License: MIT Imports: 19 Imported by: 12

README

#cryp

** this repo is undergoing active development. use at your own risk **

cryp is a small set of encryption/decryption tools to make it easy to encrypt files and command line data.

The goal of this project is to provide a simple way to store secrets in code repositories and work with sensitive data.

cryp is written using only the go stdlib and the official golang scrypt package. The code is approachable and tested.

##How does it work?

cryp consists of 6 command line programs:

  • enc - Reads from STDIN and outputs base64 encoded data to STDOUT
  • dec - Reads base64 encoded data from STDIN and outputs decrypted data to STDOUT
  • enc_file - Takes a list of file paths and creates a new, encrypted, version with the file name, size, mode, mod time and contents written in tar forma and encrypted as a single payload. The encrypted file is named the HMAC SHA-256 hash of it's contents.
  • dec_file - Takes a list of file paths and decrypts them, parsing the tar format and creating a file with the original properties of the encrypted version.
  • enc_dir - Takes a list of directories and recursively replaces each file with an encrypted version using enc_file.
  • dec_dir - Takes a list of directories and recursively decrypts each file having a file name that is the HMAC SHA-256 checksum of it's contents using dec_file.

cryp is also presented as a library for developing your own tools with. Please visit godocs for complete documentation.

Each program expects CRYP_KEY to be set in the environment of the current shell. It uses this key to encrypt/decrypt and will fail loudly if it is not present.

jason@mba ~ enc
2015/08/27 17:30:49 CRYP_KEY not set in environment

###Examples

jason@mbp ~ export CRYP_KEY=$MY_WORK_CRYP_KEY

jason@mbp ~ echo "hi" | enc
O98uYJbb5Q8ehQO4ckWB9rfEPdM1BAhS/OtLrvPKHPJMI9Tu8QTkjsBk7VGTZIaybbTvun7qrwvpbSh7mRtY7Iwq3Std+fRMGBaIUOI=

jason@mba ~ echo "hi" | enc | dec
hi

# create some secrets
jason@mba ~ : mkdir secrets
jason@mba ~ : touch secrets/{this,that,theother}
jason@mba ~ : find secrets
secrets
secrets/that
secrets/theother
secrets/this

# encrypt them
jason@mba ~ : enc_dir secrets/
Encrypting secrets/that ... 10.479019ms
Encrypting secrets/theother ... 1.160841ms
Encrypting secrets/this ... 1.990103ms

# see they are encrypted
jason@mba ~ : find secrets/
secrets
secrets/090df7a71a2a0141183e7441ed60b7586f33a4679e1a81d69e68ca1e40751c4a
secrets/58e264fe56cfe3e9351bb8a76f6f408ecc67db5a7d0efeb4a1e4c9df860fdd7d
secrets/948d14afcda7775d709691c449724004fc3c73c27a2d736e95fe2a4a6e922328

# decrypt them
jason@mba ~ : dec_dir secrets/
Decrypting secrets/090df7a71a2a0141183e7441ed60b7586f33a4679e1a81d69e68ca1e40751c4a ... 446.787µs
Decrypting secrets/58e264fe56cfe3e9351bb8a76f6f408ecc67db5a7d0efeb4a1e4c9df860fdd7d ... 195.358µs
Decrypting secrets/948d14afcda7775d709691c449724004fc3c73c27a2d736e95fe2a4a6e922328 ... 249.634µs

# see they are decrypted
jason@mba ~ : find secrets/
secrets
secrets/that
secrets/theother
secrets/this

##How do I use it?

###Setup

First set your CRYP_KEY variable. It can be any length or even be empty (not recommended):

# here's a simple way to generate and set a random one
export CRYP_KEY=$(base64 < /dev/urandom | head -c 128)
echo -e "\nexport CRYP_KEY='$CRYP_KEY'" >> ~/.profile

# you can also set it to a password you can remember

YOU MUST SAVE YOUR CRYP_KEY OR YOU WILL NOT BE ABLE TO DECRYPT YOUR DATA

^^^SUPER IMPORTANT^^^

###Install

Go 1.5 is required to build the tools. Install instructions for go are here.

go get -v github.com/jasonmoo/cryp/...

That's it.

##The details of the encryption processes

Encryption uses AES-256 CFB encryption with a few extra steps. The code is clear and readable and should reflect the following outline:

Encryption

  1. Create AES-256 key to encrypt with by taking scrypt hash of the CRYP_KEY.
  2. Gzip data
  3. Encrypt payload using AES-256 CFB with generated key

Decryption

  1. Create AES-256 key to encrypt with by taking scrypt hash of the CRYP_KEY.
  2. Decrypt payload using AES-256 CFB with generated key
  3. Gunzip data

License

This software is released under the MIT License (2015). As such it is free to use and do with as you like. Any data destroyed, maliciously or accidentally, while using this software is not the responsibility of the author. Please be careful.

Documentation

Index

Constants

View Source
const SignatureSize = sha256.Size * 2 // hex encoded SHA-256

Variables

View Source
var LogOutput io.Writer = os.Stdout

Common actions are printed to stdout. These can be silenced by setting LogOutput = ioutil.Discard

Functions

func Decrypt

func Decrypt(data []byte, sig string, key []byte) ([]byte, error)

Decrypt takes data and a key and outputs decrypted data and any possible errors The key can be any length or empty (not recommended). A SHA-512/256 key is generated from the supplied key ensuring the 32 byte AES-256 key length requirement is met. Once decrypted, the data is decompressed using gzip.

func DecryptDirFiles

func DecryptDirFiles(dir string, key []byte) error

DecryptDirFiles takes a directory and a key, and searches recursively, for any files that are named a SHA-256 checksum to decrypt. It passes each file to DecryptFile, and removes the encrypted original. Any files that do not match the SHA-256 checksum are left as-is.

func DecryptFile

func DecryptFile(path string, key []byte) (string, error)

DecryptFile ensures the file name (SHA-256 checksum of contents) matches the checksum of the contents and passes the file contents to Decrypt. It parses the decrypted tar payload and attempts to restore the file to its original form (name, contents, mode, mod time) It returns the original file path and error if occurred

func Encrypt

func Encrypt(data []byte, key []byte) ([]byte, string, error)

Encrypt takes data and a key and outputs encrypted data, it's HMAC SHA-256 signature and any possible errors. The key can be any length or empty (not recommended). The data can be any length or empty. Scrypt for the 32 byte AES-256 key derivation. The data is compressed using gzip prior to encryption. Raw byte output will need to be hex/base64 encoded before it is printable.

func EncryptDirFiles

func EncryptDirFiles(dir string, key []byte) error

EncryptDirFiles takes a directory and a key and searches, recursively, for any files to encrypt and passes it to EncryptFile, replacing the existing file with the new encrypted version. All directories, symlinks, named pipes, sockets, and devices are left as-is.

func EncryptFile

func EncryptFile(path string, key []byte) (string, error)

EncryptFile writes a file's name, size, mode, mod time, and contents in tar format and passes it to Encrypt. A new file is created that is named the SHA-256 checksum of the encrypted output. It returns the new file path and error if occurred

Types

This section is empty.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL