
Research
/Security News
60 Malicious Ruby Gems Used in Targeted Credential Theft Campaign
A RubyGems malware campaign used 60 malicious packages posing as automation tools to steal credentials from social media and marketing tool users.
The eciesjs npm package provides functionalities for Elliptic Curve Integrated Encryption Scheme (ECIES) using the secp256k1 curve. It allows for secure encryption and decryption of messages using elliptic curve cryptography.
Key Generation
This feature allows you to generate a new private key and derive the corresponding public key using the secp256k1 curve.
const ecies = require('eciesjs');
const privateKey = ecies.generatePrivateKey();
const publicKey = ecies.getPublicKey(privateKey);
console.log('Private Key:', privateKey.toString('hex'));
console.log('Public Key:', publicKey.toString('hex'));
Encryption
This feature allows you to encrypt a message using a given public key. The encrypted message is returned as a Buffer.
const ecies = require('eciesjs');
const publicKey = '04bfcab2...'; // Replace with actual public key
const message = Buffer.from('Hello, world!');
const encrypted = ecies.encrypt(publicKey, message);
console.log('Encrypted Message:', encrypted.toString('hex'));
Decryption
This feature allows you to decrypt an encrypted message using a given private key. The decrypted message is returned as a Buffer.
const ecies = require('eciesjs');
const privateKey = 'c87509a1...'; // Replace with actual private key
const encryptedMessage = Buffer.from('...'); // Replace with actual encrypted message
const decrypted = ecies.decrypt(privateKey, encryptedMessage);
console.log('Decrypted Message:', decrypted.toString());
The eccrypto package provides similar functionalities for elliptic curve cryptography, including key generation, encryption, and decryption. It also uses the secp256k1 curve and offers additional features like signing and verification of messages.
The elliptic package is a comprehensive library for elliptic curve cryptography. It supports multiple curves, including secp256k1, and provides functionalities for key generation, encryption, decryption, signing, and verification. It is more versatile compared to eciesjs.
The secp256k1 package is a specialized library for the secp256k1 elliptic curve. It provides low-level cryptographic operations, including key generation, signing, and verification. It is more focused on performance and low-level operations compared to eciesjs.
Elliptic Curve Integrated Encryption Scheme for secp256k1/curve25519 in TypeScript.
This is the JavaScript/TypeScript version of eciespy with a built-in class-like secp256k1/curve25519 API.
You can learn the details in DETAILS.md.
npm install eciesjs
We recommend using the latest Node runtime although it's still possible to install on old versions (as long as 16+).
For security, see Security.
import { PrivateKey, decrypt, encrypt } from "eciesjs";
const sk = new PrivateKey();
const data = Buffer.from("hello world🌍");
const decrypted = decrypt(sk.secret, encrypt(sk.publicKey.toBytes(), data));
console.log(Buffer.from(decrypted).toString());
Or run the example code:
$ pnpm install && pnpm build && cd example/runtime && pnpm install && node main.js
hello world🌍
See Configuration to control with more granularity.
This library also supports multiple platforms (browser, node, bun/deno, react native), see Multi-platform Support.
encrypt(receiverRawPK: string | Uint8Array, data: Uint8Array): Buffer
Parameters:
string
or Uint8Array
Returns: Buffer
decrypt(receiverRawSK: string | Uint8Array, data: Uint8Array): Buffer
Parameters:
string
or Uint8Array
Returns: Buffer
PrivateKey
static fromHex(hex: string, curve?: EllipticCurve): PrivateKey;
constructor(secret?: Uint8Array, curve?: EllipticCurve);
toHex(): string;
encapsulate(pk: PublicKey, compressed?: boolean): Uint8Array;
multiply(pk: PublicKey, compressed?: boolean): Uint8Array;
equals(other: PrivateKey): boolean;
get secret(): Buffer;
readonly publicKey: PublicKey;
PublicKey
static fromHex(hex: string, curve?: EllipticCurve): PublicKey;
constructor(data: Uint8Array, curve?: EllipticCurve);
toBytes(compressed?: boolean): Uint8Array;
toHex(compressed?: boolean): string;
decapsulate(sk: PrivateKey, compressed?: boolean): Uint8Array;
equals(other: PublicKey): boolean;
/** @deprecated - use `PublicKey.toBytes(false)` instead. You may also need `Buffer.from`. */
get uncompressed(): Buffer;
/** @deprecated - use `PublicKey.toBytes()` instead. You may also need `Buffer.from`. */
get compressed(): Buffer;
Following configurations are available.
For compatibility, make sure different applications share the same configuration.
export type EllipticCurve = "secp256k1" | "x25519" | "ed25519";
export type SymmetricAlgorithm = "aes-256-gcm" | "xchacha20";
export type NonceLength = 12 | 16;
class Config {
ellipticCurve: EllipticCurve = "secp256k1";
isEphemeralKeyCompressed: boolean = false;
isHkdfKeyCompressed: boolean = false;
symmetricAlgorithm: SymmetricAlgorithm = "aes-256-gcm";
symmetricNonceLength: NonceLength = 16;
}
export const ECIES_CONFIG = new Config();
On ellipticCurve = "x25519"
or ellipticCurve = "ed25519"
, x25519 (key exchange function on curve25519) or ed25519 (signature algorithm on curve25519) will be used for key exchange instead of secp256k1.
In this case, the payload would always be: 32 Bytes + Ciphered
regardless of isEphemeralKeyCompressed
.
If you don't know how to choose between x25519 and ed25519, just use the dedicated key exchange function x25519 for efficiency.
Because any 32-byte data is a valid curve25519 public key, the payload would seem random. This property is excellent for circumventing censorship by adversaries.
On isEphemeralKeyCompressed = true
, the payload would be: 33 Bytes + Ciphered
instead of 65 Bytes + Ciphered
.
On isHkdfKeyCompressed = true
, the hkdf key would be derived from ephemeral public key (compressed) + shared public key (compressed)
instead of ephemeral public key (uncompressed) + shared public key (uncompressed)
.
On symmetricAlgorithm = "xchacha20"
, plaintext data would be encrypted with XChaCha20-Poly1305.
On symmetricNonceLength = 12
, the nonce of AES-256-GCM would be 12 bytes. XChaCha20-Poly1305's nonce is always 24 bytes regardless of symmetricNonceLength
.
For compatibility with other ecies libraries, start with the default (secp256k1 with AES-256-GCM).
For speed and security, pick x25519 with XChaCha20-Poly1305.
If you know exactly what you are doing, configure as you wish or build your own ecies logic with this library.
Fully Supported | |
---|---|
Node | ✅ |
Bun | ✅ |
Deno | ⚠️ (only aes) |
Browser | ✅ |
React Native | ✅ |
Via @ecies/ciphers
, node:crypto
's native implementation of AES-256-GCM and XChaCha20-Poly1305 is chosen if available.
This library is browser-friendly, check the example/browser
directory for details. The online demo is hosted here.
Currently it's necessary to polyfill Buffer
for backward compatibility. From v0.5.0, it can run in browsers as is.
If you want a WASM version to run directly in modern browsers or on some blockchains, you can also try ecies-wasm
.
For bun/deno, see example/runtime
. There are some limitations currently, mentioned in @ecies/ciphers
:
chacha20-poly1305
's pure JS implementation is used on bun (node:crypto
's chacha20-poly1305
is not available due to lack of implementation);chacha20-poly1305
does not work on deno. If you found such a problem, try to upgrade deno to the latest version (no guarantee whether it works though).See the React Native demo.
To mitigate security risks, such as supply chain attacks and zero-day vulnerabilities, we only use node:crypto
and these audited dependencies:
Every release is built on GitHub Actions with provenance.
This library is fully auditable as well. We're seeking funding for a professional third-party security audit to verify implementation and identify potential vulnerabilities.
If you rely on this library or value secure open-source cryptography, please consider donating to help fund this audit.
See CHANGELOG.md.
0.4.15
curve
configurable in keys
and utils
via argumentFAQs
Elliptic Curve Integrated Encryption Scheme for secp256k1/curve25519
The npm package eciesjs receives a total of 752,339 weekly downloads. As such, eciesjs popularity was classified as popular.
We found that eciesjs demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Research
/Security News
A RubyGems malware campaign used 60 malicious packages posing as automation tools to steal credentials from social media and marketing tool users.
Security News
The CNA Scorecard ranks CVE issuers by data completeness, revealing major gaps in patch info and software identifiers across thousands of vulnerabilities.
Research
/Security News
Two npm packages masquerading as WhatsApp developer libraries include a kill switch that deletes all files if the phone number isn’t whitelisted.