Skip to content

Commit

Permalink
Refactor in AES class and adaptation to new mifiel standar
Browse files Browse the repository at this point in the history
  • Loading branch information
aldosolorzano committed Dec 4, 2018
1 parent 0fea25a commit c2c3a9a
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 56 deletions.
15 changes: 8 additions & 7 deletions lib/mifiel/crypto.rb
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
module Mifiel::Crypto
autoload :PBE, 'mifiel/crypto/pbe'
autoload :Response, 'mifiel/crypto/response'
autoload :AES, 'mifiel/crypto/aes'
autoload :ECIES, 'mifiel/crypto/ecies'
module Mifiel
module Crypto
autoload :PBE, 'mifiel/crypto/pbe'
autoload :Response, 'mifiel/crypto/response'
autoload :AES, 'mifiel/crypto/aes'
autoload :ECIES, 'mifiel/crypto/ecies'
end
end

class String
def bth
self.unpack('H*').first
unpack('H*').first
end

def htb
Array(self).pack('H*')
end
end

44 changes: 17 additions & 27 deletions lib/mifiel/crypto/aes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,15 @@ def self.random_iv(size = SIZE)
OpenSSL::Random.random_bytes(size)
end

def self.encrypt(args)
aes = Crypto::AES.new(args[:cipher] || CIPHER)
def self.encrypt(cipher: CIPHER, key: nil, iv: nil, data: nil)
aes = Mifiel::Crypto::AES.new(cipher)
args = { cipher: cipher, key: key, iv: iv, data: data }
aes.encrypt(args)
end

def self.decrypt(args)
aes = Crypto::AES.new(args[:cipher] || CIPHER)
def self.decrypt(cipher: CIPHER, key: nil, iv: nil, data: nil)
aes = Mifiel::Crypto::AES.new(cipher)
args = { cipher: cipher, key: key, iv: iv, data: data }
aes.decrypt(args)
end

Expand All @@ -27,35 +29,23 @@ def initialize(cipher_type = CIPHER)
end

def random_iv(size = SIZE)
Crypto::AES.random_iv(size)
Mifiel::Crypto::AES.random_iv(size)
end

def encrypt(args)
validate_args(args)
cipher.encrypt
cipher.key = args[:key]
cipher.iv = args[:iv]
encrypted_data = cipher.update(args[:data]) + cipher.final
Encrypted.new(encrypted_data)
def encrypt(key: nil, iv: nil, data: nil)
iv ||= random_iv
Encrypted.new(cipher_final(key, iv, data, action: :encrypt))
end

def decrypt(args)
validate_args(args)
cipher.decrypt
cipher.key = args[:key]
cipher.iv = args[:iv]
cipher.update(args[:data]) + cipher.final
def decrypt(key: nil, iv: nil, data: nil)
cipher_final(key, iv, data, action: :decrypt)
end

private

def validate_args(args)
keys = args.keys
require_args.each do |a|
unless keys.include?(a)
raise ArgumentError, "Expected keys #{require_args}"
end
end
def cipher_final(key, iv, message, action: :encrypt)
@cipher.send(action)
@cipher.iv = iv
@cipher.key = key
@cipher.update(message) + @cipher.final
end
end

Expand Down
22 changes: 11 additions & 11 deletions lib/mifiel/crypto/response.rb
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
module Mifiel
module Crypto
class Response
attr_reader :data
class Response
attr_reader :data

def ==(other)
data == other.data
end
def ==(other)
data == other.data
end

def initialize(data)
@data = data
end
def initialize(data)
@data = data
end

def to_hex
data.unpack('H*').first
def to_hex
data.unpack('H*').first
end
end
end
end
end
11 changes: 0 additions & 11 deletions spec/mifiel/crypto/aes_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,4 @@
end
end
end

describe '#AES bad' do
describe 'ArgumentError, sending wrong data to decrypt' do
args = { data: 'bad-data', iv: Mifiel::Crypto::AES.random_iv, key: 'this-aSecure-key' }
let(:expected_error) { "Expected keys #{Mifiel::Crypto::AES.new.require_args}" }
it 'should raise expected error' do
args = {}
expect { Mifiel::Crypto::AES.decrypt(args) }.to raise_error(ArgumentError, expected_error)
end
end
end
end

0 comments on commit c2c3a9a

Please sign in to comment.