Module: ScaleRb::Hasher

Defined in:
lib/scale_rb/hasher.rb

Class Method Summary collapse

Class Method Details

.apply_hasher(hasher, bytes) ⇒ Object

params:

hasher: 'Identity' | 'Twox64Concat' | 'Blake2128Concat'
bytes: u8a | hex string

return: u8a



13
14
15
16
17
18
# File 'lib/scale_rb/hasher.rb', line 13

def apply_hasher(hasher, bytes)
  bytes = Utils.hex_to_u8a(bytes) if bytes.is_a?(::String)

  function_name = Utils.underscore(hasher.to_s.gsub('_', ''))
  Hasher.send(function_name, bytes)
end

.blake2128_concat(bytes) ⇒ Object



31
32
33
# File 'lib/scale_rb/hasher.rb', line 31

def blake2128_concat(bytes)
  blake2_128(bytes) + bytes
end

.blake2_128(bytes) ⇒ Object



49
50
51
# File 'lib/scale_rb/hasher.rb', line 49

def blake2_128(bytes)
  Utils.hex_to_u8a(Blake2b.hex(bytes, 16))
end

.blake2_256(bytes) ⇒ Object



53
54
55
# File 'lib/scale_rb/hasher.rb', line 53

def blake2_256(bytes)
  Utils.hex_to_u8a(Blake2b.hex(bytes, 32))
end

.identity(bytes) ⇒ Object



22
23
24
# File 'lib/scale_rb/hasher.rb', line 22

def identity(bytes)
  bytes
end

.twox128(str) ⇒ Object



40
41
42
43
44
45
46
47
# File 'lib/scale_rb/hasher.rb', line 40

def twox128(str)
  bytes = []
  2.times do |i|
    result = XXhash.xxh64 str, i
    bytes += Utils.hex_to_u8a(result.to_s(16)).reverse
  end
  bytes
end

.twox64(str) ⇒ Object



35
36
37
38
# File 'lib/scale_rb/hasher.rb', line 35

def twox64(str)
  result = XXhash.xxh64 str, 0
  Utils.hex_to_u8a(result.to_s(16)).reverse
end

.twox64_concat(bytes) ⇒ Object



26
27
28
29
# File 'lib/scale_rb/hasher.rb', line 26

def twox64_concat(bytes)
  data = Utils.u8a_to_utf8(bytes)
  twox64(data) + bytes
end