<commit_before>module Ripple
  module Contrib
    # Generic error class for Encryptor
    class EncryptedJsonDocumentError < StandardError; end

    # Interprets a encapsulation in JSON for encrypted Ripple documents.
    #
    # Example usage:
    #     Ripple::Contrib::JsonDocument.new(@document).encrypt
    class EncryptedJsonDocument
      # Creates an object that is prepared to decrypt its contents.
      # @param [String] data json string that was stored in Riak
      def initialize(config, data)
        @config = config.to_h
        @json = JSON.parse data
        raise(EncryptedJsonDocumentError, "Missing 'iv' for decryption") unless @json['iv']
        iv = Base64.decode64 @json['iv']
        @config.merge('iv' => iv)

        @decryptor = Ripple::Contrib::Encryptor.new @config
      end

      # Returns the original data from the stored encrypted format
      def decrypt
        JSON.load @decryptor.decrypt Base64.decode64 @json['data']
      end
    end
  end
end<commit_msg>Avoid a potential race condition by pointing to a clone of the config hash.<commit_after>module Ripple
  module Contrib
    # Generic error class for Encryptor
    class EncryptedJsonDocumentError < StandardError; end

    # Interprets a encapsulation in JSON for encrypted Ripple documents.
    #
    # Example usage:
    #     Ripple::Contrib::JsonDocument.new(@document).encrypt
    class EncryptedJsonDocument
      # Creates an object that is prepared to decrypt its contents.
      # @param [String] data json string that was stored in Riak
      def initialize(config, data)
        @config = config.to_h.clone
        @json = JSON.parse data
        raise(EncryptedJsonDocumentError, "Missing 'iv' for decryption") unless @json['iv']
        iv = Base64.decode64 @json['iv']
        @config.merge!('iv' => iv)

        @decryptor = Ripple::Contrib::Encryptor.new @config
      end

      # Returns the original data from the stored encrypted format
      def decrypt
        JSON.load @decryptor.decrypt Base64.decode64 @json['data']
      end
    end
  end
end
{'fraction_non_alphanumeric': 0.07974867085548575, 'fraction_numerical': 0.007733204446592557, 'mean_word_length': 2.9204545454545454, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2641663', 'n_tokens_mistral': 598, 'n_tokens_neox': 565, 'n_words': 192}