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:
parent
b23ee190c7
commit
5d9629d6a3
19
.eslintrc.js
19
.eslintrc.js
|
@ -295,7 +295,7 @@ module.exports = {
|
|||
"error",
|
||||
"never"
|
||||
],
|
||||
"space-infix-ops": "off",
|
||||
"space-infix-ops": "error",
|
||||
"space-unary-ops": "error",
|
||||
"spaced-comment": "off",
|
||||
"strict": "off",
|
||||
|
@ -315,6 +315,13 @@ module.exports = {
|
|||
"never"
|
||||
],
|
||||
"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:
|
||||
"camelcase": 0,
|
||||
|
@ -330,17 +337,9 @@ module.exports = {
|
|||
"no-use-before-define": [ 2, { "functions": false, "classes": true, "variables": false }],
|
||||
"no-unused-expressions": [ 2, { "allowShortCircuit": true } ],
|
||||
"no-constant-condition": [ 2, { "checkLoops": false } ],
|
||||
"new-cap": [ 2, { "properties": false, "capIsNewExceptionPattern": "CMAC|CBC|OMAC|CTR", "newIsCapExceptionPattern": "type|hash*"}],
|
||||
|
||||
// Custom warnings:
|
||||
"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
|
||||
}
|
||||
};
|
||||
|
|
|
@ -137,15 +137,15 @@ async function webEncrypt(algo, key, pt, iv) {
|
|||
}
|
||||
|
||||
function nodeEncrypt(algo, key, pt, iv) {
|
||||
key = new Buffer(key);
|
||||
iv = new Buffer(iv);
|
||||
key = Buffer.from(key);
|
||||
iv = Buffer.from(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) {
|
||||
key = new Buffer(key);
|
||||
iv = new Buffer(iv);
|
||||
key = Buffer.from(key);
|
||||
iv = Buffer.from(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))));
|
||||
}
|
||||
|
|
|
@ -83,9 +83,9 @@ async function CBC(key) {
|
|||
};
|
||||
}
|
||||
if (util.getNodeCrypto()) { // Node crypto library
|
||||
key = new Buffer(key);
|
||||
key = Buffer.from(key);
|
||||
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 ct = en.update(pt);
|
||||
return new Uint8Array(ct);
|
||||
|
|
|
@ -61,10 +61,10 @@ async function CTR(key) {
|
|||
};
|
||||
}
|
||||
if (util.getNodeCrypto()) { // Node crypto library
|
||||
key = new Buffer(key);
|
||||
key = Buffer.from(key);
|
||||
return async function(pt, iv) {
|
||||
pt = new Buffer(pt);
|
||||
iv = new Buffer(iv);
|
||||
pt = Buffer.from(pt);
|
||||
iv = Buffer.from(iv);
|
||||
const en = new nodeCrypto.createCipheriv('aes-' + (key.length * 8) + '-ctr', key, iv);
|
||||
const ct = Buffer.concat([en.update(pt), en.final()]);
|
||||
return new Uint8Array(ct);
|
||||
|
|
|
@ -80,13 +80,13 @@ async function GCM(cipher, key) {
|
|||
}
|
||||
|
||||
if (util.getNodeCrypto()) { // Node crypto library
|
||||
key = new Buffer(key);
|
||||
key = Buffer.from(key);
|
||||
|
||||
return {
|
||||
encrypt: async function(pt, iv, adata = new Uint8Array()) {
|
||||
pt = new Buffer(pt);
|
||||
iv = new Buffer(iv);
|
||||
adata = new Buffer(adata);
|
||||
pt = Buffer.from(pt);
|
||||
iv = Buffer.from(iv);
|
||||
adata = Buffer.from(adata);
|
||||
const en = new nodeCrypto.createCipheriv('aes-' + (key.length * 8) + '-gcm', key, iv);
|
||||
en.setAAD(adata);
|
||||
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()) {
|
||||
ct = new Buffer(ct);
|
||||
iv = new Buffer(iv);
|
||||
adata = new Buffer(adata);
|
||||
ct = Buffer.from(ct);
|
||||
iv = Buffer.from(iv);
|
||||
adata = Buffer.from(adata);
|
||||
const de = new nodeCrypto.createDecipheriv('aes-' + (key.length * 8) + '-gcm', key, iv);
|
||||
de.setAAD(adata);
|
||||
de.setAuthTag(ct.slice(ct.length - tagLength, ct.length)); // read auth tag at end of ciphertext
|
||||
|
|
|
@ -30,7 +30,7 @@ function node_hash(type) {
|
|||
return async function (data) {
|
||||
const shasum = nodeCrypto.createHash(type);
|
||||
return stream.transform(data, value => {
|
||||
shasum.update(new Buffer(value));
|
||||
shasum.update(Buffer.from(value));
|
||||
}, () => new Uint8Array(shasum.digest()));
|
||||
};
|
||||
}
|
||||
|
|
|
@ -99,11 +99,10 @@ function s2r(t, u = false) {
|
|||
/**
|
||||
* Convert radix-64 to binary array
|
||||
* @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
|
||||
* @static
|
||||
*/
|
||||
function r2s(t, u) {
|
||||
function r2s(t) {
|
||||
// TODO check atob alternative
|
||||
let c;
|
||||
|
||||
|
|
13
src/key.js
13
src/key.js
|
@ -1344,19 +1344,12 @@ export async function generate(options) {
|
|||
throw new Error('Not valid curve.');
|
||||
}
|
||||
if (options.curve === enums.curve.ed25519 || options.curve === enums.curve.curve25519) {
|
||||
options.curve = options.sign ? enums.curve.ed25519 : enums.curve.curve25519;
|
||||
}
|
||||
if (options.sign) {
|
||||
options.algorithm = enums.publicKey.eddsa;
|
||||
options.curve = enums.curve.ed25519;
|
||||
options.algorithm = options.curve === enums.curve.ed25519 ? enums.publicKey.eddsa : enums.publicKey.ecdsa;
|
||||
} else {
|
||||
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) {
|
||||
options.algorithm = enums.publicKey.rsa_encrypt_sign;
|
||||
|
|
|
@ -20,7 +20,7 @@
|
|||
* @requires enums
|
||||
*/
|
||||
|
||||
import publicKey from './public_key';
|
||||
import PublicKey from './public_key';
|
||||
import enums from '../enums';
|
||||
|
||||
/**
|
||||
|
@ -34,11 +34,11 @@ import enums from '../enums';
|
|||
* @extends module:packet.PublicKey
|
||||
*/
|
||||
function PublicSubkey() {
|
||||
publicKey.call(this);
|
||||
PublicKey.call(this);
|
||||
this.tag = enums.packet.publicSubkey;
|
||||
}
|
||||
|
||||
PublicSubkey.prototype = new publicKey();
|
||||
PublicSubkey.prototype = new PublicKey();
|
||||
PublicSubkey.prototype.constructor = PublicSubkey;
|
||||
|
||||
export default PublicSubkey;
|
||||
|
|
|
@ -24,7 +24,7 @@
|
|||
* @requires util
|
||||
*/
|
||||
|
||||
import publicKey from './public_key';
|
||||
import PublicKey from './public_key';
|
||||
import type_keyid from '../type/keyid.js';
|
||||
import type_s2k from '../type/s2k';
|
||||
import crypto from '../crypto';
|
||||
|
@ -40,7 +40,7 @@ import util from '../util';
|
|||
* @extends module:packet.PublicKey
|
||||
*/
|
||||
function SecretKey(date = new Date()) {
|
||||
publicKey.call(this, date);
|
||||
PublicKey.call(this, date);
|
||||
/**
|
||||
* Packet type
|
||||
* @type {module:enums.packet}
|
||||
|
@ -76,7 +76,7 @@ function SecretKey(date=new Date()) {
|
|||
this.aead = 'eax';
|
||||
}
|
||||
|
||||
SecretKey.prototype = new publicKey();
|
||||
SecretKey.prototype = new PublicKey();
|
||||
SecretKey.prototype.constructor = SecretKey;
|
||||
|
||||
// Helper function
|
||||
|
|
|
@ -20,7 +20,7 @@
|
|||
* @requires enums
|
||||
*/
|
||||
|
||||
import secretKey from './secret_key';
|
||||
import SecretKey from './secret_key';
|
||||
import enums from '../enums';
|
||||
|
||||
/**
|
||||
|
@ -31,11 +31,11 @@ import enums from '../enums';
|
|||
* @extends module:packet.SecretKey
|
||||
*/
|
||||
function SecretSubkey(date = new Date()) {
|
||||
secretKey.call(this, date);
|
||||
SecretKey.call(this, date);
|
||||
this.tag = enums.packet.secretSubkey;
|
||||
}
|
||||
|
||||
SecretSubkey.prototype = new secretKey();
|
||||
SecretSubkey.prototype = new SecretKey();
|
||||
SecretSubkey.prototype.constructor = SecretSubkey;
|
||||
|
||||
export default SecretSubkey;
|
||||
|
|
Loading…
Reference in New Issue
Block a user