class ConfidentialInfoManager::Core
Condidential info manager core class @author tatsunori nishikori <tora.1986.tatsu@gmail.com>
Constants
- DEFAULT_ALGORITHM
- ITERATOR_COUNT
- RANDOM_BYTES
Public Class Methods
new(password, mode = DEFAULT_ALGORITHM, iterator_cnt = ITERATOR_COUNT)
click to toggle source
constructor @param [String] password @param [String] salt @param [String] mode @see docs.ruby-lang.org/en/2.2.0/OpenSSL/Cipher.html
# File lib/confidential_info_manager/core.rb, line 20 def initialize(password, mode = DEFAULT_ALGORITHM, iterator_cnt = ITERATOR_COUNT) raise ArgmentError.new("Password is empty") if password.empty? raise ArgmentError.new("Mode is empty") if mode.empty? raise ArgmentError.new("You must specify an integer of 1 or more") if iterator_cnt <= 0 @iterator_cnt = iterator_cnt @password = password @mode = mode end
Public Instance Methods
decrypt(encrypted_data, type = String)
click to toggle source
decrypt @param [String] encrypted data @param [Class] type
@note String/Fixnum/Bignum/Float/Array/Hash
@return [Object] decrypted data
# File lib/confidential_info_manager/core.rb, line 59 def decrypt(encrypted_data, type = String) encrypted_data = Base64.strict_decode64(encrypted_data) salt = encrypted_data[8, RANDOM_BYTES] encrypted_data = encrypted_data[8 + RANDOM_BYTES, encrypted_data.size] decrypter = generate_cipher decrypter.decrypt decrypter.pkcs5_keyivgen(@password, salt, @iterator_cnt) decrypted_data = "" decrypted_data << decrypter.update(encrypted_data) decrypted_data << decrypter.final if type == Fixnum || type == Bignum decrypted_data = decrypted_data.to_i elsif type == Float decrypted_data = decrypted_data.to_f elsif type == Array || type == Hash decrypted_data = Marshal.load(decrypted_data) end decrypted_data end
decrypt_only_value(encrypted_data)
click to toggle source
decrypt only value @param [Object] encrypted_data
@note Object is allowed an Hash or Array
@return [Object] decrypted data
@note Array/Hash
# File lib/confidential_info_manager/core.rb, line 105 def decrypt_only_value(encrypted_data) case encrypted_data when Hash Hash[encrypted_data.map { |key, val| [key, decrypt(val)] }] when Array encrypted_data.map { |val| decrypt(val) } else decrypt(encrypted_data) end end
encrypt(secret_data)
click to toggle source
encrypt @param [Object] secret data @return [String] encrypted data
# File lib/confidential_info_manager/core.rb, line 34 def encrypt(secret_data) # convert string case secret_data when Numeric secret_data = secret_data.to_s when Hash, Array secret_data = Marshal.dump(secret_data) end salt = OpenSSL::Random.random_bytes(RANDOM_BYTES) encrypter = generate_cipher encrypter.encrypt encrypter.pkcs5_keyivgen(@password, salt, @iterator_cnt) encrypted_data = "" encrypted_data << encrypter.update(secret_data) encrypted_data << encrypter.final Base64.strict_encode64("Salted__#{salt}#{encrypted_data}") end
encrypt_only_value(secret_data)
click to toggle source
encrypt only value @param [Object] secret_data
@note Object is allowed an Hash or Array
@return [Object] encrypted data
@note Array/Hash
# File lib/confidential_info_manager/core.rb, line 88 def encrypt_only_value(secret_data) case secret_data when Hash Hash[secret_data.map { |key, val| [key, encrypt(val)] }] when Array secret_data.map { |val| encrypt(val) } else encrypt(secret_data) end end
Private Instance Methods
generate_cipher()
click to toggle source
generate cipher instance @return [OpenSSL::Cipher] cipher
# File lib/confidential_info_manager/core.rb, line 121 def generate_cipher cipher = OpenSSL::Cipher.new(@mode) cipher.reset end