Class: ConfidentialInfoManager::Core
- Inherits:
-
Object
- Object
- ConfidentialInfoManager::Core
- Defined in:
- lib/confidential_info_manager/core.rb
Overview
Condidential info manager core class
Direct Known Subclasses
Constant Summary collapse
- RANDOM_BYTES =
8.freeze
- ITERATOR_COUNT =
2000.freeze
Class Method Summary collapse
-
.generate_salt(length = RANDOM_BYTES) ⇒ String
generate salt.
Instance Method Summary collapse
-
#decrypt(encrypted_data, type = String) ⇒ Object
decrypt.
-
#decrypt_only_value(encrypted_data) ⇒ Object
decrypt only value.
-
#encrypt(secret_data) ⇒ String
encrypt.
-
#encrypt_only_value(secret_data) ⇒ Object
encrypt only value.
-
#initialize(password, salt, mode = "AES-256-CBC") ⇒ Core
constructor
constructor.
Constructor Details
#initialize(password, salt, mode = "AES-256-CBC") ⇒ Core
constructor
19 20 21 22 23 |
# File 'lib/confidential_info_manager/core.rb', line 19 def initialize(password, salt, mode="AES-256-CBC") generate_encrypter(mode) generate_decrypter(mode) set_key_and_iv(password, salt) end |
Class Method Details
.generate_salt(length = RANDOM_BYTES) ⇒ String
generate salt
29 30 31 |
# File 'lib/confidential_info_manager/core.rb', line 29 def self.generate_salt(length = RANDOM_BYTES) OpenSSL::Random.random_bytes(length) end |
Instance Method Details
#decrypt(encrypted_data, type = String) ⇒ Object
decrypt
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/confidential_info_manager/core.rb', line 60 def decrypt(encrypted_data, type=String) @@decrypter.reset encrypted_data = Base64.strict_decode64(encrypted_data) 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) ⇒ Object
decrypt only value
101 102 103 104 105 106 107 108 109 110 |
# File 'lib/confidential_info_manager/core.rb', line 101 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) ⇒ String
encrypt
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/confidential_info_manager/core.rb', line 37 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 @@encrypter.reset encrypted_data = "" encrypted_data << @@encrypter.update(secret_data) encrypted_data << @@encrypter.final Base64.strict_encode64(encrypted_data) end |
#encrypt_only_value(secret_data) ⇒ Object
encrypt only value
84 85 86 87 88 89 90 91 92 93 |
# File 'lib/confidential_info_manager/core.rb', line 84 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 |