<commit_before>require 'digest/sha2'
require 'securerandom'
module Cifrado
  class CryptoServices

    def initialize(options = {})
      require 'shellwords'
      require 'securerandom'
      @options = options
      @gpg_binary = @options[:gpg_binary] || '/usr/bin/gpg'
      @gpg_extra_args = @options[:gpg_extra_args] || []
      @gpg_extra_args = @gpg_extra_args.concat %w(--batch --yes)
      @encrypt_name = @options[:encrypt_name] || false
    end

    def encrypt(file, output)
      unless File.exist?(file)
        raise ArgumentError.new "Invalid file #{file}"
      end

      if output.nil?
        raise ArgumentError.new "Invalid output file path"
      end

      # so we can use --use-embedded-filename to recover the original
      # filename
      @gpg_extra_args << "--set-filename #{file}"

      check_args
      raise ArgumentError.new("#{@gpg_binary} not found") unless File.exist?(@gpg_binary)

      Log.debug "Encrypting file #{file}..."
      if @encrypt_name and output != '-'
        Log.debug "Scrambling file name #{file}..."
        dir = File.dirname(output)
        output = File.join dir, (Digest::SHA2.new << (output + SecureRandom.hex)).to_s
      end
      
      if output != '-'
        @gpg_extra_args << ["--output #{Shellwords.escape(output)}"]<commit_msg>URI.encode file path so GPG does not strip it<commit_after>require 'digest/sha2'
require 'securerandom'
require 'uri'

module Cifrado
  class CryptoServices

    def initialize(options = {})
      require 'shellwords'
      require 'securerandom'
      @options = options
      @gpg_binary = @options[:gpg_binary] || '/usr/bin/gpg'
      @gpg_extra_args = @options[:gpg_extra_args] || []
      @gpg_extra_args = @gpg_extra_args.concat %w(--batch --yes)
      @encrypt_name = @options[:encrypt_name] || false
    end

    def encrypt(file, output)
      unless File.exist?(file)
        raise ArgumentError.new "Invalid file #{file}"
      end

      if output.nil?
        raise ArgumentError.new "Invalid output file path"
      end

      # so we can use --use-embedded-filename to recover the original
      # filename
      Log.debug "Setting GPG filename #{file}"
      @gpg_extra_args << "--set-filename '#{URI.encode(file)}'"

      check_args
      raise ArgumentError.new("#{@gpg_binary} not found") unless File.exist?(@gpg_binary)

      Log.debug "Encrypting file #{file}..."
      if @encrypt_name and output != '-'
        Log.debug "Scrambling file name #{file}..."
        dir = File.dirname(output)
        output = File.join dir, (Digest::SHA2.new << (output + SecureRandom.hex)).to_s
      end
      
      if output != '-'
        @gpg_extra_args << ["--output #{Shellwords.escape(output)}"]
{'fraction_non_alphanumeric': 0.13274336283185842, 'fraction_numerical': 0.0014749262536873156, 'mean_word_length': 2.7062841530054644, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 3, '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': '2737618', 'n_tokens_mistral': 863, 'n_tokens_neox': 844, 'n_words': 206}