Class: Synthra::Types::Crypto::HashType

Inherits:
Base
  • Object
show all
Defined in:
lib/synthra/types/crypto/crypto.rb

Overview

HashType type

Constant Summary collapse

HASH_LENGTHS =

Hash algorithm lengths (in hex characters)

{
  md5: 32,
  sha1: 40,
  sha256: 64,
  sha512: 128
}.freeze

Instance Method Summary collapse

Instance Method Details

#generate_edge(rng, context, args) ⇒ Object



152
153
154
# File 'lib/synthra/types/crypto/crypto.rb', line 152

def generate_edge(rng, context, args)
  ["0" * 64, "f" * 64].sample(random: rng.instance_variable_get(:@random))
end

#generate_invalid(rng, context, args) ⇒ Object



156
157
158
# File 'lib/synthra/types/crypto/crypto.rb', line 156

def generate_invalid(rng, context, args)
  [nil, "not a hash", "invalid", [], {}].sample(random: rng.instance_variable_get(:@random))
end

#generate_random(rng, context, args) ⇒ String

Generate random hash

Parameters:

Options Hash (args):

  • :algorithm (Symbol, String)

    hash algorithm (:md5, :sha1, :sha256, :sha512)

Returns:

  • (String)

    generated hash string



143
144
145
146
147
148
149
150
# File 'lib/synthra/types/crypto/crypto.rb', line 143

def generate_random(rng, context, args)
  algorithm = (args[:algorithm] || :sha256).to_sym
  length = HASH_LENGTHS[algorithm] || 64

  # Generate random hex string
  hex_chars = ("0".."9").to_a + ("a".."f").to_a
  length.times.map { rng.sample(hex_chars) }.join
end