Skip to content
This repository was archived by the owner on Jan 9, 2018. It is now read-only.
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
1 change: 1 addition & 0 deletions aescrypt.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,5 @@ Gem::Specification.new do |gem|
gem.version = "1.0.1"

gem.add_development_dependency "rake"
gem.add_development_dependency "rspec"
end
30 changes: 15 additions & 15 deletions lib/aescrypt.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,20 @@
# and have been included with prior permission.
#
# Copyright (c) 2012 Gurpartap Singh
#
#
# MIT License
#
#
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and associated documentation files (the
# "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so, subject to
# the following conditions:
#
#
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
#
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
Expand All @@ -44,40 +44,40 @@ def self.key_digest(password)
end

# Decrypts a block of data (encrypted_data) given an encryption key
# and an initialization vector (iv). Keys, iv's, and the data
# and an initialization vector (iv). Keys, iv's, and the data
# returned are all binary strings. Cipher_type should be
# "AES-256-CBC", "AES-256-ECB", or any of the cipher types
# supported by OpenSSL. Pass nil for the iv if the encryption type
# doesn't use iv's (like ECB).
#:return: => String
#:arg: encrypted_data => String
#:arg: encrypted_data => String
#:arg: key => String
#:arg: iv => String
#:arg: cipher_type => String
def self.decrypt_data(encrypted_data, key, iv, cipher_type)
aes = OpenSSL::Cipher::Cipher.new(cipher_type)
aes = OpenSSL::Cipher.new(cipher_type)
aes.decrypt
aes.key = key
aes.iv = iv if iv != nil
aes.update(encrypted_data) + aes.final
aes.update(encrypted_data) + aes.final
end

# Encrypts a block of data given an encryption key and an
# initialization vector (iv). Keys, iv's, and the data returned
# Encrypts a block of data given an encryption key and an
# initialization vector (iv). Keys, iv's, and the data returned
# are all binary strings. Cipher_type should be "AES-256-CBC",
# "AES-256-ECB", or any of the cipher types supported by OpenSSL.
# "AES-256-ECB", or any of the cipher types supported by OpenSSL.
# Pass nil for the iv if the encryption type doesn't use iv's (like
# ECB).
#:return: => String
#:arg: data => String
#:arg: data => String
#:arg: key => String
#:arg: iv => String
#:arg: cipher_type => String
#:arg: cipher_type => String
def self.encrypt_data(data, key, iv, cipher_type)
aes = OpenSSL::Cipher::Cipher.new(cipher_type)
aes = OpenSSL::Cipher.new(cipher_type)
aes.encrypt
aes.key = key
aes.iv = iv if iv != nil
aes.update(data) + aes.final
aes.update(data) + aes.final
end
end
23 changes: 23 additions & 0 deletions spec/aescrypt_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
require_relative '../lib/aescrypt'

describe AESCrypt do
describe '.encrypt(message, password)' do
it 'returns an encrypted string' do
expect(described_class.encrypt('some secret', 'password')).to_not eq 'some secret'
end
end

describe '.decrypt(message, password)' do
it 'can decrypt strings encrypted with the same password' do
encrypted_string = described_class.encrypt('some secret', 'password')
expect(described_class.decrypt(encrypted_string, 'password')).to eq 'some secret'
end

it 'cannot decrypt strings encrypted with a different password' do
encrypted_string = described_class.encrypt('some secret', 'password')
expect {
described_class.decrypt(encrypted_string, 'bad password')
}.to raise_error OpenSSL::Cipher::CipherError
end
end
end