Sha256: a37290638529dd7c0df46dfaa2060e008fed0e7d9eed6d98233aa8cc36ca1705

Contents?: true

Size: 906 Bytes

Versions: 2

Compression:

Stored size: 906 Bytes

Contents

# frozen_string_literal: true

class Cryptoform::EncryptionBackends::DiffLockbox < Cryptoform::EncryptionBackends::Lockbox
  def encrypt(json)
    JSON.pretty_generate(encrypt_object(json, lockbox))
  end

  def decrypt(ciphertext)
    decrypt_object(JSON.parse(ciphertext, symbolize_names: true), lockbox)
  end

  private

  def decrypt_object(object, box)
    return object.transform_values { decrypt_object(_1, box) } if object.is_a?(Hash)
    return object.map { decrypt_object(_1, box) } if object.is_a?(Array)

    decrypted = box.decrypt(object)

    decrypted.start_with?("{") ? JSON.parse(decrypted, symbolize_names: true)[:value] : decrypted
  end

  def encrypt_object(object, box)
    return object.transform_values { encrypt_object(_1, box) } if object.is_a?(Hash)
    return object.map { encrypt_object(_1, box) } if object.is_a?(Array)

    box.encrypt({ value: object }.to_json)
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
cryptoform-0.5.1 lib/cryptoform/encryption_backends/diff_lockbox.rb
cryptoform-0.5.0 lib/cryptoform/encryption_backends/diff_lockbox.rb