Skip to content

Commit

Permalink
Use Buffer.(from|alloc) instead of deprecated Buffer API
Browse files Browse the repository at this point in the history
This also includes a dependnecy on a polyfill targeting older Node.js
versions where Buffer.alloc() and Buffer.from() API is not implemented
(Node.js < 4.5.0 and some 5.x versions).

Ref: nodejs/node#19079
Ref: https://nodejs.org/api/deprecations.html#deprecations_dep0005_buffer_constructor
Ref: https://nodejs.org/api/buffer.html#buffer_class_buffer
Ref: https://github.com/ChALkeR/safer-buffer/blob/master/Porting-Buffer.md
  • Loading branch information
ChALkeR committed Mar 21, 2018
1 parent 6edb37c commit 6a15d1b
Show file tree
Hide file tree
Showing 28 changed files with 142 additions and 123 deletions.
3 changes: 2 additions & 1 deletion bin/sshpk-verify
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ var dashdash = require('dashdash');
var sshpk = require('../lib/index');
var fs = require('fs');
var path = require('path');
var Buffer = require('safer-buffer').Buffer;

var options = [
{
Expand Down Expand Up @@ -93,7 +94,7 @@ if (require.main === module) {
}

var fmt = opts.format || 'asn1';
var sigData = new Buffer(opts.signature, 'base64');
var sigData = Buffer.from(opts.signature, 'base64');

var sig;
try {
Expand Down
38 changes: 20 additions & 18 deletions lib/algs.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
// Copyright 2015 Joyent, Inc.

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

var algInfo = {
'dsa': {
parts: ['p', 'q', 'g', 'y'],
Expand Down Expand Up @@ -52,27 +54,27 @@ var curves = {
'nistp256': {
size: 256,
pkcs8oid: '1.2.840.10045.3.1.7',
p: new Buffer(('00' +
p: Buffer.from(('00' +
'ffffffff 00000001 00000000 00000000' +
'00000000 ffffffff ffffffff ffffffff').
replace(/ /g, ''), 'hex'),
a: new Buffer(('00' +
a: Buffer.from(('00' +
'FFFFFFFF 00000001 00000000 00000000' +
'00000000 FFFFFFFF FFFFFFFF FFFFFFFC').
replace(/ /g, ''), 'hex'),
b: new Buffer((
b: Buffer.from((
'5ac635d8 aa3a93e7 b3ebbd55 769886bc' +
'651d06b0 cc53b0f6 3bce3c3e 27d2604b').
replace(/ /g, ''), 'hex'),
s: new Buffer(('00' +
s: Buffer.from(('00' +
'c49d3608 86e70493 6a6678e1 139d26b7' +
'819f7e90').
replace(/ /g, ''), 'hex'),
n: new Buffer(('00' +
n: Buffer.from(('00' +
'ffffffff 00000000 ffffffff ffffffff' +
'bce6faad a7179e84 f3b9cac2 fc632551').
replace(/ /g, ''), 'hex'),
G: new Buffer(('04' +
G: Buffer.from(('04' +
'6b17d1f2 e12c4247 f8bce6e5 63a440f2' +
'77037d81 2deb33a0 f4a13945 d898c296' +
'4fe342e2 fe1a7f9b 8ee7eb4a 7c0f9e16' +
Expand All @@ -82,31 +84,31 @@ var curves = {
'nistp384': {
size: 384,
pkcs8oid: '1.3.132.0.34',
p: new Buffer(('00' +
p: Buffer.from(('00' +
'ffffffff ffffffff ffffffff ffffffff' +
'ffffffff ffffffff ffffffff fffffffe' +
'ffffffff 00000000 00000000 ffffffff').
replace(/ /g, ''), 'hex'),
a: new Buffer(('00' +
a: Buffer.from(('00' +
'FFFFFFFF FFFFFFFF FFFFFFFF FFFFFFFF' +
'FFFFFFFF FFFFFFFF FFFFFFFF FFFFFFFE' +
'FFFFFFFF 00000000 00000000 FFFFFFFC').
replace(/ /g, ''), 'hex'),
b: new Buffer((
b: Buffer.from((
'b3312fa7 e23ee7e4 988e056b e3f82d19' +
'181d9c6e fe814112 0314088f 5013875a' +
'c656398d 8a2ed19d 2a85c8ed d3ec2aef').
replace(/ /g, ''), 'hex'),
s: new Buffer(('00' +
s: Buffer.from(('00' +
'a335926a a319a27a 1d00896a 6773a482' +
'7acdac73').
replace(/ /g, ''), 'hex'),
n: new Buffer(('00' +
n: Buffer.from(('00' +
'ffffffff ffffffff ffffffff ffffffff' +
'ffffffff ffffffff c7634d81 f4372ddf' +
'581a0db2 48b0a77a ecec196a ccc52973').
replace(/ /g, ''), 'hex'),
G: new Buffer(('04' +
G: Buffer.from(('04' +
'aa87ca22 be8b0537 8eb1c71e f320ad74' +
'6e1d3b62 8ba79b98 59f741e0 82542a38' +
'5502f25d bf55296c 3a545e38 72760ab7' +
Expand All @@ -118,34 +120,34 @@ var curves = {
'nistp521': {
size: 521,
pkcs8oid: '1.3.132.0.35',
p: new Buffer((
p: Buffer.from((
'01ffffff ffffffff ffffffff ffffffff' +
'ffffffff ffffffff ffffffff ffffffff' +
'ffffffff ffffffff ffffffff ffffffff' +
'ffffffff ffffffff ffffffff ffffffff' +
'ffff').replace(/ /g, ''), 'hex'),
a: new Buffer(('01FF' +
a: Buffer.from(('01FF' +
'FFFFFFFF FFFFFFFF FFFFFFFF FFFFFFFF' +
'FFFFFFFF FFFFFFFF FFFFFFFF FFFFFFFF' +
'FFFFFFFF FFFFFFFF FFFFFFFF FFFFFFFF' +
'FFFFFFFF FFFFFFFF FFFFFFFF FFFFFFFC').
replace(/ /g, ''), 'hex'),
b: new Buffer(('51' +
b: Buffer.from(('51' +
'953eb961 8e1c9a1f 929a21a0 b68540ee' +
'a2da725b 99b315f3 b8b48991 8ef109e1' +
'56193951 ec7e937b 1652c0bd 3bb1bf07' +
'3573df88 3d2c34f1 ef451fd4 6b503f00').
replace(/ /g, ''), 'hex'),
s: new Buffer(('00' +
s: Buffer.from(('00' +
'd09e8800 291cb853 96cc6717 393284aa' +
'a0da64ba').replace(/ /g, ''), 'hex'),
n: new Buffer(('01ff' +
n: Buffer.from(('01ff' +
'ffffffff ffffffff ffffffff ffffffff' +
'ffffffff ffffffff ffffffff fffffffa' +
'51868783 bf2f966b 7fcc0148 f709a5d0' +
'3bb5c9b8 899c47ae bb6fb71e 91386409').
replace(/ /g, ''), 'hex'),
G: new Buffer(('04' +
G: Buffer.from(('04' +
'00c6 858e06b7 0404e9cd 9e3ecb66 2395b442' +
'9c648139 053fb521 f828af60 6b4d3dba' +
'a14b5e77 efe75928 fe1dc127 a2ffa8de' +
Expand Down
5 changes: 3 additions & 2 deletions lib/certificate.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
module.exports = Certificate;

var assert = require('assert-plus');
var Buffer = require('safer-buffer').Buffer;
var algs = require('./algs');
var crypto = require('crypto');
var Fingerprint = require('./fingerprint');
Expand Down Expand Up @@ -185,7 +186,7 @@ Certificate.createSelfSigned = function (subjectOrSubjects, key, options) {
assert.optionalBuffer(options.serial, 'options.serial');
var serial = options.serial;
if (serial === undefined)
serial = new Buffer('0000000000000001', 'hex');
serial = Buffer.from('0000000000000001', 'hex');

var purposes = options.purposes;
if (purposes === undefined)
Expand Down Expand Up @@ -283,7 +284,7 @@ Certificate.create =
assert.optionalBuffer(options.serial, 'options.serial');
var serial = options.serial;
if (serial === undefined)
serial = new Buffer('0000000000000001', 'hex');
serial = Buffer.from('0000000000000001', 'hex');

var purposes = options.purposes;
if (purposes === undefined)
Expand Down
29 changes: 15 additions & 14 deletions lib/dhe.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ module.exports = {

var assert = require('assert-plus');
var crypto = require('crypto');
var Buffer = require('safer-buffer').Buffer;
var algs = require('./algs');
var utils = require('./utils');
var nacl;
Expand Down Expand Up @@ -189,7 +190,7 @@ DiffieHellman.prototype.computeSecret = function (otherpk) {
var secret = nacl.box.before(new Uint8Array(pub),
new Uint8Array(priv));

return (new Buffer(secret));
return (Buffer.from(secret));
}

throw (new Error('Invalid algorithm: ' + this._algo));
Expand Down Expand Up @@ -218,7 +219,7 @@ DiffieHellman.prototype.generateKey = function () {
this._dh.generateKeys();

parts.push({name: 'curve',
data: new Buffer(this._curve)});
data: Buffer.from(this._curve)});
parts.push({name: 'Q', data: this._dh.getPublicKey()});
parts.push({name: 'd', data: this._dh.getPrivateKey()});
this._key = new PrivateKey({
Expand All @@ -236,14 +237,14 @@ DiffieHellman.prototype.generateKey = function () {
priv = r.mod(n1).add(jsbn.ONE);
pub = this._ecParams.getG().multiply(priv);

priv = new Buffer(priv.toByteArray());
pub = new Buffer(this._ecParams.getCurve().
priv = Buffer.from(priv.toByteArray());
pub = Buffer.from(this._ecParams.getCurve().
encodePointHex(pub), 'hex');

this._priv = new ECPrivate(this._ecParams, priv);

parts.push({name: 'curve',
data: new Buffer(this._curve)});
data: Buffer.from(this._curve)});
parts.push({name: 'Q', data: pub});
parts.push({name: 'd', data: priv});

Expand All @@ -258,8 +259,8 @@ DiffieHellman.prototype.generateKey = function () {

} else if (this._algo === 'curve25519') {
var pair = nacl.box.keyPair();
priv = new Buffer(pair.secretKey);
pub = new Buffer(pair.publicKey);
priv = Buffer.from(pair.secretKey);
pub = Buffer.from(pair.publicKey);
priv = Buffer.concat([priv, pub]);
assert.strictEqual(priv.length, 64);
assert.strictEqual(pub.length, 32);
Expand Down Expand Up @@ -316,16 +317,16 @@ function ECPrivate(params, buffer) {
ECPrivate.prototype.deriveSharedSecret = function (pubKey) {
assert.ok(pubKey instanceof ECPublic);
var S = pubKey._pub.multiply(this._priv);
return (new Buffer(S.getX().toBigInteger().toByteArray()));
return (Buffer.from(S.getX().toBigInteger().toByteArray()));
};

function generateED25519() {
if (nacl === undefined)
nacl = require('tweetnacl');

var pair = nacl.sign.keyPair();
var priv = new Buffer(pair.secretKey);
var pub = new Buffer(pair.publicKey);
var priv = Buffer.from(pair.secretKey);
var pub = Buffer.from(pair.publicKey);
assert.strictEqual(priv.length, 64);
assert.strictEqual(pub.length, 32);

Expand Down Expand Up @@ -362,7 +363,7 @@ function generateECDSA(curve) {
dh.generateKeys();

parts.push({name: 'curve',
data: new Buffer(curve)});
data: Buffer.from(curve)});
parts.push({name: 'Q', data: dh.getPublicKey()});
parts.push({name: 'd', data: dh.getPrivateKey()});

Expand Down Expand Up @@ -395,11 +396,11 @@ function generateECDSA(curve) {
var priv = c.mod(n1).add(jsbn.ONE);
var pub = ecParams.getG().multiply(priv);

priv = new Buffer(priv.toByteArray());
pub = new Buffer(ecParams.getCurve().
priv = Buffer.from(priv.toByteArray());
pub = Buffer.from(ecParams.getCurve().
encodePointHex(pub), 'hex');

parts.push({name: 'curve', data: new Buffer(curve)});
parts.push({name: 'curve', data: Buffer.from(curve)});
parts.push({name: 'Q', data: pub});
parts.push({name: 'd', data: priv});

Expand Down
9 changes: 5 additions & 4 deletions lib/ed-compat.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ var nacl;
var stream = require('stream');
var util = require('util');
var assert = require('assert-plus');
var Buffer = require('safer-buffer').Buffer;
var Signature = require('./signature');

function Verifier(key, hashAlgo) {
Expand All @@ -33,7 +34,7 @@ Verifier.prototype._write = function (chunk, enc, cb) {

Verifier.prototype.update = function (chunk) {
if (typeof (chunk) === 'string')
chunk = new Buffer(chunk, 'binary');
chunk = Buffer.from(chunk, 'binary');
this.chunks.push(chunk);
};

Expand All @@ -45,7 +46,7 @@ Verifier.prototype.verify = function (signature, fmt) {
sig = signature.toBuffer('raw');

} else if (typeof (signature) === 'string') {
sig = new Buffer(signature, 'base64');
sig = Buffer.from(signature, 'base64');

} else if (Signature.isSignature(signature, [1, 0])) {
throw (new Error('signature was created by too old ' +
Expand Down Expand Up @@ -81,7 +82,7 @@ Signer.prototype._write = function (chunk, enc, cb) {

Signer.prototype.update = function (chunk) {
if (typeof (chunk) === 'string')
chunk = new Buffer(chunk, 'binary');
chunk = Buffer.from(chunk, 'binary');
this.chunks.push(chunk);
};

Expand All @@ -90,7 +91,7 @@ Signer.prototype.sign = function () {
new Uint8Array(Buffer.concat(this.chunks)),
new Uint8Array(Buffer.concat([
this.key.part.k.data, this.key.part.A.data])));
var sigBuf = new Buffer(sig);
var sigBuf = Buffer.from(sig);
var sigObj = Signature.parse(sigBuf, 'ed25519', 'raw');
sigObj.hashAlgorithm = 'sha512';
return (sigObj);
Expand Down
5 changes: 3 additions & 2 deletions lib/fingerprint.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
module.exports = Fingerprint;

var assert = require('assert-plus');
var Buffer = require('safer-buffer').Buffer;
var algs = require('./algs');
var crypto = require('crypto');
var errs = require('./errors');
Expand Down Expand Up @@ -90,7 +91,7 @@ Fingerprint.parse = function (fp, options) {
if (!base64RE.test(parts[1]))
throw (new FingerprintFormatError(fp));
try {
hash = new Buffer(parts[1], 'base64');
hash = Buffer.from(parts[1], 'base64');
} catch (e) {
throw (new FingerprintFormatError(fp));
}
Expand All @@ -104,7 +105,7 @@ Fingerprint.parse = function (fp, options) {
if (!md5RE.test(parts))
throw (new FingerprintFormatError(fp));
try {
hash = new Buffer(parts, 'hex');
hash = Buffer.from(parts, 'hex');
} catch (e) {
throw (new FingerprintFormatError(fp));
}
Expand Down
3 changes: 2 additions & 1 deletion lib/formats/auto.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ module.exports = {
};

var assert = require('assert-plus');
var Buffer = require('safer-buffer').Buffer;
var utils = require('../utils');
var Key = require('../key');
var PrivateKey = require('../private-key');
Expand All @@ -27,7 +28,7 @@ function read(buf, options) {
return (ssh.read(buf, options));
if (findDNSSECHeader(buf))
return (dnssec.read(buf, options));
buf = new Buffer(buf, 'binary');
buf = Buffer.from(buf, 'binary');
} else {
assert.buffer(buf);
if (findPEMHeader(buf))
Expand Down
Loading

0 comments on commit 6a15d1b

Please sign in to comment.