Class: Pokepay::Crypto

Inherits:
Object
  • Object
show all
Defined in:
lib/pokepay_partner_ruby_sdk/crypto.rb

Instance Method Summary collapse

Constructor Details

#initialize(key) ⇒ Crypto

Returns a new instance of Crypto.



9
10
11
12
# File 'lib/pokepay_partner_ruby_sdk/crypto.rb', line 9

def initialize(key)
  @key = Base64.urlsafe_decode64(key).force_encoding("utf-8")
  @cipher = OpenSSL::Cipher.new('AES-256-CBC')
end

Instance Method Details

#decrypt(ciphertext) ⇒ Object



23
24
25
26
27
28
29
30
# File 'lib/pokepay_partner_ruby_sdk/crypto.rb', line 23

def decrypt(ciphertext)
  dec = @cipher
  dec.decrypt
  dec.key = @key
  dec.iv  = ciphertext.byteslice(0,16)
  decrypted_text = dec.update(ciphertext) + dec.final
  decrypted_text.byteslice(16, decrypted_text.bytesize - 16)
end

#encrypt(plaintext) ⇒ Object



14
15
16
17
18
19
20
21
# File 'lib/pokepay_partner_ruby_sdk/crypto.rb', line 14

def encrypt(plaintext)
  enc = @cipher
  enc.encrypt
  enc.key = @key
  enc.padding = 1
  enc.iv = SecureRandom.random_bytes(16)
  enc.update('0'*16 + plaintext) + enc.final
end