diff --git a/src/crypto/util.js b/src/crypto/util.js index a641b82..a33efd9 100644 --- a/src/crypto/util.js +++ b/src/crypto/util.js @@ -5,7 +5,7 @@ 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') + let s = bn.toArrayLike(Buffer, 'be').toString('base64') return s .replace(/(=*)$/, '') // Remove any trailing '='s diff --git a/test/util.spec.js b/test/util.spec.js new file mode 100644 index 0000000..31b2434 --- /dev/null +++ b/test/util.spec.js @@ -0,0 +1,21 @@ +/* eslint max-nested-callbacks: ["error", 8] */ +/* eslint-env mocha */ +'use strict' + +const expect = require('chai').expect +const util = require('../src/crypto/util') +const BN = require('bn.js') + +describe('Util', () => { + let bn + + before((done) => { + bn = new BN('dead', 16) + done() + }) + + it('toBase64', (done) => { + expect(util.toBase64(bn)).to.be.eql('3q0') + done() + }) +})