Elliptic Curve Integrated Encryption Scheme for secp256k1, written in TypeScript with minimal dependencies.
This is the JavaScript/TypeScript version of eciespy with a built-in class-like secp256k1 API, you may go there for detailed documentation of the mechanism under the hood.
Install with npm install eciesjs
(secp256k1
is the only dependency).
> 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
Parameters:
- receiverPubhex - Receiver's secp256k1 public key hex string
- msg - Data to encrypt
Returns: Buffer
Parameters:
- receiverPrvhex - Receiver's secp256k1 private key hex string
- msg - Data to decrypt
Returns: Buffer
- Methods
static fromHex(hex: string): PrivateKey;
constructor(secret?: Buffer);
toHex(): string;
encapsulateKEM(pub: PublicKey): Buffer;
ecdh(pub: PublicKey): Buffer;
equals(other: PrivateKey): boolean;
- Properties
readonly secret: Buffer;
readonly publicKey: PublicKey;
- Methods
static fromHex(hex: string): PublicKey;
constructor(buffer: Buffer);
toHex(compressed?: boolean): string;
decapsulateKEM(priv: PrivateKey): Buffer;
equals(other: PublicKey): boolean;
- Properties
readonly uncompressed: Buffer;
readonly compressed: Buffer;