mirror of
https://github.com/fluencelabs/js-libp2p-crypto
synced 2025-04-09 12:46:21 +00:00
20 lines
616 B
JavaScript
20 lines
616 B
JavaScript
|
'use strict'
|
||
|
|
||
|
const BN = require('asn1.js').bignum
|
||
|
|
||
|
// Convert a BN.js instance to a base64 encoded string without padding
|
||
|
// Adapted from https://tools.ietf.org/html/draft-ietf-jose-json-web-signature-41#appendix-C
|
||
|
exports.toBase64 = function toBase64 (bn) {
|
||
|
let s = bn.toBuffer('be').toString('base64')
|
||
|
|
||
|
return s
|
||
|
.replace(/(=*)$/, '') // Remove any trailing '='s
|
||
|
.replace(/\+/g, '-') // 62nd char of encoding
|
||
|
.replace(/\//g, '_') // 63rd char of encoding
|
||
|
}
|
||
|
|
||
|
// Convert a base64 encoded string to a BN.js instance
|
||
|
exports.toBn = function toBn (str) {
|
||
|
return new BN(Buffer.from(str, 'base64'))
|
||
|
}
|