What is multihashes?
The 'multihashes' npm package provides utilities for creating, decoding, and working with multihashes. Multihashes are self-describing hashes that include information about the hash function used and the length of the hash, making them useful for decentralized systems and content-addressable storage.
What are multihashes's main functionalities?
Encoding a hash
This feature allows you to encode a hash using a specified hash function. In this example, a SHA-256 hash is encoded into a multihash.
const multihashes = require('multihashes');
const hash = Buffer.from('1220' + 'e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855', 'hex');
const encoded = multihashes.encode(hash, 'sha2-256');
console.log(encoded.toString('hex'));
Decoding a multihash
This feature allows you to decode a multihash to retrieve the original hash and the hash function used. The example decodes a SHA-256 multihash.
const multihashes = require('multihashes');
const encoded = Buffer.from('1220e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855', 'hex');
const decoded = multihashes.decode(encoded);
console.log(decoded);
Validating a multihash
This feature allows you to validate a multihash to ensure it is correctly formatted. The example checks if a given multihash is valid.
const multihashes = require('multihashes');
const encoded = Buffer.from('1220e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855', 'hex');
const isValid = multihashes.validate(encoded);
console.log(isValid);
Other packages similar to multihashes
multibase
The 'multibase' package provides utilities for encoding and decoding data in various base encodings. It is similar to 'multihashes' in that it deals with encoding schemes, but it focuses on base encodings rather than hash functions.
multicodec
The 'multicodec' package provides a way to self-describe content with a compact codec identifier. It is similar to 'multihashes' in that it includes metadata about the content, but it focuses on codec identifiers rather than hash functions.
multiformats
The 'multiformats' package is a collection of self-describing protocol formats, including multihash, multibase, and multicodec. It provides a more comprehensive suite of tools compared to 'multihashes', which focuses solely on multihashes.
js-multihash
multihash implementation in node.js
This is the multihash implementation in Node.
It is extended by js-multihashing
and js-multihashing-async,
so give those a look as well.
Lead Maintainer
Hugo Dias
Table of Contents
Install
Using npm
> npm install multihashes
Using a <script>
tag
Loading this module through a script tag will make the Multihashes
obj available in the global namespace.
<script src="https://unpkg.com/multihashes/dist/index.min.js"></script>
Usage
> var multihash = require('multihashes')
> var bytes = Uint8Array.from([0, 1, 2, 3...])
> var encoded = multihash.encode(bytes, 'sha1')
> console.log(encoded)
<Uint8Array 11 14 0b ee c7 b5 ea 3f 0f db c9 5d 0d d4 7f 3c 5b c2 75 da 8a 33>
> multihash.decode(encoded)
{ code: 17,
name: 'sha1',
length: 20,
digest: <Uint8Array 0b ee c7 b5 ea 3f 0f db c9 5d 0d d4 7f 3c 5b c2 75 da 8a 33> }
API
https://multiformats.github.io/js-multihash/
Update Constants
To update the constants table run the command below. This will fetch the main codec list from https://raw.githubusercontent.com/multiformats/multicodec/master/table.csv and filter only the multihash codecs and update the constants.js
file in this repo.
npm run update-constants
Contribute
Contributions welcome. Please check out the issues.
Check out our contributing document for more information on how we work, and about contributing in general. Please be aware that all interactions related to multiformats are subject to the IPFS Code of Conduct.
Small note: If editing the README, please conform to the standard-readme specification.
License
MIT © Protocol Labs Inc.