Class: RightScale::MessageEncoder::SecretSerializer

Inherits:
Object
  • Object
show all
Defined in:
lib/instance/message_encoder.rb

Instance Method Summary collapse

Constructor Details

#initialize(serializer, identity, secret) ⇒ SecretSerializer

Returns a new instance of SecretSerializer.



37
38
39
40
41
# File 'lib/instance/message_encoder.rb', line 37

def initialize(serializer, identity, secret)
  @serializer = serializer
  @identity = identity
  @secret = secret
end

Instance Method Details

#dump(data) ⇒ String

Encodes the given serializable object to text.

Parameters:

  • data (Object)

    in form of any serializable object

Returns:

  • (String)

    text representing encoded data



47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/instance/message_encoder.rb', line 47

def dump(data)
  serialized_data = @serializer.dump(data)
  encrypted_data = ::Encryptor.encrypt(serialized_data, :key => @secret)
  printable_data = ::Base64.encode64(encrypted_data)

  # adhere to the SecureSerializer format in case we want to roll this
  # implementation into that class and distinguish 'secure' encryption
  # from 'secret' by the presence or absence of 'signature'.
  #
  # FIX: do we want to roll them together because it will introduce a
  # dependency on the encryptor gem?
  return @serializer.dump({'id' => @identity, 'data' => printable_data, 'encrypted' => true}, :json)
end

#load(text) ⇒ Object

Loads an encoded serializable object from text.

Parameters:

  • text (String)

    to decode

Returns:



65
66
67
68
69
70
71
# File 'lib/instance/message_encoder.rb', line 65

def load(text)
  hash = @serializer.load(text)
  printable_data = hash['data']  # the only relevant field in this case
  encrypted_data = ::Base64.decode64(printable_data)
  decrypted_data = ::Encryptor.decrypt(encrypted_data, :key => @secret)
  return @serializer.load(decrypted_data)
end