<commit_before>require 'native'
require 'daniel/opal/sjcl.js'
require 'daniel/opal/aes.js'
require 'daniel/opal/bitArray.js'
require 'daniel/opal/codecBytes.js'
require 'daniel/opal/codecHex.js'
require 'daniel/opal/codecString.js'
require 'daniel/opal/hmac.js'
require 'daniel/opal/pbkdf2.js'
require 'daniel/opal/sha256.js'

module Daniel
  # Miscellaneous utilities.
  class Util
    # Convert a String to a bitArray.
    def self.to_bit_array(s)
      Native(`sjcl.codec.bytes.toBits(#{s.bytes})`)
    end

    # Convert a bitArray to a String.
    def self.from_bit_array(bits)
      Daniel::Util.from_hex(`sjcl.codec.hex.fromBits(#{bits})`)
    end
  end

  # A simple implementation of counter mode for ciphers with 16-byte blocks.
  class CounterMode
    def initialize(cipher, iv)
      @cipher = cipher
      @iv = iv
    end

    def encrypt(data)
      len = data.length
      s = ''
      (len / 16).ceil.times do
        cur, data = data[0..15], data[16..-1]
        buf = @cipher.encrypt(Daniel::Util.to_bit_array(@iv))
        s += xor(Daniel::Util.from_bit_array(buf)[0, cur.length], cur)
        increment
      end
      s
    end

    private

    def increment
      iv = @iv.bytes.reverse
      offset = 0
      while offset < 16
        iv[offset] += 1
        break unless iv[offset] == 256
        iv[offset] = 0
        offset += 1
      end
      @iv = iv.reverse.map { |b| b.chr }.join('')
    end

    def xor(a, b)
      a.bytes.zip(b.bytes).map { |p| (p[0] ^ p[1]).chr }.join('')
    end
  end
end

module OpenSSL<commit_msg>Use unary ampersand instead of a block.<commit_after>require 'native'
require 'daniel/opal/sjcl.js'
require 'daniel/opal/aes.js'
require 'daniel/opal/bitArray.js'
require 'daniel/opal/codecBytes.js'
require 'daniel/opal/codecHex.js'
require 'daniel/opal/codecString.js'
require 'daniel/opal/hmac.js'
require 'daniel/opal/pbkdf2.js'
require 'daniel/opal/sha256.js'

module Daniel
  # Miscellaneous utilities.
  class Util
    # Convert a String to a bitArray.
    def self.to_bit_array(s)
      Native(`sjcl.codec.bytes.toBits(#{s.bytes})`)
    end

    # Convert a bitArray to a String.
    def self.from_bit_array(bits)
      Daniel::Util.from_hex(`sjcl.codec.hex.fromBits(#{bits})`)
    end
  end

  # A simple implementation of counter mode for ciphers with 16-byte blocks.
  class CounterMode
    def initialize(cipher, iv)
      @cipher = cipher
      @iv = iv
    end

    def encrypt(data)
      len = data.length
      s = ''
      (len / 16).ceil.times do
        cur, data = data[0..15], data[16..-1]
        buf = @cipher.encrypt(Daniel::Util.to_bit_array(@iv))
        s += xor(Daniel::Util.from_bit_array(buf)[0, cur.length], cur)
        increment
      end
      s
    end

    private

    def increment
      iv = @iv.bytes.reverse
      offset = 0
      while offset < 16
        iv[offset] += 1
        break unless iv[offset] == 256
        iv[offset] = 0
        offset += 1
      end
      @iv = iv.reverse.map(&:chr).join('')
    end

    def xor(a, b)
      a.bytes.zip(b.bytes).map { |p| (p[0] ^ p[1]).chr }.join('')
    end
  end
end

module OpenSSL
{'fraction_non_alphanumeric': 0.13286489939316512, 'fraction_numerical': 0.01660811242414564, 'mean_word_length': 2.437980241492865, 'pattern_counts': {'":': 0, '<': 5, '<?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': '1520028', 'n_tokens_mistral': 1191, 'n_tokens_neox': 1108, 'n_words': 262}