Sha256: fca310cf30f860ffa2df0b051d68709adcb408ea24604a0b782de4f538339747

Contents?: true

Size: 1.11 KB

Versions: 2

Compression:

Stored size: 1.11 KB

Contents

require "base64"

module Strongroom
  class Enigma

    def initialize parameters
      @cipher = parameters.fetch :cipher
      @ciphertext = parameters.fetch :ciphertext
      @encrypted_key = parameters.fetch :encrypted_key
      @iv = parameters.fetch :iv
    end

    attr_accessor :cipher, :ciphertext, :encrypted_key, :iv

    def to_s
      keys = [ :ciphertext, :encrypted_key, :iv ]
      "#<Strongroom::Enigma #{cipher} %s>" %
        keys.map { |key| "#{key}: #{send(key).length} bytes" }.join(", ")
    end

    def to_hash
      {
        "cipher" => cipher,
        "ciphertext" => Base64.encode64(ciphertext),
        "encrypted_key" => Base64.encode64(encrypted_key),
        "iv" => Base64.encode64(iv)
      }
    end

    def self.from_hash hash
      new(
        cipher: hash["cipher"],
        ciphertext: Base64.decode64(hash["ciphertext"]),
        encrypted_key: Base64.decode64(hash["encrypted_key"]),
        iv: Base64.decode64(hash["iv"])
      )
    end

    def serialize
      to_hash.to_yaml.encode!("US-ASCII")
    end

    def self.deserialize input
      from_hash YAML.load(input)
    end

  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
strongroom-0.0.2 lib/strongroom/enigma.rb
strongroom-0.0.1 lib/strongroom/enigma.rb