Remove unnecessary Buffer.from calls
This commit is contained in:
parent
06aef92752
commit
97e9cdefe9
|
@ -10,7 +10,6 @@ import util from '../util';
|
|||
|
||||
const webCrypto = util.getWebCrypto();
|
||||
const nodeCrypto = util.getNodeCrypto();
|
||||
const Buffer = util.getNodeBuffer();
|
||||
|
||||
|
||||
/**
|
||||
|
@ -82,9 +81,7 @@ async function CBC(key) {
|
|||
};
|
||||
}
|
||||
if (util.getNodeCrypto()) { // Node crypto library
|
||||
key = Buffer.from(key);
|
||||
return async function(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);
|
||||
|
|
|
@ -19,13 +19,12 @@ import defaultConfig from '../../config';
|
|||
|
||||
const webCrypto = util.getWebCrypto();
|
||||
const nodeCrypto = util.getNodeCrypto();
|
||||
const Buffer = util.getNodeBuffer();
|
||||
|
||||
function nodeHash(type) {
|
||||
return async function (data) {
|
||||
const shasum = nodeCrypto.createHash(type);
|
||||
return stream.transform(data, value => {
|
||||
shasum.update(Buffer.from(value));
|
||||
shasum.update(value);
|
||||
}, () => new Uint8Array(shasum.digest()));
|
||||
};
|
||||
}
|
||||
|
|
|
@ -30,7 +30,6 @@ import util from '../../util';
|
|||
|
||||
const webCrypto = util.getWebCrypto();
|
||||
const nodeCrypto = util.getNodeCrypto();
|
||||
const Buffer = util.getNodeBuffer();
|
||||
|
||||
const knownAlgos = nodeCrypto ? nodeCrypto.getCiphers() : [];
|
||||
const nodeAlgos = {
|
||||
|
@ -149,15 +148,11 @@ async function webEncrypt(algo, key, pt, iv) {
|
|||
}
|
||||
|
||||
function nodeEncrypt(algo, key, pt, iv) {
|
||||
key = Buffer.from(key);
|
||||
iv = Buffer.from(iv);
|
||||
const cipherObj = new nodeCrypto.createCipheriv(nodeAlgos[algo], key, iv);
|
||||
return stream.transform(pt, value => new Uint8Array(cipherObj.update(Buffer.from(value))));
|
||||
return stream.transform(pt, value => new Uint8Array(cipherObj.update(value)));
|
||||
}
|
||||
|
||||
function nodeDecrypt(algo, key, ct, iv) {
|
||||
key = Buffer.from(key);
|
||||
iv = Buffer.from(iv);
|
||||
const decipherObj = new nodeCrypto.createDecipheriv(nodeAlgos[algo], key, iv);
|
||||
return stream.transform(ct, value => new Uint8Array(decipherObj.update(Buffer.from(value))));
|
||||
return stream.transform(ct, value => new Uint8Array(decipherObj.update(value)));
|
||||
}
|
||||
|
|
|
@ -59,10 +59,7 @@ async function CTR(key) {
|
|||
};
|
||||
}
|
||||
if (util.getNodeCrypto()) { // Node crypto library
|
||||
key = Buffer.from(key);
|
||||
return async function(pt, 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);
|
||||
|
|
|
@ -79,13 +79,8 @@ async function GCM(cipher, key) {
|
|||
}
|
||||
|
||||
if (util.getNodeCrypto()) { // Node crypto library
|
||||
key = Buffer.from(key);
|
||||
|
||||
return {
|
||||
encrypt: async function(pt, iv, adata = new Uint8Array()) {
|
||||
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
|
||||
|
@ -93,9 +88,6 @@ async function GCM(cipher, key) {
|
|||
},
|
||||
|
||||
decrypt: async function(ct, iv, adata = new Uint8Array()) {
|
||||
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
|
||||
|
|
Loading…
Reference in New Issue
Block a user