Class: Ripple::Encryption::BinarySerializer

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

Overview

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

Example usage:

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

::Riak::Serializers['application/x-binary-encrypted'] = Ripple::Encryption::BinarySerializer.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-binary-encrypted'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(cipher, path) ⇒ BinarySerializer

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



38
39
40
41
# File 'lib/ripple-encryption/types/binary_serializer.rb', line 38

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



24
25
26
# File 'lib/ripple-encryption/types/binary_serializer.rb', line 24

def cipher
  @cipher
end

#ivObject

Cipher-specific settings

See Also:

  • OpenSSL::Cipher


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

def iv
  @iv
end

#keyObject

Cipher-specific settings

See Also:

  • OpenSSL::Cipher


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

def key
  @key
end

#key_lengthObject

Cipher-specific settings

See Also:

  • OpenSSL::Cipher


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

def key_length
  @key_length
end

#paddingObject

Cipher-specific settings

See Also:

  • OpenSSL::Cipher


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

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



47
48
49
# File 'lib/ripple-encryption/types/binary_serializer.rb', line 47

def dump(object)
  BinaryDocument.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



55
56
57
58
# File 'lib/ripple-encryption/types/binary_serializer.rb', line 55

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