Module: Bixby::CryptoUtil

Defined in:
lib/bixby_common/util/crypto_util.rb

Class Method Summary collapse

Class Method Details

.decrypt(data, key_pem, iv_pem) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/bixby_common/util/crypto_util.rb', line 25

def decrypt(data, key_pem, iv_pem)
  data = StringIO.new(data, 'rb')
  key = key_pem.private_decrypt(read_next(data))
  iv  = iv_pem.public_decrypt(read_next(data))

  c = new_cipher()
  c.decrypt
  c.key = key
  c.iv = iv

  ret = c.update(d64(data.read)) + c.final
end

.encrypt(data, key_pem, iv_pem) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/bixby_common/util/crypto_util.rb', line 10

def encrypt(data, key_pem, iv_pem)
  c = new_cipher()
  c.encrypt
  key = c.random_key
  iv = c.random_iv
  encrypted = c.update(data) + c.final

  out = []
  out << w( key_pem.public_encrypt(key) )
  out << w( iv_pem.private_encrypt(iv) )
  out << e64(encrypted)

  return out.join("\n")
end