GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com β€” GitHub - ecies/js: Elliptic Curve Integrated Encryption Scheme for secp256k1/curve25519 in TypeScript

BranchesTags
Open more actions menu

Latest commit

 

History

684 Commits
684 Commits

Folders and files

NameName
Last commit message
Last commit date
.github
.github
 
 
example
example
 
 
scripts
scripts
 
 
src
src
 
 
tests-browser
tests-browser
 
 
tests
tests
 
 
.cspell.jsonc
.cspell.jsonc
 
 
.gitattributes
.gitattributes
 
 
.gitignore
.gitignore
 
 
.npmrc.windows
.npmrc.windows
 
 
CHANGELOG.md
CHANGELOG.md
 
 
DETAILS.md
DETAILS.md
 
 
LICENSE
LICENSE
 
 
README.md
README.md
 
 
_config.yml
_config.yml
 
 
biome.jsonc
biome.jsonc
 
 
package.json
package.json
 
 
pnpm-lock.yaml
pnpm-lock.yaml
 
 
tsconfig.json
tsconfig.json
 
 
vitest.config.mts
vitest.config.mts
 
 

Repository files navigation

eciesjs

Codacy Badge License NPM Package NPM Downloads Bundle size CI Codecov

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.

Install

npm install eciesjs

We recommend using the latest JavaScript/TypeScript runtime although it's still possible to install on some old versions.

This library supports multiple platforms (browser, node, bun/deno, react native), see Multi-platform Support.

For security, see Security.

Quick Start

// example/runtime/quick-start.js
import { PrivateKey, decrypt, encrypt } from "eciesjs";

const encoder = new TextEncoder();
const decoder = new TextDecoder();

const sk = new PrivateKey();
const data = encoder.encode("hello world🌍");
const decrypted = decrypt(sk.secret, encrypt(sk.publicKey.toBytes(), data));
console.log(decoder.decode(decrypted));

Or run the example code:

$ pnpm install && pnpm build && cd example/runtime && pnpm install && node quick-start.js
hello world🌍

See Configuration to control with more granularity.

Sponsors

dotenvx

API

encrypt(receiverRawPK: string | Uint8Array, data: Uint8Array, config?: Config): Uint8Array

Parameters:

  • receiverRawPK - Receiver's public key, hex string or Uint8Array
  • data - Data to encrypt
  • config - Optional configuration (defaults to ECIES_CONFIG)

Returns: Uint8Array

decrypt(receiverRawSK: string | Uint8Array, data: Uint8Array, config?: Config): Uint8Array

Parameters:

  • receiverRawSK - Receiver's private key, hex string or Uint8Array
  • data - Data to decrypt
  • config - Optional configuration (defaults to ECIES_CONFIG)

Returns: Uint8Array

PrivateKey

  • Methods
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;
  • Properties
get secret(): Uint8Array;
readonly publicKey: PublicKey;

PublicKey

  • Methods
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;

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.

export type EllipticCurve = "secp256k1" | "x25519" | "ed25519";
export type SymmetricAlgorithm = "aes-256-gcm" | "xchacha20";
export type NonceLength = 12 | 16;

export 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();

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.

Multi-platform Support

Fully Supported
Node βœ…
Bun βœ…
Deno βœ… (see below)
Browser βœ…
React Native βœ…

Via @ecies/ciphers, node:crypto's native implementation of AES-256-GCM and XChaCha20-Poly1305 is chosen if available.

Browser

This library is browser-friendly, check the example/browser directory for details. You can check the online demo as well.

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:

  • chacha20-poly1305's pure JS implementation is used on bun (node:crypto's chacha20-poly1305 is not available due to lack of implementation);
  • eciesjs needs deno 2.7.10 or later (engines in package.json), and you may need to run it with --conditions deno.

React Native

See the React Native demo.

Security

To mitigate security risks, such as supply chain attacks and zero-day vulnerabilities, we only use node:crypto and these audited dependencies:

  • noble-curves
  • noble-hashes
  • noble-ciphers

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.

Changelog

See CHANGELOG.md.

About

Elliptic Curve Integrated Encryption Scheme for secp256k1/curve25519 in TypeScript

ecies.org/js

Topics

bitcoincryptocurrencycryptographyecieselliptic-curve-cryptographyethereumjavascriptsecp256k1typescript

Resources

Readme
MIT license
Activity
Custom properties

Stars

162 stars

Watchers

3 watching

Forks

13 forks
Report repository

Releases

Sponsor this project

Packages

Used by

Contributors

Languages

You are visitor 01402775 β€” GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800Γ—600. No cookies required.