Sha256: 0ae6736d55fffce422748c82c924df2c65006bd3b894d8ebd41e8df1ecd4e265

Contents?: true

Size: 921 Bytes

Versions: 1

Compression:

Stored size: 921 Bytes

Contents

require 'multihashes/version'

module Multihashes
  class HashFunctionNotImplemented < StandardError; end
  class DigestLengthError < StandardError; end

  # https://github.com/jbenet/multihash
  TABLE = {
    0xd5 => 'md5',
    0x11 => 'sha1',
    0x12 => 'sha2-256',
    0x13 => 'sha2-512',
    0x14 => 'sha3',
    0x40 => 'blake2b',
    0x41 => 'blake2s'
  }

  def self.encode(digest, hash_function)
    length = digest.bytesize
    key = TABLE.key hash_function
    raise HashFunctionNotImplemented, 'unknown hash function code' if key.nil?
    [TABLE.key(hash_function), length, digest].pack("CCA#{length}")
  end

  def self.decode(multihash)
    integer, length, digest = multihash.unpack('CCA*')

    if length != digest.bytesize
      raise DigestLengthError, 'digest did not match expected multihash length'
    end

    {code: integer, hash_function: TABLE[integer], length: length, digest: digest}
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
multihashes-0.1.3 lib/multihashes.rb