Class: Ripple::Encryption::JsonSerializer

Inherits:
Object
  • Object
show all
Defined in:
lib/ripple-encryption/types/json_serializer.rb

Overview

Implements the Riak::Serializer API for the purpose of encrypting/decrypting Ripple documents as JSON.

Example usage:

path = File.join(ROOT_DIR,'config','encryption.yml')

::Riak::Serializers['application/x-json-encrypted'] = Ripple::Encryption::JsonSerializer.new
(
  OpenSSL::Cipher.new(config['cipher']), path
)

class MyDocument
  include Ripple::Document
  include Ripple::Encryption
end

See Also:

Constant Summary collapse

REGISTER_KEY =
'application/x-json-encrypted'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(cipher, path) ⇒ JsonSerializer

Creates a serializer using the provided cipher and internal content type. Be sure to set the #key, #iv, #key_length, #padding as appropriate for the cipher before attempting (de-)serialization.

Parameters:

  • cipher (OpenSSL::Cipher)

    the desired encryption/decryption algorithm

  • path (String)

    the File location of ‘encryption.yml’ private key file



42
43
44
45
# File 'lib/ripple-encryption/types/json_serializer.rb', line 42

def initialize(cipher, path)
  @cipher, @content_type = cipher, REGISTER_KEY
  @config = Ripple::Encryption::Config.new(path)
end

Instance Attribute Details

#cipherOpenSSL::Cipher, OpenSSL::PKey::*

Returns the cipher used to encrypt the object.

Returns:

  • (OpenSSL::Cipher, OpenSSL::PKey::*)

    the cipher used to encrypt the object



28
29
30
# File 'lib/ripple-encryption/types/json_serializer.rb', line 28

def cipher
  @cipher
end

#content_typeString

Returns The Content-Type of the internal format, generally “application/json”.

Returns:

  • (String)

    The Content-Type of the internal format, generally “application/json”



25
26
27
# File 'lib/ripple-encryption/types/json_serializer.rb', line 25

def content_type
  @content_type
end

#ivObject

Cipher-specific settings

See Also:

  • OpenSSL::Cipher


32
33
34
# File 'lib/ripple-encryption/types/json_serializer.rb', line 32

def iv
  @iv
end

#keyObject

Cipher-specific settings

See Also:

  • OpenSSL::Cipher


32
33
34
# File 'lib/ripple-encryption/types/json_serializer.rb', line 32

def key
  @key
end

#key_lengthObject

Cipher-specific settings

See Also:

  • OpenSSL::Cipher


32
33
34
# File 'lib/ripple-encryption/types/json_serializer.rb', line 32

def key_length
  @key_length
end

#paddingObject

Cipher-specific settings

See Also:

  • OpenSSL::Cipher


32
33
34
# File 'lib/ripple-encryption/types/json_serializer.rb', line 32

def padding
  @padding
end

Instance Method Details

#dump(object) ⇒ String

Serializes and encrypts the Ruby object using the assigned cipher and Content-Type.

Parameters:

  • object (Object)

    the Ruby object to serialize/encrypt

Returns:

  • (String)

    the serialized, encrypted form of the object



51
52
53
# File 'lib/ripple-encryption/types/json_serializer.rb', line 51

def dump(object)
  JsonDocument.new(@config, object).encrypt
end

#load(object) ⇒ Object

Decrypts and deserializes the blob using the assigned cipher and Content-Type.

Parameters:

  • blob (String)

    the original content from Riak

Returns:

  • (Object)

    the decrypted and deserialized object



59
60
61
62
# File 'lib/ripple-encryption/types/json_serializer.rb', line 59

def load(object)
  # this serializer now only supports the v2 (0.0.2 - 0.0.4) format
  return EncryptedJsonDocument.new(@config, object).decrypt
end