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 more commonly used.

multibase can be used to change the representation of the 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 to 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, and can decode this string into raw binary data.

static function

encode

Encode data in a multibase manner

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

Call parameters:

  • data:Buffer, the data to encode
  • codec: String, specifies the encoding method

return result:

  • String, returns the encoded string

decode

Decode strings to binary data in multibase mode

1
static Buffer multibase.decode(String data);

Call parameters:

  • data: String, the string to decode

return result:

  • Buffer, returns the decoded binary data