Module basic module

Module multibase

Multibase is based on Base1x encoding and introduces a variety of encoding prefix encoding methods.

multibase can encode the same data using different encoding methods and add a prefix to indicate the encoding method. There are 15 encoding methods supported by multibase, namely base1, base2, base8, base10, base16,base32, base32hex, base32z, base36, base40, base56, base58flickr, base58btc,base64, base64url. Among them, base16,base32andbase64is relatively commonly used.

Multibase can be used to change the presentation of binary data without changing it. For example, encoding a randomly generated binary data asbase32A string of the form:

1 2 3 4 5 6 7 8
const { encode } = require('multibase'); const crypto = require('crypto'); const data = crypto.randomBytes(10); // generate 10 bytes random data const encodedStr = encode(data, 'base32'); // encode data to base32 string console.log(encodedStr); // ==> "bpgwnvztqmlbo5fy"

Decode the above string into raw binary data:

1 2 3 4 5 6
const { decode } = require('multibase'); const data = decode('bpgwnvztqmlbo5fy', 'base32'); // decode base32 string to data console.log(data); // ==> <Buffer a7 55 3d 33 ca 97 ac 0d aa 40>

As you can see, through multibase, we encode the original binary data asbase32form of a string, and this string can be decoded into raw binary data.

static function

encode

Encode data in multibase mode

1 2
static String multibase.encode(Buffer data, String codec);

Call parameters:

  • data:Buffer, the data to be encoded
  • codec: String, specify encoding method

Return results:

  • String, returns the encoded string

decode

Decode string into binary data in multibase mode

1
static Buffer multibase.decode(String data);

Call parameters:

  • data: String, the string to be decoded

Return results:

  • Buffer, returns the decoded binary data