Class: Strongroom::Enigma

Inherits:
Object
  • Object
show all
Defined in:
lib/strongroom/enigma.rb

Constant Summary collapse

BINARY =

“ASCII-8BIT” is Ruby’s “BINARY” type:

Encoding.aliases["BINARY"] # => "ASCII-8BIT"
"ASCII-8BIT"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(parameters) ⇒ Enigma

Returns a new instance of Enigma.



11
12
13
14
15
16
# File 'lib/strongroom/enigma.rb', line 11

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

Instance Attribute Details

#cipherObject

Returns the value of attribute cipher.



18
19
20
# File 'lib/strongroom/enigma.rb', line 18

def cipher
  @cipher
end

#ciphertextObject

Returns the value of attribute ciphertext.



18
19
20
# File 'lib/strongroom/enigma.rb', line 18

def ciphertext
  @ciphertext
end

#encrypted_keyObject

Returns the value of attribute encrypted_key.



18
19
20
# File 'lib/strongroom/enigma.rb', line 18

def encrypted_key
  @encrypted_key
end

#ivObject

Returns the value of attribute iv.



18
19
20
# File 'lib/strongroom/enigma.rb', line 18

def iv
  @iv
end

Class Method Details

.deserialize(input) ⇒ Object



47
48
49
# File 'lib/strongroom/enigma.rb', line 47

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

.from_hash(hash) ⇒ Object



34
35
36
37
38
39
40
41
# File 'lib/strongroom/enigma.rb', line 34

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

Instance Method Details

#serializeObject



43
44
45
# File 'lib/strongroom/enigma.rb', line 43

def serialize
  YAML.dump(to_hash).force_encoding(BINARY)
end

#to_hashObject



26
27
28
29
30
31
32
# File 'lib/strongroom/enigma.rb', line 26

def to_hash
  Hash[
    to_hash_with_default_encoding.map do |key, value|
      [key.dup.force_encoding(BINARY), value.force_encoding(BINARY)]
    end
  ]
end

#to_sObject



20
21
22
23
24
# File 'lib/strongroom/enigma.rb', line 20

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