Installations
npm install eciesjs
Score
99.4
Supply Chain
98.6
Quality
90.1
Maintenance
100
Vulnerability
100
License
Releases
Contributors
Developer
Developer Guide
Module System
CommonJS, ESM
Min. Node Version
>=16
Typescript Support
Yes
Node Version
22.11.0
NPM Version
10.9.0
Statistics
139 Stars
630 Commits
17 Forks
5 Watching
3 Branches
4 Contributors
Updated on 24 Nov 2024
Languages
TypeScript (86.73%)
JavaScript (9.38%)
CSS (3.12%)
HTML (0.64%)
Shell (0.12%)
Total Downloads
Cumulative downloads
Total Downloads
8,350,983
Last day
-20%
59,812
Compared to previous day
Last week
-2.2%
368,232
Compared to previous week
Last month
13.5%
1,564,214
Compared to previous month
Last year
2,717.2%
7,985,329
Compared to previous year
Daily Downloads
Weekly Downloads
Monthly Downloads
Yearly Downloads
Dev Dependencies
5
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 may go there for detailed documentation and learn the mechanism under the hood.
Install
1npm install eciesjs
We recommend using the latest Node runtime although it's still possible to install on old versions (as long as 16+).
Quick Start
1import { PrivateKey, decrypt, encrypt } from "eciesjs"; 2 3const sk = new PrivateKey() 4const data = Buffer.from("hello world🌍") 5const decrypted = decrypt(sk.secret, encrypt(sk.publicKey.toBytes(), data)) 6console.log(Buffer.from(decrypted).toString())
Or run the example code:
1$ pnpm install && pnpm build && cd example/runtime && pnpm install && node main.js 2hello world🌍
See Configuration to control with more granularity.
Multi-platform Support
Browser
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
.
Bun/Deno
For bun/deno, see example/runtime
. There are some limitations currently, mentioned in @ecies/ciphers
:
node:crypto
'sxchacha20
does not work on bun (pure JS implementation is used instead)aes-256-gcm
only works with 12 bytes nonce on deno (deno is not handling package exports correctly)
React Native
See the React Native demo.
API
encrypt(receiverRawPK: string | Uint8Array, msg: Uint8Array): Buffer
Parameters:
- receiverRawPK - Receiver's public key, hex string or Uint8Array
- msg - Data to encrypt
Returns: Buffer
decrypt(receiverRawSK: string | Uint8Array, msg: Uint8Array): Buffer
Parameters:
- receiverRawSK - Receiver's private key, hex string or Uint8Array
- msg - Data to decrypt
Returns: Buffer
PrivateKey
- Methods
1static fromHex(hex: string): PrivateKey; 2constructor(secret?: Uint8Array); 3toHex(): string; 4encapsulate(pk: PublicKey, compressed?: boolean): Uint8Array; 5multiply(pk: PublicKey, compressed?: boolean): Uint8Array; 6equals(other: PrivateKey): boolean;
- Properties
1get secret(): Buffer; 2readonly publicKey: PublicKey;
PublicKey
- Methods
1static fromHex(hex: string): PublicKey; 2constructor(data: Uint8Array); 3toBytes(compressed?: boolean): Uint8Array; 4toHex(compressed?: boolean): string; 5decapsulate(sk: PrivateKey, compressed?: boolean): Uint8Array; 6equals(other: PublicKey): boolean;
- Properties
1/** @deprecated - use `PublicKey.toBytes(false)` instead. You may also need `Buffer.from`. */ 2get uncompressed(): Buffer; 3/** @deprecated - use `PublicKey.toBytes()` instead. You may also need `Buffer.from`. */ 4get compressed(): Buffer;
Configuration
Following configurations are available.
- Elliptic curve: secp256k1 or curve25519 (x25519/ed25519)
- Ephemeral key format in the payload: compressed or uncompressed (only for secp256k1)
- Shared elliptic curve key format in the key derivation: compressed or uncompressed (only for secp256k1)
- Symmetric cipher algorithm: AES-256-GCM or XChaCha20-Poly1305
- Symmetric nonce length: 12 or 16 bytes (only for AES-256-GCM)
For compatibility, make sure different applications share the same configuration.
1export type EllipticCurve = "secp256k1" | "x25519" | "ed25519"; 2export type SymmetricAlgorithm = "aes-256-gcm" | "xchacha20"; 3export type NonceLength = 12 | 16; 4 5class Config { 6 ellipticCurve: EllipticCurve = "secp256k1"; 7 isEphemeralKeyCompressed: boolean = false; 8 isHkdfKeyCompressed: boolean = false; 9 symmetricAlgorithm: SymmetricAlgorithm = "aes-256-gcm"; 10 symmetricNonceLength: NonceLength = 16; 11} 12 13export const ECIES_CONFIG = new Config();
Elliptic curve configuration
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.
Secp256k1-specific configuration
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)
.
Symmetric cipher configuration
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
.
Which configuration should I choose?
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.
Security Audit
Following dependencies are audited:
Changelog
See CHANGELOG.md.
No vulnerabilities found.
Reason
16 commit(s) and 3 issue activity found in the last 90 days -- score normalized to 10
Reason
no dangerous workflow patterns detected
Reason
no binaries found in the repo
Reason
license file detected
Details
- Info: project has a license file: LICENSE:0
- Info: FSF or OSI recognized license: MIT License: LICENSE:0
Reason
packaging workflow detected
Details
- Info: Project packages its releases by way of GitHub Actions.: .github/workflows/cd.yml:8
Reason
0 existing vulnerabilities detected
Reason
Found 1/19 approved changesets -- score normalized to 0
Reason
detected GitHub workflow tokens with excessive permissions
Details
- Info: jobLevel 'contents' permission set to 'read': .github/workflows/cd.yml:11
- Warn: no topLevel permission defined: .github/workflows/cd.yml:1
- Warn: no topLevel permission defined: .github/workflows/ci.yml:1
- Info: no jobLevel write permissions found
Reason
dependency not pinned by hash detected -- score normalized to 0
Details
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/cd.yml:14: update your workflow using https://app.stepsecurity.io/secureworkflow/ecies/js/cd.yml/master?enable=pin
- Warn: third-party GitHubAction not pinned by hash: .github/workflows/cd.yml:15: update your workflow using https://app.stepsecurity.io/secureworkflow/ecies/js/cd.yml/master?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/cd.yml:16: update your workflow using https://app.stepsecurity.io/secureworkflow/ecies/js/cd.yml/master?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/ci.yml:18: update your workflow using https://app.stepsecurity.io/secureworkflow/ecies/js/ci.yml/master?enable=pin
- Warn: third-party GitHubAction not pinned by hash: .github/workflows/ci.yml:19: update your workflow using https://app.stepsecurity.io/secureworkflow/ecies/js/ci.yml/master?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/ci.yml:20: update your workflow using https://app.stepsecurity.io/secureworkflow/ecies/js/ci.yml/master?enable=pin
- Warn: third-party GitHubAction not pinned by hash: .github/workflows/ci.yml:29: update your workflow using https://app.stepsecurity.io/secureworkflow/ecies/js/ci.yml/master?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/ci.yml:60: update your workflow using https://app.stepsecurity.io/secureworkflow/ecies/js/ci.yml/master?enable=pin
- Warn: third-party GitHubAction not pinned by hash: .github/workflows/ci.yml:61: update your workflow using https://app.stepsecurity.io/secureworkflow/ecies/js/ci.yml/master?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/ci.yml:62: update your workflow using https://app.stepsecurity.io/secureworkflow/ecies/js/ci.yml/master?enable=pin
- Warn: third-party GitHubAction not pinned by hash: .github/workflows/ci.yml:68: update your workflow using https://app.stepsecurity.io/secureworkflow/ecies/js/ci.yml/master?enable=pin
- Warn: third-party GitHubAction not pinned by hash: .github/workflows/ci.yml:72: update your workflow using https://app.stepsecurity.io/secureworkflow/ecies/js/ci.yml/master?enable=pin
- Info: 0 out of 6 GitHub-owned GitHubAction dependencies pinned
- Info: 0 out of 6 third-party GitHubAction dependencies pinned
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
- Warn: no security policy file detected
- Warn: no security file to analyze
- Warn: no security file to analyze
- Warn: no security file to analyze
Reason
project is not fuzzed
Details
- Warn: no fuzzer integrations found
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
- Warn: 0 commits out of 30 are checked with a SAST tool
Score
5.2
/10
Last Scanned on 2024-11-25
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More