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, written in TypeScript with minimal dependencies.
This is the JavaScript/TypeScript version of eciespy, you may go there for documentation about detailed mechanism.
Install
Install with npm install eciesjs
(secp256k1
is the only dependency).
Quick Start
> import { encrypt, decrypt, PrivateKey, utils } 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'
> utils.sha256(Buffer.from('0')).slice(0, 8)
<Buffer 5f ec eb 66 ff c8 6f 38>
> const k2 = new PrivateKey()
> k1.ecdh(k2.publicKey).equals(k2.ecdh(k1.publicKey))
true
API
encrypt(receiverPubhex: string, msg: Buffer): Buffer
Parameters:
- receiverPubhex - Receiver's secp256k1 public key hex string
- msg - Data to encrypt
Returns: Buffer
decrypt(receiverPrvhex: string, msg: Buffer): Buffer
Parameters:
- receiverPrvhex - Receiver's secp256k1 private key hex string
- msg - Data to decrypt
Returns: Buffer
PrivateKey
static fromHex(hex: string): PrivateKey;
constructor(secret?: Buffer);
toHex(): string;
ecdh(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;
equals(other: PublicKey): boolean;
readonly uncompressed: Buffer;
readonly compressed: Buffer;