-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
New structure, Mifiel module should include all classes
- Loading branch information
1 parent
322416a
commit 0fea25a
Showing
10 changed files
with
157 additions
and
140 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
module Mifiel::Crypto | ||
autoload :PBE, 'mifiel/crypto/pbe' | ||
autoload :Response, 'mifiel/crypto/response' | ||
autoload :AES, 'mifiel/crypto/aes' | ||
autoload :ECIES, 'mifiel/crypto/ecies' | ||
end | ||
|
||
class String | ||
def bth | ||
self.unpack('H*').first | ||
end | ||
|
||
def htb | ||
Array(self).pack('H*') | ||
end | ||
end | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
require 'openssl' | ||
module Mifiel | ||
module Crypto | ||
class AES | ||
CIPHER = 256 | ||
SIZE = 16 | ||
|
||
def self.random_iv(size = SIZE) | ||
OpenSSL::Random.random_bytes(size) | ||
end | ||
|
||
def self.encrypt(args) | ||
aes = Crypto::AES.new(args[:cipher] || CIPHER) | ||
aes.encrypt(args) | ||
end | ||
|
||
def self.decrypt(args) | ||
aes = Crypto::AES.new(args[:cipher] || CIPHER) | ||
aes.decrypt(args) | ||
end | ||
|
||
attr_accessor :cipher, :require_args | ||
|
||
def initialize(cipher_type = CIPHER) | ||
@require_args = [:key, :iv, :data] | ||
@cipher = OpenSSL::Cipher::AES.new(cipher_type, :CBC) | ||
end | ||
|
||
def random_iv(size = SIZE) | ||
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) | ||
end | ||
|
||
def decrypt(args) | ||
validate_args(args) | ||
cipher.decrypt | ||
cipher.key = args[:key] | ||
cipher.iv = args[:iv] | ||
cipher.update(args[:data]) + cipher.final | ||
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 | ||
end | ||
end | ||
|
||
class Encrypted < Mifiel::Crypto::Response | ||
def to_str | ||
data | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
require 'openssl' | ||
require 'securerandom' | ||
|
||
module Mifiel | ||
module Crypto | ||
class PBE | ||
ALPHA_NUM = ('0'..'9').to_a + ('A'..'Z').to_a + ('a'..'z').to_a | ||
SPECIALS = ['-', '_', '+', '=', '#', '&', '*', '.'].freeze | ||
CHARS = ALPHA_NUM + SPECIALS | ||
|
||
def self.generate | ||
pbe = Crypto::PBE.new | ||
password = pbe.random_password | ||
salt = pbe.random_salt | ||
pbe.derived_key(password, salt) | ||
end | ||
|
||
def initialize(i = 1000) | ||
@iterations = i | ||
@digest = OpenSSL::Digest::SHA256.new | ||
end | ||
|
||
def random_password(length = 32) | ||
CHARS.sort_by { SecureRandom.random_number }.join[0...length] | ||
end | ||
|
||
def random_salt(size = 16) | ||
SecureRandom.random_bytes(size) | ||
end | ||
|
||
def derived_key(password, salt, sizeKey = 24) | ||
args = { | ||
password: password, | ||
salt: salt, | ||
iterations: @iterations, | ||
sizeKey: sizeKey, | ||
digest: @digest | ||
} | ||
PKCS5.new(args) | ||
end | ||
end | ||
|
||
class PKCS5 < Mifiel::Crypto::Response | ||
attr_reader :key | ||
|
||
def initialize(args) | ||
@key = OpenSSL::PKCS5.pbkdf2_hmac(*args.values) | ||
@data = key # key attribute is syntax sugar | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters