Skip to content

Commit

Permalink
Merge pull request #14 from omsmith/use-safe-buffer
Browse files Browse the repository at this point in the history
Use safe buffer
  • Loading branch information
omsmith committed Nov 15, 2016
2 parents 4a8e195 + ee856b9 commit 41ff0aa
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 17 deletions.
4 changes: 3 additions & 1 deletion benchmarks/der-to-jose.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
'use strict';

var Buffer = require('safe-buffer').Buffer;

var derToJose = require('..').derToJose;

var sigs = [
Expand All @@ -9,7 +11,7 @@ var sigs = [
];

var sigBuffers = sigs.map(function(sig) {
return [new Buffer(sig[0], 'base64'), sig[1]];
return [Buffer.from(sig[0], 'base64'), sig[1]];
});

module.exports.compare = {
Expand Down
4 changes: 3 additions & 1 deletion benchmarks/jose-to-der.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
'use strict';

var Buffer = require('safe-buffer').Buffer;

var joseToDer = require('..').joseToDer;

var sigs = [
Expand All @@ -9,7 +11,7 @@ var sigs = [
];

var sigBuffers = sigs.map(function(sig) {
return [new Buffer(sig[0], 'base64'), sig[1]];
return [Buffer.from(sig[0], 'base64'), sig[1]];
});

module.exports.compare = {
Expand Down
3 changes: 2 additions & 1 deletion fuzz/keygen.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use strict';

var base64url = require('base64-url').escape,
Buffer = require('safe-buffer').Buffer,
EC = require('elliptic').ec,
jwkToPem = require('jwk-to-pem');

Expand All @@ -17,7 +18,7 @@ var curves = {

function b64(val) {
val = val.toString('hex', 2);
val = new Buffer(val, 'hex');
val = Buffer.from(val, 'hex');
val = val.toString('base64');
val = base64url(val);
return val;
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@
},
"homepage": "https://github.com/Brightspace/node-ecdsa-sig-formatter#readme",
"dependencies": {
"base64-url": "^1.2.1"
"base64-url": "^1.2.1",
"safe-buffer": "^5.0.1"
},
"devDependencies": {
"bench": "^0.3.6",
Expand Down
21 changes: 11 additions & 10 deletions spec/der-to-jose.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use strict';

var expect = require('chai').expect,
var Buffer = require('safe-buffer').Buffer,
expect = require('chai').expect,
mocha = require('mocha');

var describe = mocha.describe,
Expand Down Expand Up @@ -43,7 +44,7 @@ describe('#derToJose', function() {
});

it('no seq', function() {
var input = new Buffer(10);
var input = Buffer.alloc(10);
input[0] = TAG_SEQ + 1; // not seq

function fn() {
Expand All @@ -54,7 +55,7 @@ describe('#derToJose', function() {
});

it('seq length exceeding input', function() {
var input = new Buffer(10);
var input = Buffer.alloc(10);
input[0] = TAG_SEQ;
input[1] = 10;

Expand All @@ -66,7 +67,7 @@ describe('#derToJose', function() {
});

it('r is not marked as int', function() {
var input = new Buffer(10);
var input = Buffer.alloc(10);
input[0] = TAG_SEQ;
input[1] = 8;
input[2] = TAG_INT + 1; // not int
Expand All @@ -79,7 +80,7 @@ describe('#derToJose', function() {
});

it('r length exceeds available input', function() {
var input = new Buffer(10);
var input = Buffer.alloc(10);
input[0] = TAG_SEQ;
input[1] = 8;
input[2] = TAG_INT;
Expand All @@ -93,7 +94,7 @@ describe('#derToJose', function() {
});

it('r length exceeds sensical param length', function() {
var input = new Buffer(getParamBytesForAlg(alg) + 2 + 6);
var input = Buffer.alloc(getParamBytesForAlg(alg) + 2 + 6);
input[0] = TAG_SEQ;
input[1] = getParamBytesForAlg(alg) + 2 + 4;
input[2] = TAG_INT;
Expand All @@ -107,7 +108,7 @@ describe('#derToJose', function() {
});

it('s is not marked as int', function() {
var input = new Buffer(10);
var input = Buffer.alloc(10);
input[0] = TAG_SEQ;
input[1] = 8;
input[2] = TAG_INT;
Expand All @@ -124,7 +125,7 @@ describe('#derToJose', function() {
});

it('s length exceeds available input', function() {
var input = new Buffer(10);
var input = Buffer.alloc(10);
input[0] = TAG_SEQ;
input[1] = 8;
input[2] = TAG_INT;
Expand All @@ -142,7 +143,7 @@ describe('#derToJose', function() {
});

it('s length does not consume available input', function() {
var input = new Buffer(10);
var input = Buffer.alloc(10);
input[0] = TAG_SEQ;
input[1] = 8;
input[2] = TAG_INT;
Expand All @@ -160,7 +161,7 @@ describe('#derToJose', function() {
});

it('s length exceeds sensical param length', function() {
var input = new Buffer(getParamBytesForAlg(alg) + 2 + 8);
var input = Buffer.alloc(getParamBytesForAlg(alg) + 2 + 8);
input[0] = TAG_SEQ;
input[1] = getParamBytesForAlg(alg) + 2 + 6;
input[2] = TAG_INT;
Expand Down
7 changes: 4 additions & 3 deletions src/ecdsa-sig-formatter.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use strict';

var base64Url = require('base64-url').escape;
var Buffer = require('safe-buffer').Buffer;

var getParamBytesForAlg = require('./param-bytes-for-alg');

Expand All @@ -16,7 +17,7 @@ function signatureAsBuffer(signature) {
if (Buffer.isBuffer(signature)) {
return signature;
} else if ('string' === typeof signature) {
return new Buffer(signature, 'base64');
return Buffer.from(signature, 'base64');
}

throw new TypeError('ECDSA signature must be a Base64 string or a Buffer');
Expand Down Expand Up @@ -87,7 +88,7 @@ function derToJose(signature, alg) {
var rPadding = paramBytes - rLength,
sPadding = paramBytes - sLength;

var dst = new Buffer(rPadding + rLength + sPadding + sLength);
var dst = Buffer.allocUnsafe(rPadding + rLength + sPadding + sLength);

for (offset = 0; offset < rPadding; ++offset) {
dst[offset] = 0;
Expand Down Expand Up @@ -139,7 +140,7 @@ function joseToDer(signature, alg) {

var shortLength = rsBytes < MAX_OCTET;

var dst = new Buffer((shortLength ? 2 : 3) + rsBytes);
var dst = Buffer.allocUnsafe((shortLength ? 2 : 3) + rsBytes);

var offset = 0;
dst[offset++] = ENCODED_TAG_SEQ;
Expand Down

0 comments on commit 41ff0aa

Please sign in to comment.