Comparing version 0.4.1 to 0.4.2
@@ -1,3 +0,3 @@ | ||
export type SymmetricAlgorithm = "aes-256-gcm"; | ||
export type NonceLength = 12 | 16 | 24; | ||
export type SymmetricAlgorithm = "aes-256-gcm" | "xchacha20"; | ||
export type NonceLength = 12 | 16; | ||
declare class Config { | ||
@@ -13,4 +13,4 @@ isEphemeralKeyCompressed: boolean; | ||
export declare const ephemeralKeySize: () => 33 | 65; | ||
export declare const symmetricAlgorithm: () => "aes-256-gcm"; | ||
export declare const symmetricAlgorithm: () => SymmetricAlgorithm; | ||
export declare const symmetricNonceLength: () => NonceLength; | ||
export {}; |
@@ -6,2 +6,3 @@ export declare const COMPRESSED_PUBLIC_KEY_SIZE = 33; | ||
export declare const ONE: bigint; | ||
export declare const XCHACHA20_NONCE_LENGTH = 24; | ||
export declare const AEAD_TAG_LENGTH = 16; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.AEAD_TAG_LENGTH = exports.ONE = exports.SECRET_KEY_LENGTH = exports.ETH_PUBLIC_KEY_SIZE = exports.UNCOMPRESSED_PUBLIC_KEY_SIZE = exports.COMPRESSED_PUBLIC_KEY_SIZE = void 0; | ||
exports.AEAD_TAG_LENGTH = exports.XCHACHA20_NONCE_LENGTH = exports.ONE = exports.SECRET_KEY_LENGTH = exports.ETH_PUBLIC_KEY_SIZE = exports.UNCOMPRESSED_PUBLIC_KEY_SIZE = exports.COMPRESSED_PUBLIC_KEY_SIZE = void 0; | ||
exports.COMPRESSED_PUBLIC_KEY_SIZE = 33; | ||
@@ -9,2 +9,3 @@ exports.UNCOMPRESSED_PUBLIC_KEY_SIZE = 65; | ||
exports.ONE = BigInt(1); | ||
exports.XCHACHA20_NONCE_LENGTH = 24; | ||
exports.AEAD_TAG_LENGTH = 16; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.deriveKey = exports.aesDecrypt = exports.aesEncrypt = void 0; | ||
var chacha_1 = require("@noble/ciphers/chacha"); | ||
var utils_1 = require("@noble/ciphers/webcrypto/utils"); | ||
var hkdf_1 = require("@noble/hashes/hkdf"); | ||
@@ -9,5 +11,5 @@ var sha256_1 = require("@noble/hashes/sha256"); | ||
var consts_1 = require("../consts"); | ||
function _aesEncrypt(key, plainText, algorithm, nonceLength) { | ||
var nonce = (0, crypto_1.randomBytes)(nonceLength); | ||
var cipher = (0, crypto_1.createCipheriv)(algorithm, key, nonce); | ||
function _aesEncrypt(key, plainText, nonceLength) { | ||
var nonce = (0, utils_1.randomBytes)(nonceLength); | ||
var cipher = (0, crypto_1.createCipheriv)("aes-256-gcm", key, nonce); | ||
var encrypted = Buffer.concat([cipher.update(plainText), cipher.final()]); | ||
@@ -17,21 +19,62 @@ var tag = cipher.getAuthTag(); | ||
} | ||
function _aesDecrypt(key, cipherText, algorithm, nonceLength) { | ||
function _aesDecrypt(key, cipherText, nonceLength) { | ||
var nonceTagLength = nonceLength + consts_1.AEAD_TAG_LENGTH; | ||
var nonce = cipherText.subarray(0, nonceLength); | ||
var tag = cipherText.subarray(nonceLength, nonceLength + consts_1.AEAD_TAG_LENGTH); | ||
var ciphered = cipherText.subarray(nonceLength + consts_1.AEAD_TAG_LENGTH); | ||
var decipher = (0, crypto_1.createDecipheriv)(algorithm, key, nonce); | ||
var tag = cipherText.subarray(nonceLength, nonceTagLength); | ||
var ciphered = cipherText.subarray(nonceTagLength); | ||
var decipher = (0, crypto_1.createDecipheriv)("aes-256-gcm", key, nonce); | ||
decipher.setAuthTag(tag); | ||
return Buffer.concat([decipher.update(ciphered), decipher.final()]); | ||
} | ||
function _encrypt(func, key, plainText, nonceLength) { | ||
var nonce = (0, utils_1.randomBytes)(nonceLength); | ||
var cipher = func(key, nonce); | ||
var ciphered = cipher.encrypt(plainText); | ||
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]); | ||
} | ||
function _decrypt(func, key, cipherText, nonceLength) { | ||
var nonceTagLength = nonceLength + consts_1.AEAD_TAG_LENGTH; | ||
var nonce = cipherText.subarray(0, 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)); | ||
} | ||
function aesEncrypt(key, plainText) { | ||
return _aesEncrypt(key, plainText, (0, config_1.symmetricAlgorithm)(), (0, config_1.symmetricNonceLength)()); | ||
// TODO: Rename to symEncrypt | ||
var algorithm = (0, config_1.symmetricAlgorithm)(); | ||
if (algorithm === "aes-256-gcm") { | ||
return _aesEncrypt(key, plainText, (0, config_1.symmetricNonceLength)()); | ||
} | ||
else if (algorithm === "xchacha20") { | ||
return _encrypt(chacha_1.xchacha20_poly1305, key, plainText, consts_1.XCHACHA20_NONCE_LENGTH); | ||
} | ||
else { | ||
throw new Error("Not implemented"); | ||
} | ||
} | ||
exports.aesEncrypt = aesEncrypt; | ||
function aesDecrypt(key, cipherText) { | ||
return _aesDecrypt(key, cipherText, (0, config_1.symmetricAlgorithm)(), (0, config_1.symmetricNonceLength)()); | ||
// TODO: Rename to symDecrypt | ||
var algorithm = (0, config_1.symmetricAlgorithm)(); | ||
if (algorithm === "aes-256-gcm") { | ||
return _aesDecrypt(key, cipherText, (0, config_1.symmetricNonceLength)()); | ||
} | ||
else if (algorithm === "xchacha20") { | ||
return _decrypt(chacha_1.xchacha20_poly1305, key, cipherText, consts_1.XCHACHA20_NONCE_LENGTH); | ||
} | ||
else { | ||
throw new Error("Not implemented"); | ||
} | ||
} | ||
exports.aesDecrypt = aesDecrypt; | ||
function deriveKey(master) { | ||
// 32 bytes shared secret for aes and chacha20 | ||
return Buffer.from((0, hkdf_1.hkdf)(sha256_1.sha256, master, undefined, undefined, 32)); | ||
} | ||
exports.deriveKey = deriveKey; |
@@ -10,2 +10,7 @@ { | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/ecies/js.git" | ||
}, | ||
"version": "0.4.2", | ||
"engines": { | ||
@@ -32,8 +37,4 @@ "node": ">=16.0.0" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/ecies/js.git" | ||
}, | ||
"version": "0.4.1", | ||
"dependencies": { | ||
"@noble/ciphers": "^0.1.4", | ||
"@noble/curves": "^1.1.0" | ||
@@ -40,0 +41,0 @@ }, |
@@ -97,6 +97,9 @@ # eciesjs | ||
```ts | ||
export type SymmetricAlgorithm = "aes-256-gcm" | "xchacha20"; | ||
export type NonceLength = 12 | 16; // bytes. Only for aes-256-gcm | ||
class Config { | ||
isEphemeralKeyCompressed: boolean = false; | ||
isHkdfKeyCompressed: boolean = false; | ||
symmetricAlgorithm: Algorithm = "aes-256-gcm"; // currently we only support aes-256-gcm | ||
symmetricAlgorithm: SymmetricAlgorithm = "aes-256-gcm"; | ||
symmetricNonceLength: NonceLength = 16; | ||
@@ -112,4 +115,6 @@ } | ||
If you set `symmetricNonceLength = 12`, then the nonce of aes-256-gcm would be 12 bytes. | ||
If you set `symmetricAlgorithm = "xchacha20"`, plaintext data will encrypted with XChacha20-Poly1305. | ||
If you set `symmetricNonceLength = 12`, then the nonce of aes-256-gcm would be 12 bytes. XChacha20-Poly1305's nonce is always 24 bytes. | ||
For compatibility, make sure different applications share the same configuration. | ||
@@ -119,2 +124,2 @@ | ||
See [CHANGELOG.md](./CHANGELOG.md) | ||
See [CHANGELOG.md](./CHANGELOG.md). |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
22442
388
123
0
2
+ Added@noble/ciphers@^0.1.4
+ Added@noble/ciphers@0.1.4(transitive)