Skip to content

ThiniusAirington/js-multihash

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

37 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

js-multihash

Coverage Status Travis CI Circle CI Dependency Status js-standard-style

multihash implementation.

Installation

In Node.js through npm

$ npm install --save multihashes # node the name of the module is multihashes

Browser: Browserify, Webpack, other bundlers

The code published to npm that gets loaded on require is in fact an ES5 transpiled version with the right shims added. This means that you can require it and use with your favourite bundler without having to adjust asset management process.

var multihashes = require('multihashes')

In the Browser through <script> tag

Loading this module through a script tag will make the Multihashes obj available in the global namespace.

<script src="https://npmcdn.com/multihashes/dist/index.min.js"></script>
<!-- OR -->
<script src="https://npmcdn.com/multihashes/dist/index.js"></script>

Gotchas

You will need to use Node.js Buffer API compatible, if you are running inside the browser, you can access it by multihash.Buffer or you can install Feross's Buffer.

Usage

> var multihash = require('multihashes')
> var buf = new Buffer('0beec7b5ea3f0fdbc95d0dd47f3c5bc275da8a33', 'hex')

> var encoded = multihash.encode(buf, 'sha1')
> console.log(encoded)
<Buffer 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: <Buffer 0b ee c7 b5 ea 3f 0f db c9 5d 0d d4 7f 3c 5b c2 75 da 8a 33> }

API

decode(buf)

  • buf: Buffer

Decode a hash from the given multihash.

Returns an object of the form,

{
  code: Number,
  name: String,
  length: Number,
  digest: Buffer
}

encode(digest, hashfn[, length])

  • digest: Buffer
  • hashfn: String|Number
  • length: Number (optional)

Encode a hash digest along with the specified function code.

Note: the length is derived from the length of the digest itself.

Returns a buffer.

toHexString(multihash)

  • multihash: Buffer

Convert the given multihash to a hex encoded string.

Returns a string.

fromHexString(string)

  • string: String

Convert the given hex encoded string to a multihash (a Buffer).

Returns a buffer.

toB58String(multihash)

  • multihash: Buffer

Convert the given multihash to a base58 encoded string.

Returns a string.

fromB58String(string)

  • string: String

Convert the given base58 encoded string to a multihash (a Buffer).

Returns a buffer.

coerceCode(name)

  • name: String|Number

Converts a given hash function into the matching code. If passed a number it will return the number if it's a valid code.

Returns a number.

isAppCode(code)

  • code: Number

Checks wether a code is part of the app range.

Returns a boolean.

isValidCode(code)

  • code: Number

Checks whether a multihash code is valid.

Returns a boolean.

validate(multihash)

  • multihash: Buffer

Check if the given buffer is a valid multihash. Throws an error if it is not valid.

License

MIT

About

multihash implementation in node.js

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages

  • JavaScript 100.0%