Sha256: f15c7624182600a5ae64d2693dbc3c1cf9fb10bed2bfb1a4514b151ebe6e2058

Contents?: true

Size: 1.09 KB

Versions: 11

Compression:

Stored size: 1.09 KB

Contents

# frozen_string_literal: true

require 'xxhash'
require 'blake2b'

module ScaleRb
  module Hasher
    class << self
      # params:
      #   hasher: 'Identity' | 'Twox64Concat' | 'Blake2128Concat'
      #    bytes: u8 array
      # return: u8 array
      def apply_hasher(hasher, bytes)
        function_name = hasher.gsub('_', '')._underscore
        Hasher.send(function_name, bytes)
      end
    end

    class << self
      def identity(bytes)
        bytes
      end

      def twox64_concat(bytes)
        data = bytes._to_utf8
        twox64(data) + bytes
      end

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

      def twox64(str)
        result = XXhash.xxh64 str, 0
        result._to_bytes.reverse
      end

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

      def blake2_128(bytes)
        Blake2b.hex(bytes, 16)._to_bytes
      end

      def blake2_256(bytes)
        Blake2b.hex(bytes, 32)._to_bytes
      end
    end
  end
end

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
scale_rb-0.4.2 lib/hasher.rb
scale_rb-0.4.1 lib/hasher.rb
scale_rb-0.4.0 lib/hasher.rb
scale_rb-0.3.5 lib/hasher.rb
scale_rb-0.3.4 lib/hasher.rb
scale_rb-0.3.3 lib/hasher.rb
scale_rb-0.3.2 lib/hasher.rb
scale_rb-0.3.1 lib/hasher.rb
scale_rb-0.3.0 lib/hasher.rb
scale_rb-0.2.2 lib/hasher.rb
scale_rb-0.2.0 lib/hasher.rb