Socket
Socket
Sign inDemoInstall

eciesjs

Package Overview
Dependencies
3
Maintainers
1
Versions
34
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    eciesjs

Elliptic Curve Integrated Encryption Scheme for secp256k1


Version published
Maintainers
1
Install size
2.45 MB
Created

Readme

Source

eciesjs

Codacy Badge License Npm Package CI Codecov

Elliptic Curve Integrated Encryption Scheme for secp256k1 in TypeScript.

This is the JavaScript/TypeScript version of eciespy with a built-in class-like secp256k1 API, you may go there for detailed documentation and learn the mechanism under the hood.

If you want a WASM version to run directly in modern browsers or on some blockchains, check ecies-wasm.

Install

npm install eciesjs

Quick Start

Run the code below with npx ts-node.

> import { encrypt, decrypt, PrivateKey } from 'eciesjs'
> const k1 = new PrivateKey()
> const data = Buffer.from('this is a test')
> decrypt(k1.toHex(), encrypt(k1.publicKey.toHex(), data)).toString()
'this is a test'

API

encrypt(receiverRawPK: string | Buffer, msg: Buffer): Buffer

Parameters:

  • receiverRawPK - Receiver's secp256k1 public key, hex string or buffer
  • msg - Data to encrypt

Returns: Buffer

decrypt(receiverRawSK: string | Buffer, msg: Buffer): Buffer

Parameters:

  • receiverRawSK - Receiver's secp256k1 private key, hex string or buffer
  • msg - Data to decrypt

Returns: Buffer

PrivateKey

  • Methods
static fromHex(hex: string): PrivateKey;
constructor(secret?: Buffer);
toHex(): string;
encapsulate(pub: PublicKey): Buffer;
multiply(pub: PublicKey): Buffer;
equals(other: PrivateKey): boolean;
  • Properties
readonly secret: Buffer;
readonly publicKey: PublicKey;

PublicKey

  • Methods
static fromHex(hex: string): PublicKey;
constructor(buffer: Buffer);
toHex(compressed?: boolean): string;
decapsulate(priv: PrivateKey): Buffer;
equals(other: PublicKey): boolean;
  • Properties
readonly uncompressed: Buffer;
readonly compressed: Buffer;

Configuration

Ephemeral key format in the payload and shared key in the key derivation can be configured as compressed or uncompressed format.

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: SymmetricAlgorithm = "aes-256-gcm";
  symmetricNonceLength: NonceLength = 16;
}

export const ECIES_CONFIG = new Config();

For example, if you set isEphemeralKeyCompressed = true, the payload would be like: 33 Bytes + AES instead of 65 Bytes + AES.

If you set 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).

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.

Changelog

See CHANGELOG.md.

Keywords

FAQs

Last updated on 28 Jul 2023

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.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc