What is eciesjs?
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.
What are eciesjs's main functionalities?
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());
Other packages similar to eciesjs
eccrypto
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.
elliptic
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.
secp256k1
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.
eciesjs
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.
Install
Install with npm install eciesjs
Quick Start
> 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(receiverRawPub: string | Buffer, msg: Buffer): Buffer
Parameters:
- receiverRawPub - Receiver's secp256k1 public key, hex string or buffer
- msg - Data to encrypt
Returns: Buffer
decrypt(receiverRawPrv: string | Buffer, msg: Buffer): Buffer
Parameters:
- receiverRawPrv - Receiver's secp256k1 private key, hex string or buffer
- msg - Data to decrypt
Returns: Buffer
PrivateKey
static fromHex(hex: string): PrivateKey;
constructor(secret?: Buffer);
toHex(): string;
encapsulate(pub: PublicKey): Buffer;
multiply(pub: PublicKey): Buffer;
equals(other: PrivateKey): boolean;
readonly secret: Buffer;
readonly publicKey: PublicKey;
PublicKey
static fromHex(hex: string): PublicKey;
constructor(buffer: Buffer);
toHex(compressed?: boolean): string;
decapsulate(priv: PrivateKey): Buffer;
equals(other: PublicKey): boolean;
readonly uncompressed: Buffer;
readonly compressed: Buffer;