eciesjs
Advanced tools
Comparing version 0.4.2 to 0.4.3
/// <reference types="node" /> | ||
export declare function aesEncrypt(key: Buffer, plainText: Buffer): Buffer; | ||
export declare function aesDecrypt(key: Buffer, cipherText: Buffer): Buffer; | ||
export declare function deriveKey(master: Buffer): Buffer; | ||
export declare function aesEncrypt(key: Uint8Array, plainText: Uint8Array): Buffer; | ||
export declare function aesDecrypt(key: Uint8Array, cipherText: Uint8Array): Buffer; | ||
export declare function deriveKey(master: Uint8Array): Buffer; |
@@ -14,5 +14,11 @@ "use strict"; | ||
var cipher = (0, crypto_1.createCipheriv)("aes-256-gcm", key, nonce); | ||
var encrypted = Buffer.concat([cipher.update(plainText), cipher.final()]); | ||
var updated = cipher.update(plainText); | ||
var finalized = cipher.final(); | ||
var tag = cipher.getAuthTag(); | ||
return Buffer.concat([nonce, tag, encrypted]); | ||
var payload = new Uint8Array(nonce.length + tag.length + updated.length + finalized.length); | ||
payload.set(nonce); | ||
payload.set(tag, nonce.length); | ||
payload.set(updated, nonce.length + tag.length); | ||
payload.set(finalized, nonce.length + tag.length + updated.length); | ||
return payload; | ||
} | ||
@@ -26,3 +32,8 @@ function _aesDecrypt(key, cipherText, nonceLength) { | ||
decipher.setAuthTag(tag); | ||
return Buffer.concat([decipher.update(ciphered), decipher.final()]); | ||
var updated = decipher.update(ciphered); | ||
var finalized = decipher.final(); | ||
var payload = new Uint8Array(updated.length + finalized.length); | ||
payload.set(updated); | ||
payload.set(finalized, updated.length); | ||
return payload; | ||
} | ||
@@ -32,6 +43,10 @@ function _encrypt(func, key, plainText, nonceLength) { | ||
var cipher = func(key, nonce); | ||
var ciphered = cipher.encrypt(plainText); | ||
var ciphered = cipher.encrypt(plainText); // TAG + encrypted | ||
var encrypted = ciphered.subarray(0, ciphered.length - consts_1.AEAD_TAG_LENGTH); | ||
var tag = ciphered.subarray(-consts_1.AEAD_TAG_LENGTH); | ||
return Buffer.concat([nonce, tag, encrypted]); | ||
var payload = new Uint8Array(nonce.length + tag.length + encrypted.length); | ||
payload.set(nonce); | ||
payload.set(tag, nonce.length); | ||
payload.set(encrypted, nonce.length + tag.length); | ||
return payload; | ||
} | ||
@@ -42,8 +57,8 @@ function _decrypt(func, key, cipherText, nonceLength) { | ||
var tag = cipherText.subarray(nonceLength, nonceTagLength); | ||
var ciphered = cipherText.subarray(nonceTagLength); | ||
var decipher = func(key, nonce); | ||
var res = new Uint8Array(consts_1.AEAD_TAG_LENGTH + ciphered.length); | ||
res.set(ciphered); | ||
res.set(tag, ciphered.length); | ||
return Buffer.from(decipher.decrypt(res)); | ||
var encrypted = cipherText.subarray(nonceTagLength); | ||
var decipher = func(key, Uint8Array.from(nonce)); // to reset byteOffset | ||
var ciphered = new Uint8Array(encrypted.length + consts_1.AEAD_TAG_LENGTH); | ||
ciphered.set(encrypted); | ||
ciphered.set(tag, encrypted.length); | ||
return decipher.decrypt(ciphered); | ||
} | ||
@@ -54,6 +69,6 @@ function aesEncrypt(key, plainText) { | ||
if (algorithm === "aes-256-gcm") { | ||
return _aesEncrypt(key, plainText, (0, config_1.symmetricNonceLength)()); | ||
return Buffer.from(_aesEncrypt(key, plainText, (0, config_1.symmetricNonceLength)())); | ||
} | ||
else if (algorithm === "xchacha20") { | ||
return _encrypt(chacha_1.xchacha20_poly1305, key, plainText, consts_1.XCHACHA20_NONCE_LENGTH); | ||
return Buffer.from(_encrypt(chacha_1.xchacha20_poly1305, key, plainText, consts_1.XCHACHA20_NONCE_LENGTH)); | ||
} | ||
@@ -69,6 +84,6 @@ else { | ||
if (algorithm === "aes-256-gcm") { | ||
return _aesDecrypt(key, cipherText, (0, config_1.symmetricNonceLength)()); | ||
return Buffer.from(_aesDecrypt(key, cipherText, (0, config_1.symmetricNonceLength)())); | ||
} | ||
else if (algorithm === "xchacha20") { | ||
return _decrypt(chacha_1.xchacha20_poly1305, key, cipherText, consts_1.XCHACHA20_NONCE_LENGTH); | ||
return Buffer.from(_decrypt(chacha_1.xchacha20_poly1305, key, cipherText, consts_1.XCHACHA20_NONCE_LENGTH)); | ||
} | ||
@@ -75,0 +90,0 @@ else { |
@@ -14,3 +14,3 @@ { | ||
}, | ||
"version": "0.4.2", | ||
"version": "0.4.3", | ||
"engines": { | ||
@@ -17,0 +17,0 @@ "node": ">=16.0.0" |
23169
403