Class: Synthra::Types::Crypto::HashType
- 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
- #generate_edge(rng, context, args) ⇒ Object
- #generate_invalid(rng, context, args) ⇒ Object
-
#generate_random(rng, context, args) ⇒ String
Generate random hash.
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
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 |