Style fixes; add spaces around all infix operators, remove new Buffer (#954)

* Add "space-infix-ops": "error" rule

* Remove deprecated Buffer constructor

* Resolve new-cap eslint rule

* @twiss: Clarify code that selects curve and algorithm
This commit is contained in:
Ilya Chesnokov 2019-08-30 13:27:30 +03:00 committed by Daniel Huigens
parent b23ee190c7
commit 5d9629d6a3
26 changed files with 155 additions and 164 deletions

View File

@ -295,7 +295,7 @@ module.exports = {
"error", "error",
"never" "never"
], ],
"space-infix-ops": "off", "space-infix-ops": "error",
"space-unary-ops": "error", "space-unary-ops": "error",
"spaced-comment": "off", "spaced-comment": "off",
"strict": "off", "strict": "off",
@ -315,6 +315,13 @@ module.exports = {
"never" "never"
], ],
"indent": [ "error", 2, { "SwitchCase": 1 } ], "indent": [ "error", 2, { "SwitchCase": 1 } ],
"no-buffer-constructor": "error",
"no-lonely-if": "error",
"no-unused-vars": "error",
// eslint-plugin-import rules:
"import/extensions": "never",
"import/no-extraneous-dependencies": ["error", {"devDependencies": true, "optionalDependencies": false, "peerDependencies": false}],
// Custom silencers: // Custom silencers:
"camelcase": 0, "camelcase": 0,
@ -330,17 +337,9 @@ module.exports = {
"no-use-before-define": [ 2, { "functions": false, "classes": true, "variables": false }], "no-use-before-define": [ 2, { "functions": false, "classes": true, "variables": false }],
"no-unused-expressions": [ 2, { "allowShortCircuit": true } ], "no-unused-expressions": [ 2, { "allowShortCircuit": true } ],
"no-constant-condition": [ 2, { "checkLoops": false } ], "no-constant-condition": [ 2, { "checkLoops": false } ],
"new-cap": [ 2, { "properties": false, "capIsNewExceptionPattern": "CMAC|CBC|OMAC|CTR", "newIsCapExceptionPattern": "type|hash*"}],
// Custom warnings: // Custom warnings:
"no-console": 1, "no-console": 1,
"no-unused-vars": 1,
// TODO Consider fixing these:
"valid-jsdoc": 0,
"new-cap": [ 0, { "properties": false, "capIsNewExceptionPattern": "^type_.*" }],
"no-lonely-if": 0,
"import/extensions": 0,
"import/no-extraneous-dependencies": 0,
"no-buffer-constructor": 0, // deprecated
} }
}; };

View File

@ -137,15 +137,15 @@ async function webEncrypt(algo, key, pt, iv) {
} }
function nodeEncrypt(algo, key, pt, iv) { function nodeEncrypt(algo, key, pt, iv) {
key = new Buffer(key); key = Buffer.from(key);
iv = new Buffer(iv); iv = Buffer.from(iv);
const cipherObj = new nodeCrypto.createCipheriv('aes-' + algo.substr(3, 3) + '-cfb', key, iv); const cipherObj = new nodeCrypto.createCipheriv('aes-' + algo.substr(3, 3) + '-cfb', key, iv);
return stream.transform(pt, value => new Uint8Array(cipherObj.update(new Buffer(value)))); return stream.transform(pt, value => new Uint8Array(cipherObj.update(Buffer.from(value))));
} }
function nodeDecrypt(algo, key, ct, iv) { function nodeDecrypt(algo, key, ct, iv) {
key = new Buffer(key); key = Buffer.from(key);
iv = new Buffer(iv); iv = Buffer.from(iv);
const decipherObj = new nodeCrypto.createDecipheriv('aes-' + algo.substr(3, 3) + '-cfb', key, iv); const decipherObj = new nodeCrypto.createDecipheriv('aes-' + algo.substr(3, 3) + '-cfb', key, iv);
return stream.transform(ct, value => new Uint8Array(decipherObj.update(new Buffer(value)))); return stream.transform(ct, value => new Uint8Array(decipherObj.update(Buffer.from(value))));
} }

View File

@ -83,9 +83,9 @@ async function CBC(key) {
}; };
} }
if (util.getNodeCrypto()) { // Node crypto library if (util.getNodeCrypto()) { // Node crypto library
key = new Buffer(key); key = Buffer.from(key);
return async function(pt) { return async function(pt) {
pt = new Buffer(pt); pt = Buffer.from(pt);
const en = new nodeCrypto.createCipheriv('aes-' + (key.length * 8) + '-cbc', key, zeroBlock); const en = new nodeCrypto.createCipheriv('aes-' + (key.length * 8) + '-cbc', key, zeroBlock);
const ct = en.update(pt); const ct = en.update(pt);
return new Uint8Array(ct); return new Uint8Array(ct);

View File

@ -61,10 +61,10 @@ async function CTR(key) {
}; };
} }
if (util.getNodeCrypto()) { // Node crypto library if (util.getNodeCrypto()) { // Node crypto library
key = new Buffer(key); key = Buffer.from(key);
return async function(pt, iv) { return async function(pt, iv) {
pt = new Buffer(pt); pt = Buffer.from(pt);
iv = new Buffer(iv); iv = Buffer.from(iv);
const en = new nodeCrypto.createCipheriv('aes-' + (key.length * 8) + '-ctr', key, iv); const en = new nodeCrypto.createCipheriv('aes-' + (key.length * 8) + '-ctr', key, iv);
const ct = Buffer.concat([en.update(pt), en.final()]); const ct = Buffer.concat([en.update(pt), en.final()]);
return new Uint8Array(ct); return new Uint8Array(ct);

View File

@ -80,13 +80,13 @@ async function GCM(cipher, key) {
} }
if (util.getNodeCrypto()) { // Node crypto library if (util.getNodeCrypto()) { // Node crypto library
key = new Buffer(key); key = Buffer.from(key);
return { return {
encrypt: async function(pt, iv, adata = new Uint8Array()) { encrypt: async function(pt, iv, adata = new Uint8Array()) {
pt = new Buffer(pt); pt = Buffer.from(pt);
iv = new Buffer(iv); iv = Buffer.from(iv);
adata = new Buffer(adata); adata = Buffer.from(adata);
const en = new nodeCrypto.createCipheriv('aes-' + (key.length * 8) + '-gcm', key, iv); const en = new nodeCrypto.createCipheriv('aes-' + (key.length * 8) + '-gcm', key, iv);
en.setAAD(adata); en.setAAD(adata);
const ct = Buffer.concat([en.update(pt), en.final(), en.getAuthTag()]); // append auth tag to ciphertext const ct = Buffer.concat([en.update(pt), en.final(), en.getAuthTag()]); // append auth tag to ciphertext
@ -94,9 +94,9 @@ async function GCM(cipher, key) {
}, },
decrypt: async function(ct, iv, adata = new Uint8Array()) { decrypt: async function(ct, iv, adata = new Uint8Array()) {
ct = new Buffer(ct); ct = Buffer.from(ct);
iv = new Buffer(iv); iv = Buffer.from(iv);
adata = new Buffer(adata); adata = Buffer.from(adata);
const de = new nodeCrypto.createDecipheriv('aes-' + (key.length * 8) + '-gcm', key, iv); const de = new nodeCrypto.createDecipheriv('aes-' + (key.length * 8) + '-gcm', key, iv);
de.setAAD(adata); de.setAAD(adata);
de.setAuthTag(ct.slice(ct.length - tagLength, ct.length)); // read auth tag at end of ciphertext de.setAuthTag(ct.slice(ct.length - tagLength, ct.length)); // read auth tag at end of ciphertext

View File

@ -30,7 +30,7 @@ function node_hash(type) {
return async function (data) { return async function (data) {
const shasum = nodeCrypto.createHash(type); const shasum = nodeCrypto.createHash(type);
return stream.transform(data, value => { return stream.transform(data, value => {
shasum.update(new Buffer(value)); shasum.update(Buffer.from(value));
}, () => new Uint8Array(shasum.digest())); }, () => new Uint8Array(shasum.digest()));
}; };
} }

View File

@ -99,11 +99,10 @@ function s2r(t, u = false) {
/** /**
* Convert radix-64 to binary array * Convert radix-64 to binary array
* @param {String | ReadableStream<String>} t radix-64 string to convert * @param {String | ReadableStream<String>} t radix-64 string to convert
* @param {bool} u if true, input is interpreted as URL-safe
* @returns {Uint8Array | ReadableStream<Uint8Array>} binary array version of input string * @returns {Uint8Array | ReadableStream<Uint8Array>} binary array version of input string
* @static * @static
*/ */
function r2s(t, u) { function r2s(t) {
// TODO check atob alternative // TODO check atob alternative
let c; let c;

View File

@ -1344,19 +1344,12 @@ export async function generate(options) {
throw new Error('Not valid curve.'); throw new Error('Not valid curve.');
} }
if (options.curve === enums.curve.ed25519 || options.curve === enums.curve.curve25519) { if (options.curve === enums.curve.ed25519 || options.curve === enums.curve.curve25519) {
options.curve = options.sign ? enums.curve.ed25519 : enums.curve.curve25519;
}
if (options.sign) { if (options.sign) {
options.algorithm = enums.publicKey.eddsa; options.algorithm = options.curve === enums.curve.ed25519 ? enums.publicKey.eddsa : enums.publicKey.ecdsa;
options.curve = enums.curve.ed25519;
} else { } else {
options.algorithm = enums.publicKey.ecdh; options.algorithm = enums.publicKey.ecdh;
options.curve = enums.curve.curve25519;
}
} else {
if (options.sign) {
options.algorithm = enums.publicKey.ecdsa;
} else {
options.algorithm = enums.publicKey.ecdh;
}
} }
} else if (options.numBits) { } else if (options.numBits) {
options.algorithm = enums.publicKey.rsa_encrypt_sign; options.algorithm = enums.publicKey.rsa_encrypt_sign;

View File

@ -20,7 +20,7 @@
* @requires enums * @requires enums
*/ */
import publicKey from './public_key'; import PublicKey from './public_key';
import enums from '../enums'; import enums from '../enums';
/** /**
@ -34,11 +34,11 @@ import enums from '../enums';
* @extends module:packet.PublicKey * @extends module:packet.PublicKey
*/ */
function PublicSubkey() { function PublicSubkey() {
publicKey.call(this); PublicKey.call(this);
this.tag = enums.packet.publicSubkey; this.tag = enums.packet.publicSubkey;
} }
PublicSubkey.prototype = new publicKey(); PublicSubkey.prototype = new PublicKey();
PublicSubkey.prototype.constructor = PublicSubkey; PublicSubkey.prototype.constructor = PublicSubkey;
export default PublicSubkey; export default PublicSubkey;

View File

@ -24,7 +24,7 @@
* @requires util * @requires util
*/ */
import publicKey from './public_key'; import PublicKey from './public_key';
import type_keyid from '../type/keyid.js'; import type_keyid from '../type/keyid.js';
import type_s2k from '../type/s2k'; import type_s2k from '../type/s2k';
import crypto from '../crypto'; import crypto from '../crypto';
@ -40,7 +40,7 @@ import util from '../util';
* @extends module:packet.PublicKey * @extends module:packet.PublicKey
*/ */
function SecretKey(date = new Date()) { function SecretKey(date = new Date()) {
publicKey.call(this, date); PublicKey.call(this, date);
/** /**
* Packet type * Packet type
* @type {module:enums.packet} * @type {module:enums.packet}
@ -76,7 +76,7 @@ function SecretKey(date=new Date()) {
this.aead = 'eax'; this.aead = 'eax';
} }
SecretKey.prototype = new publicKey(); SecretKey.prototype = new PublicKey();
SecretKey.prototype.constructor = SecretKey; SecretKey.prototype.constructor = SecretKey;
// Helper function // Helper function

View File

@ -20,7 +20,7 @@
* @requires enums * @requires enums
*/ */
import secretKey from './secret_key'; import SecretKey from './secret_key';
import enums from '../enums'; import enums from '../enums';
/** /**
@ -31,11 +31,11 @@ import enums from '../enums';
* @extends module:packet.SecretKey * @extends module:packet.SecretKey
*/ */
function SecretSubkey(date = new Date()) { function SecretSubkey(date = new Date()) {
secretKey.call(this, date); SecretKey.call(this, date);
this.tag = enums.packet.secretSubkey; this.tag = enums.packet.secretSubkey;
} }
SecretSubkey.prototype = new secretKey(); SecretSubkey.prototype = new SecretKey();
SecretSubkey.prototype.constructor = SecretSubkey; SecretSubkey.prototype.constructor = SecretSubkey;
export default SecretSubkey; export default SecretSubkey;