Sha256: ceb669fc142323a2e85712d02d0c3fa103bb5c24b132859c2d228043ad61d14b

Contents?: true

Size: 1.06 KB

Versions: 1

Compression:

Stored size: 1.06 KB

Contents

require 'openssl'
require 'base64'
require 'json'
require 'parsel/marshal'
require 'parsel/json'
require 'parsel/version'

module Parsel
  DEFAULT_IV = 'f89209ffcdd1a225'.freeze
  CIPHER = 'AES-256-CBC'.freeze

  def self.default_iv=(iv)
    @default_iv = iv
  end

  def self.default_iv
    @default_iv
  end

  self.default_iv = DEFAULT_IV

  def self.encrypt(*args)
    encode cipher(:encrypt, *expand_args(args))
  end

  def self.decrypt(*args)
    key, iv, data = expand_args(args)
    cipher(:decrypt, key, iv, decode(data))
  rescue Exception
    false
  end

  def self.expand_args(args)
    if args.size == 2
      iv = default_iv
      key, data = args
    else
      key, iv, data = args
    end

    [key, iv, data]
  end

  private
  def self.cipher(mode, key, iv, data)
    cipher = OpenSSL::Cipher.new(CIPHER).public_send(mode)
    cipher.key = Digest::SHA256.digest(key)
    cipher.iv = iv
    cipher.update(data) + cipher.final
  end

  def self.encode(data)
    Base64.encode64(data).gsub(/\n/, '')
  end

  def self.decode(data)
    Base64.decode64(data)
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
parsel-0.3.0 lib/parsel.rb