module Sinopac::FunBiz::Message

Public Class Methods

decrypt(content:, key:, iv:) click to toggle source
# File lib/sinopac/funbiz/message.rb, line 21
def self.decrypt(content:, key:, iv:)
  cipher = OpenSSL::Cipher.new('AES-256-CBC')
  cipher.decrypt
  cipher.key = key
  cipher.iv = iv

  decrypted_message = cipher.update([content].pack('H*')) + cipher.final
  JSON.parse(decrypted_message, { symbolize_names: true })
end
encrypt(content:, key:, iv:) click to toggle source
# File lib/sinopac/funbiz/message.rb, line 11
def self.encrypt(content:, key:, iv:)
  cipher = OpenSSL::Cipher.new('AES-256-CBC')
  cipher.encrypt
  cipher.key = key
  cipher.iv = iv

  encrypted_message = cipher.update(content.to_json) + cipher.final
  encrypted_message.unpack('H*').first.upcase
end
iv(nonce:) click to toggle source
# File lib/sinopac/funbiz/message.rb, line 7
def self.iv(nonce:)
  Digest::SHA256.hexdigest(nonce).upcase[-16..]
end