fix(utils): make util.toBase64 browserify compatible

`bn.toArrayLike` is used instead of `bn.toBuffer`, to ensure compatibility with browserify.
This commit is contained in:
nikuda 2016-12-03 19:32:07 +11:00 committed by Friedel Ziegelmayer
parent a4e6f9dd83
commit 933119445f
2 changed files with 22 additions and 1 deletions

View File

@ -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

21
test/util.spec.js Normal file
View File

@ -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()
})
})