Class: Ork::Encryption::Serializers::Json

Inherits:
Object
  • Object
show all
Defined in:
lib/ork/serializers/json.rb

Overview

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

See Also:

Class Method Summary collapse

Class Method Details

.content_typeObject

The Content-Type of the internal format



11
12
13
# File 'lib/ork/serializers/json.rb', line 11

def self.content_type
  'application/x-json-encrypted'
end

.dump(data) ⇒ Object

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

data - Hash representing persisted_data to serialize/encrypt.



25
26
27
28
29
30
31
32
33
34
35
# File 'lib/ork/serializers/json.rb', line 25

def self.dump(data)
  json_attributes = data.to_json(Riak.json_options)

  encrypted_object = {
    iv:      Base64.encode64(cipher.random_iv!),
    data:    Base64.encode64(cipher.encrypt json_attributes),
    version: Ork::Encryption::VERSION
  }

  encrypted_object.to_json(Riak.json_options)
end

.load(blob) ⇒ Object

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

blob - String of the original content from Riak



42
43
44
45
46
47
48
49
# File 'lib/ork/serializers/json.rb', line 42

def self.load(blob)
  encrypted_object = Riak::JSON.parse(blob)
  cipher.iv = Base64.decode64 encrypted_object['iv']
  decoded_data = Base64.decode64 encrypted_object['data']

  # this serializer now only supports the v2 (0.0.2 - 0.0.4) format
  Riak::JSON.parse(cipher.decrypt decoded_data)
end

.register!Object

Register the serializer into Riak



16
17
18
# File 'lib/ork/serializers/json.rb', line 16

def self.register!
  Riak::Serializers[content_type] = self
end