Skip to content

Commit

Permalink
Switch to keyword arguments
Browse files Browse the repository at this point in the history
  • Loading branch information
rubysamurai committed Dec 8, 2015
1 parent 6ce4b1a commit 31de476
Show file tree
Hide file tree
Showing 3 changed files with 109 additions and 75 deletions.
30 changes: 15 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
# Cryptology

[![Gem Version](https://badge.fury.io/rb/cryptology.svg)](http://badge.fury.io/rb/cryptology)
[![Gem Version](https://badge.fury.io/rb/cryptology.svg)](https://badge.fury.io/rb/cryptology)
[![Build Status](https://travis-ci.org/rubysamurai/cryptology.svg?branch=master)](https://travis-ci.org/rubysamurai/cryptology)

`Cryptology` is a wrapper for encryption and decryption in Ruby. Supports all algorithms, that are available in installed version of OpenSSL. By default `AES-256-CBC` is used.
`Cryptology` is a wrapper for encryption and decryption in Ruby. Supports all cipher algorithms, that are available in installed version of OpenSSL. By default `AES-256-CBC` is used.

## Installation

Expand All @@ -25,18 +25,18 @@ $ gem install cryptology

```ruby
# Encrypting
Cryptology.encrypt(data, key, algorithm, iv)
Cryptology.encrypt(data: data, key: key, cipher: cipher, iv: iv)

# Decrypting
Cryptology.decrypt(data, key, algorithm, iv)
Cryptology.decrypt(data: data, key: key, cipher: cipher, iv: iv)
```

Argument | Required? | Default | Comment
----------|-----------|---------------|-------------
data | **Yes** | n/a | Data to encrypt or decrypt
key | **Yes** | n/a | Secure key for encryption and decryption
algorithm | *No* | `AES-256-CBC` | Cipher algorithm
iv | *No* | `nil` | Initialization vector for CBC, CFB, CTR, OFB modes
Argument | Required? | Default | Comment
---------|-----------|---------------|-------------
data | **Yes** | n/a | Data to encrypt or decrypt
key | **Yes** | n/a | Secure key for encryption and decryption
cipher | *No* | `AES-256-CBC` | Cipher algorithm
iv | *No* | `nil` | Initialization vector for CBC, CFB, CTR, OFB modes

Example:

Expand All @@ -45,16 +45,16 @@ Example:
data = 'Very, very confidential data'
# Secure key for encryption (required)
key = 'veryLongAndSecurePassword_6154309'
# Use Blowfish algorithm in CBC mode (optional)
algorithm = 'BF-CBC'
# Use Blowfish cipher in CBC mode (optional)
cipher = 'BF-CBC'
# Initialization vector for BF-CBC (optional)
iv = OpenSSL::Cipher::Cipher.new(algorithm).random_iv
iv = OpenSSL::Cipher::Cipher.new(cipher).random_iv

# Encrypt our data
encrypted = Cryptology.encrypt(data, key, algorithm, iv)
encrypted = Cryptology.encrypt(data: data, key: key, cipher: cipher, iv: iv)

# Decrypt our data
plain = Cryptology.decrypt(encrypted, key, algorithm, iv)
plain = Cryptology.decrypt(data: encrypted, key: key, cipher: cipher, iv: iv)

```

Expand Down
22 changes: 11 additions & 11 deletions lib/cryptology.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,33 +3,33 @@
require 'base64'

module Cryptology
def self.encrypt(data, key, algorithm = 'AES-256-CBC', iv = nil)
encrypted = encrypt_data(data.to_s, digest(key), algorithm, iv)
def self.encrypt(data:, key:, cipher: 'AES-256-CBC', iv: nil)
encrypted = encrypt_data(data.to_s, digest(key), cipher, iv)
::Base64.encode64(encrypted)
end

def self.decrypt(data, key, algorithm = 'AES-256-CBC', iv = nil)
def self.decrypt(data:, key:, cipher: 'AES-256-CBC', iv: nil)
base64_decoded = ::Base64.decode64(data.to_s)
decrypt_data(base64_decoded, digest(key), algorithm, iv)
decrypt_data(base64_decoded, digest(key), cipher, iv)
.force_encoding('UTF-8').encode
end

private

def self.encrypt_data(data, key, algorithm, iv)
cipher = ::OpenSSL::Cipher::Cipher.new(algorithm)
def self.encrypt_data(data, key, cipher, iv)
cipher = ::OpenSSL::Cipher::Cipher.new(cipher)
cipher.encrypt
cipher.key = key
cipher.iv = iv unless iv.nil?
cipher.iv = iv unless iv.nil?
cipher.update(data) + cipher.final
end

def self.decrypt_data(encrypted_data, key, algorithm, iv)
decipher = ::OpenSSL::Cipher::Cipher.new(algorithm)
def self.decrypt_data(data, key, cipher, iv)
decipher = ::OpenSSL::Cipher::Cipher.new(cipher)
decipher.decrypt
decipher.key = key
decipher.iv = iv unless iv.nil?
decipher.update(encrypted_data) + decipher.final
decipher.iv = iv unless iv.nil?
decipher.update(data) + decipher.final
end

def self.digest(key)
Expand Down
132 changes: 83 additions & 49 deletions spec/cryptology_spec.rb
Original file line number Diff line number Diff line change
@@ -1,71 +1,105 @@
require 'spec_helper'

ALGORITHMS = %w(AES-128-CBC
AES-128-CFB
AES-128-CFB1
AES-128-CFB8
AES-128-ECB
AES-128-OFB
CIPHERS = %w(AES-128-CBC
AES-128-CFB
AES-128-CFB1
AES-128-CFB8
AES-128-ECB
AES-128-OFB

AES-192-CBC
AES-192-CFB
AES-192-CFB1
AES-192-CFB8
AES-192-ECB
AES-192-OFB
AES-192-CBC
AES-192-CFB
AES-192-CFB1
AES-192-CFB8
AES-192-ECB
AES-192-OFB

AES-256-CBC
AES-256-CFB
AES-256-CFB1
AES-256-CFB8
AES-256-CTR
AES-256-ECB
AES-256-OFB
AES-256-CBC
AES-256-CFB
AES-256-CFB1
AES-256-CFB8
AES-256-CTR
AES-256-ECB
AES-256-OFB

BF-CBC
BF-CFB
BF-ECB
BF-OFB
BF-CBC
BF-CFB
BF-ECB
BF-OFB

CAMELLIA-256-CBC
CAST5-CBC
DES-CBC
DES-EDE-CBC
DES-EDE3-CBC
DESX-CBC
RC2-CBC
SEED-CBC)
CAMELLIA-256-CBC
CAST5-CBC
DES-CBC
DES-EDE-CBC
DES-EDE3-CBC
DESX-CBC
RC2-CBC
SEED-CBC)

describe Cryptology do
it 'encrypts and decrypts with default arguments' do
data = 'Very confidential data with UTF-8 symbols: ♠ я ü æ'
key = 'veryLongAndSecurePassword_6154309'
encrypted = Cryptology.encrypt(data, key)
expect(Cryptology.decrypt(encrypted, key)).to eq data
encrypted = Cryptology.encrypt(data: data, key: key)
expect(Cryptology.decrypt(data: encrypted, key: key)).to eq data
end

it 'encrypts and decrypts with iv argument' do
data = 'Very confidential data with UTF-8 symbols: ♠ я ü æ'
key = 'veryLongAndSecurePassword_6154309'
iv = OpenSSL::Cipher::Cipher.new('AES-256-CBC').random_iv
encrypted = Cryptology.encrypt(data: data, key: key, iv: iv)
expect(Cryptology.decrypt(data: encrypted, key: key, iv: iv)).to eq data
end

it 'encrypts and decrypts with cipher argument' do
data = 'Very confidential data with UTF-8 symbols: ♠ я ü æ'
key = 'veryLongAndSecurePassword_6154309'
cipher = 'BF-CBC'
encrypted = Cryptology.encrypt(data: data, key: key, cipher: cipher)
expect(Cryptology.decrypt(data: encrypted, key: key, cipher: cipher))
.to eq data
end

it 'throws error without data argument' do
key = 'veryLongAndSecurePassword_6154309'
cipher = 'AES-256-CBC'
iv = OpenSSL::Cipher::Cipher.new('AES-256-CBC').random_iv
expect { Cryptology.encrypt(key: key, cipher: cipher, iv: iv) }
.to raise_error(ArgumentError, 'missing keyword: data')
end

it 'throws error without key argument' do
data = 'Very confidential data with UTF-8 symbols: ♠ я ü æ'
cipher = 'AES-256-CBC'
iv = OpenSSL::Cipher::Cipher.new('AES-256-CBC').random_iv
expect { Cryptology.encrypt(data: data, cipher: cipher, iv: iv) }
.to raise_error(ArgumentError, 'missing keyword: key')
end

context 'encryption' do
ALGORITHMS.each do |alg|
it "encrypts #{alg}" do
algorithm = alg
data = 'Very, very confidential data'
key = OpenSSL::Cipher::Cipher.new(algorithm).random_key
iv = OpenSSL::Cipher::Cipher.new(algorithm).random_iv
expect { Cryptology.encrypt(data, key, algorithm, iv) }
.not_to raise_error
CIPHERS.each do |c|
it "encrypts #{c}" do
data = 'Very, very confidential data'
key = OpenSSL::Cipher::Cipher.new(c).random_key
iv = OpenSSL::Cipher::Cipher.new(c).random_iv
expect {
Cryptology.encrypt(data: data, key: key, cipher: c, iv: iv)
}.not_to raise_error
end
end
end

context 'decryption' do
ALGORITHMS.each do |alg|
it "decrypts #{alg}" do
algorithm = alg
data = 'Very confidential data with UTF-8 symbols: ♠ я ü æ'
key = OpenSSL::Cipher::Cipher.new(algorithm).random_key
iv = OpenSSL::Cipher::Cipher.new(algorithm).random_iv
encrypted = Cryptology.encrypt(data, key, algorithm, iv)
expect(Cryptology.decrypt(encrypted, key, algorithm, iv)).to eq data
CIPHERS.each do |c|
it "decrypts #{c}" do
data = 'Very confidential data with UTF-8 symbols: ♠ я ü æ'
key = OpenSSL::Cipher::Cipher.new(c).random_key
iv = OpenSSL::Cipher::Cipher.new(c).random_iv
encrypted = Cryptology.encrypt(data: data, key: key, cipher: c, iv: iv)
expect(
Cryptology.decrypt(data: encrypted, key: key, cipher: c, iv: iv)
).to eq data
end
end
end
Expand Down

0 comments on commit 31de476

Please sign in to comment.