Class: MyOpenSSL
- Inherits:
-
AbstractSymmetricCrypt
- Object
- AbstractSymmetricCrypt
- MyOpenSSL
- Defined in:
- lib/ruby-common/v5/MyOpenSSL.rb
Constant Summary collapse
- METHOD =
0x0200
Instance Method Summary collapse
- #decode(input, key, iv) ⇒ Object
- #decrypt_with_key(payload, key) ⇒ Object
-
#initialize(crypt_method: "AES-256-CBC") ⇒ MyOpenSSL
constructor
A new instance of MyOpenSSL.
Methods inherited from AbstractSymmetricCrypt
Constructor Details
#initialize(crypt_method: "AES-256-CBC") ⇒ MyOpenSSL
Returns a new instance of MyOpenSSL.
8 9 10 11 12 13 14 15 |
# File 'lib/ruby-common/v5/MyOpenSSL.rb', line 8 def initialize(crypt_method: "AES-256-CBC") if CryptMethodConstans::CRYPT_METHODS.key?(crypt_method) @crypt_method = crypt_method @crypt_iv = CryptMethodConstans::CRYPT_METHODS[crypt_method] else raise DecryptError, "Method not supported #{crypt_method}" end end |
Instance Method Details
#decode(input, key, iv) ⇒ Object
26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/ruby-common/v5/MyOpenSSL.rb', line 26 def decode(input, key, iv) begin cipher = OpenSSL::Cipher.new(@crypt_method) cipher.decrypt cipher.key = key.bytes.pack('C*') cipher.iv = iv.bytes.pack('C*') return cipher.update(input) + cipher.final rescue StandardError => e raise DecryptError, "Decryption OpenSSL failed: #{e.}" end end |
#decrypt_with_key(payload, key) ⇒ Object
17 18 19 20 21 22 23 24 |
# File 'lib/ruby-common/v5/MyOpenSSL.rb', line 17 def decrypt_with_key(payload, key) lengths = {"iv" => @crypt_iv} result = parse(payload, lengths) raise DecryptError, 'Unrecognized payload' if result.method != METHOD; return decode(payload, key, result.byte_buffer_map['iv']) end |