UNPKG

@nlindley/eccrypto

Version:

JavaScript Elliptic curve cryptography library

141 lines (140 loc) 5.41 kB
/** * Input/output structure for ECIES operations. */ export type Ecies = { /** * - Initialization vector (16 bytes) */ iv: Buffer; /** * - Ephemeral public key (65 bytes) */ ephemPublicKey: Buffer; /** * - The result of encryption (variable size) */ ciphertext: Buffer; /** * - Message authentication code (32 bytes) */ mac: Buffer; }; /** * Generate a new valid private key. Will use crypto.randomBytes as source. * @return {Buffer} A 32-byte private key. * @function */ export function generatePrivate(): Buffer; /** * Compute the public key for a given private key. * @param {Buffer} privateKey - A 32-byte private key * @return {Buffer} A 65-byte public key. * @function */ export function getPublic(privateKey: Buffer): Buffer; /** * Get compressed version of public key. */ export function getPublicCompressed(privateKey: any): Buffer; /** * Create an ECDSA signature. * @param {Buffer} privateKey - A 32-byte private key * @param {Buffer} msg - The message being signed * @return {Buffer} A promise that resolves with the * signature and rejects on bad key or message. */ export function signSync(privateKey: Buffer, msg: Buffer): Buffer; /** * Create an ECDSA signature. * @param {Buffer} privateKey - A 32-byte private key * @param {Buffer} msg - The message being signed * @return {Promise.<Buffer>} A promise that resolves with the * signature and rejects on bad key or message. */ export function sign(privateKey: Buffer, msg: Buffer): Promise<Buffer>; /** * Verify an ECDSA signature. * @param {Buffer} publicKey - A 65-byte public key * @param {Buffer} msg - The message being verified * @param {Buffer} sig - The signature * @return {null} A promise that resolves on correct signature * and rejects on bad key or signature. */ export function verifySync(publicKey: Buffer, msg: Buffer, sig: Buffer): null; /** * Verify an ECDSA signature. * @param {Buffer} publicKey - A 65-byte public key * @param {Buffer} msg - The message being verified * @param {Buffer} sig - The signature * @return {Promise.<null>} A promise that resolves on correct signature * and rejects on bad key or signature. */ export function verify(publicKey: Buffer, msg: Buffer, sig: Buffer): Promise<null>; /** * Derive shared secret for given private and public keys. * @param {Buffer} privateKeyA - Sender's private key (32 bytes) * @param {Buffer} publicKeyB - Recipient's public key (65 bytes) * @return {Buffer} The derived shared secret (Px, 32 bytes). */ export function deriveSync(privateKeyA: Buffer, publicKeyB: Buffer): Buffer; /** * Derive shared secret for given private and public keys. * @param {Buffer} privateKeyA - Sender's private key (32 bytes) * @param {Buffer} publicKeyB - Recipient's public key (65 bytes) * @return {Promise.<Buffer>} The derived shared secret (Px, 32 bytes). */ export function derive(privateKeyA: Buffer, publicKeyB: Buffer): Promise<Buffer>; /** * Input/output structure for ECIES operations. * @typedef {Object} Ecies * @property {Buffer} iv - Initialization vector (16 bytes) * @property {Buffer} ephemPublicKey - Ephemeral public key (65 bytes) * @property {Buffer} ciphertext - The result of encryption (variable size) * @property {Buffer} mac - Message authentication code (32 bytes) */ /** * Encrypt message for given recepient's public key. * @param {Buffer} publicKeyTo - Recipient's public key (65 bytes) * @param {Buffer} msg - The message being encrypted * @param {{iv?: Buffer, ephemPrivateKey?: Buffer}} [opts] - You may also * specify initialization vector (16 bytes) and ephemeral private key * (32 bytes) to get deterministic results. * @return {Ecies} - A promise that resolves with the ECIES * structure on successful encryption and rejects on failure. */ export function encryptSync(publicKeyTo: Buffer, msg: Buffer, opts?: { iv?: Buffer; ephemPrivateKey?: Buffer; }): Ecies; /** * Encrypt message for given recepient's public key. * @param {Buffer} publicKeyTo - Recipient's public key (65 bytes) * @param {Buffer} msg - The message being encrypted * @param {{iv?: Buffer, ephemPrivateKey?: Buffer}} [opts] - You may also * specify initialization vector (16 bytes) and ephemeral private key * (32 bytes) to get deterministic results. * @return {Promise.<Ecies>} - A promise that resolves with the ECIES * structure on successful encryption and rejects on failure. */ export function encrypt(publicKeyTo: Buffer, msg: Buffer, opts?: { iv?: Buffer; ephemPrivateKey?: Buffer; }): Promise<Ecies>; /** * Decrypt message using given private key. * @param {Buffer} privateKey - A 32-byte private key of recepient of * the mesage * @param {Ecies} opts - ECIES structure (result of ECIES encryption) * @return {Buffer} - A promise that resolves with the * plaintext on successful decryption and rejects on failure. */ export function decryptSync(privateKey: Buffer, opts: Ecies): Buffer; /** * Decrypt message using given private key. * @param {Buffer} privateKey - A 32-byte private key of recepient of * the mesage * @param {Ecies} opts - ECIES structure (result of ECIES encryption) * @return {Promise.<Buffer>} - A promise that resolves with the * plaintext on successful decryption and rejects on failure. */ export function decrypt(privateKey: Buffer, opts: Ecies): Promise<Buffer>;