Module: Philotic::Serialization::Encryptor
Instance Method Summary collapse
- #content_type ⇒ Object
- #default_algorithm ⇒ Object
- #default_encryption_key ⇒ Object
- #dump(payload, metadata) ⇒ Object
- #key ⇒ Object
- #load(payload, metadata) ⇒ Object
- #random_iv(algorithm = default_algorithm) ⇒ Object
- #random_salt ⇒ Object
- #serialization ⇒ Object
Instance Method Details
#content_type ⇒ Object
11 12 13 |
# File 'lib/philotic/serialization/encryptor.rb', line 11 def content_type 'application/json' end |
#default_algorithm ⇒ Object
27 28 29 |
# File 'lib/philotic/serialization/encryptor.rb', line 27 def default_algorithm 'aes-256-cbc' end |
#default_encryption_key ⇒ Object
19 20 21 |
# File 'lib/philotic/serialization/encryptor.rb', line 19 def default_encryption_key Philotic.config.encryption_key end |
#dump(payload, metadata) ⇒ Object
39 40 41 42 43 44 45 |
# File 'lib/philotic/serialization/encryptor.rb', line 39 def dump(payload, ) [:headers][:encryption] ||= {} algorithm = ([:headers][:encryption][:algorithm] ||= default_algorithm) iv = Base64.decode64([:headers][:encryption][:iv] ||= Base64.encode64(random_iv(algorithm))) salt = Base64.decode64([:headers][:encryption][:salt] ||= Base64.encode64(random_salt)) Base64.encode64 ::Encryptor.encrypt(payload, key: key, iv: iv, salt: salt) end |
#key ⇒ Object
35 36 37 |
# File 'lib/philotic/serialization/encryptor.rb', line 35 def key @key ||= default_encryption_key end |
#load(payload, metadata) ⇒ Object
47 48 49 50 51 52 53 |
# File 'lib/philotic/serialization/encryptor.rb', line 47 def load(payload, ) headers = Marshal.load(Marshal.dump([:headers])).deep_symbolize_keys iv = Base64.decode64 headers[:encryption][:iv] salt = Base64.decode64 headers[:encryption][:salt] ::Encryptor.decrypt(Base64.decode64(payload), key: key, iv: iv, salt: salt) end |
#random_iv(algorithm = default_algorithm) ⇒ Object
31 32 33 |
# File 'lib/philotic/serialization/encryptor.rb', line 31 def random_iv(algorithm = default_algorithm) OpenSSL::Cipher::Cipher.new(algorithm).random_iv end |
#random_salt ⇒ Object
23 24 25 |
# File 'lib/philotic/serialization/encryptor.rb', line 23 def random_salt Base64.encode64 SecureRandom.random_bytes(256) end |
#serialization ⇒ Object
15 16 17 |
# File 'lib/philotic/serialization/encryptor.rb', line 15 def serialization :encrypted end |