<commit_before>require 'digest'
require 'rufus/mnemo'

module Authorize
  class Token
    SALT = "Replace this value with an application-specific value of your choosing."
    KEY_SIZE = 16
    attr_reader :key, :digest

    # Normalize the key, combine with salt and hash the result
    def self.digest(key)
      message = SALT + key
      Digest::SHA256.hexdigest(message)
    end

    def self.generate_key(bytes = KEY_SIZE)
      ("\0" * (KEY_SIZE - bytes)) + ActiveSupport::SecureRandom.random_bytes(bytes)
    end

    # Part One: The reversible key<->integer encoding standard
    def self.i_to_key(i)
      i.to_s(16).rjust(KEY_SIZE * 2, '0').to_a.pack("H*") # fixnum -> hexstring (zero padded) -> binary string
    end
    
    # Part Two: The reversible key<->integer encoding standard
    def self.key_to_i(key)
      key.unpack("H*").first.hex # binary string -> hex string -> fixnum
    end
    
    # Part One: The reversible key<->mnemonic encoding standard
    def self.mnemonic_to_key(mnemonic)
      i = Rufus::Mnemo::to_integer(mnemonic)
      self.i_to_key(i)      
    end

    # Part Two: The reversible key<->mnemonic encoding standard
    def self.key_to_mnemonic(key)
      i = self.key_to_i(key) 
      Rufus::Mnemo::from_integer(i)
    end

    # Build a token from a mnemonic string in Rufus::Mnemo format
    def self.build(mnemonic)
      key = self.mnemonic_to_key(mnemonic)
      _build(key)
    end
    
    def self._build(key)
      digest = self.digest(key)
      self.new(digest, key)
    end

    # Construct a shiny new token using a random key.  The size of the random key, measured in bytes, can be specified.
    def self.generate(bytes = KEY_SIZE)
      self.new.tap {|t| t.generate!(bytes)}
    end
<commit_msg>Make Token more flexible on input by coercing arguments with #to_s<commit_after>require 'digest'
require 'rufus/mnemo'

module Authorize
  class Token
    SALT = "Replace this value with an application-specific value of your choosing."
    KEY_SIZE = 16
    attr_reader :key, :digest

    # Normalize the key, combine with salt and hash the result
    def self.digest(key)
      message = SALT + key
      Digest::SHA256.hexdigest(message)
    end

    def self.generate_key(bytes = KEY_SIZE)
      ("\0" * (KEY_SIZE - bytes)) + ActiveSupport::SecureRandom.random_bytes(bytes)
    end

    # Part One: The reversible key<->integer encoding standard
    def self.i_to_key(i)
      i.to_s(16).rjust(KEY_SIZE * 2, '0').to_a.pack("H*") # fixnum -> hexstring (zero padded) -> binary string
    end
    
    # Part Two: The reversible key<->integer encoding standard
    def self.key_to_i(key)
      key.unpack("H*").first.hex # binary string -> hex string -> fixnum
    end
    
    # Part One: The reversible key<->mnemonic encoding standard
    def self.mnemonic_to_key(mnemonic)
      i = Rufus::Mnemo::to_integer(mnemonic)
      self.i_to_key(i)      
    end

    # Part Two: The reversible key<->mnemonic encoding standard
    def self.key_to_mnemonic(key)
      i = self.key_to_i(key.to_s) 
      Rufus::Mnemo::from_integer(i)
    end

    # Build a token from a mnemonic string in Rufus::Mnemo format
    def self.build(mnemonic)
      key = self.mnemonic_to_key(mnemonic.to_s)
      _build(key)
    end
    
    def self._build(key)
      key = key.to_s
      digest = self.digest(key)
      self.new(digest, key)
    end

    # Construct a shiny new token using a random key.  The size of the random key, measured in bytes, can be specified.
    def self.generate(bytes = KEY_SIZE)
      self.new.tap {|t| t.generate!(bytes)}
    end

{'fraction_non_alphanumeric': 0.1010016694490818, 'fraction_numerical': 0.005564830272676683, 'mean_word_length': 2.8367129135538955, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 19, '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': '6306835', 'n_tokens_mistral': 1253, 'n_tokens_neox': 1193, 'n_words': 334}