Sha256: 4f5a511ff63cdfecc6212f4af04a8740c45833fe7e98a72ae885c815dcd9e716

Contents?: true

Size: 1.05 KB

Versions: 2

Compression:

Stored size: 1.05 KB

Contents

require 'cider_ci/open_session/encoder'
require 'openssl'
require 'digest/sha1'
require 'digest/sha2'
require 'json'

module CiderCi
  module OpenSession
    module Encryptor
      include CiderCi::OpenSession::Encoder
      extend self

      def decrypt(secret, encrypted_message)
        cipher = create_cipher :decrypt, secret
        iv, encrypted_data = encrypted_message.split('~').map { |m| decode(m) }
        cipher.iv = iv
        decrypted_data = cipher.update(encrypted_data)
        decrypted_data << cipher.final
        JSON.parse(decrypted_data)
      end

      def encrypt(secret, message_object)
        cipher = create_cipher :encrypt, secret
        iv = cipher.random_iv
        encrypted_data = cipher.update(message_object.to_json)
        encrypted_data << cipher.final
        "#{encode iv}~#{encode encrypted_data}"
      end

      private

      def create_cipher(mode, secret)
        OpenSSL::Cipher::Cipher.new('aes-256-cbc').tap do |c|
          c.send mode
          c.key = Digest::SHA256.digest(secret)
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 2 rubygems

Version Path
cider_ci-open_session-1.0.0 lib/cider_ci/open_session/encryptor.rb
cider_ci-open_session-rails-1.0.0 lib/cider_ci/open_session/encryptor.rb