class Ork::Encryption::Serializers::Json

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

@see Encryption

Public Class Methods

content_type() click to toggle source

The Content-Type of the internal format

# File lib/ork/serializers/json.rb, line 11
def self.content_type
  'application/x-json-encrypted'
end
dump(data) click to toggle source

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

data - Hash representing persisted_data to serialize/encrypt.

# 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) click to toggle source

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

blob - String of the original content from Riak

# 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!() click to toggle source

Register the serializer into Riak

# File lib/ork/serializers/json.rb, line 16
def self.register!
  Riak::Serializers[content_type] = self
end

Private Class Methods

cipher() click to toggle source
# File lib/ork/serializers/json.rb, line 53
def self.cipher
  @cipher ||= Cipher.new(Ork::Encryption.encryption_config)
end