Class: MyOpenSSL

Inherits:
AbstractSymmetricCrypt show all
Defined in:
lib/ruby-common/v5/MyOpenSSL.rb

Constant Summary collapse

METHOD =
0x0200

Instance Method Summary collapse

Methods inherited from AbstractSymmetricCrypt

#parse

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.message}"
  end
end

#decrypt_with_key(payload, key) ⇒ Object

Raises:



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