eccrypto-js
Elliptic curve cryptography library (NodeJS, Browser and Pure JS)
Description
This library is a port from eccrypto it makes use of native libraries on NodeJS and Browser enviroments with pure javascript fallbacks.
Usage
RandomBytes
import * as eccryptoJS from 'eccrypto-js';
const length = 32;
const key = eccryptoJS.randomBytes(length);
AES
import * as eccryptoJS from 'eccrypto-js';
const key = eccryptoJS.randomBytes(32);
const iv = eccryptoJS.randomBytes(16);
const str = 'test message to encrypt';
const msg = eccryptoJS.utf8ToBuffer(str);
const ciphertext = await eccryptoJS.aesCbcEncrypt(iv, key, msg);
const decrypted = await eccryptoJS.aesCbcDecrypt(iv, key, ciphertext);
HMAC
import * as eccryptoJS from 'eccrypto-js';
const key = eccryptoJS.randomBytes(32);
const iv = eccryptoJS.randomBytes(16);
const macKey = eccryptoJS.concatBuffers(iv, key);
const dataToMac = eccryptoJS.concatBuffers(iv, key, msg);
const mac = await eccryptoJS.hmacSha256Sign(macKey, dataToMac);
const result = await eccryptoJS.hmacSha256Verify(macKey, dataToMac, mac);
SHA2
import * as eccryptoJS from 'eccrypto-js';
const str = 'test message to hash';
const msg = eccryptoJS.utf8ToBuffer(str);
const hash = await eccryptoJS.sha256(str);
const str = 'test message to hash';
const msg = eccryptoJS.utf8ToBuffer(str);
const hash = await eccryptoJS.sha512(str);
SHA3
import * as eccryptoJS from 'eccrypto-js';
const str = 'test message to hash';
const msg = eccryptoJS.utf8ToBuffer(str);
const hash = await eccryptoJS.sha3(str);
const str = 'test message to hash';
const msg = eccryptoJS.utf8ToBuffer(str);
const hash = await eccryptoJS.keccak256(str);
ECDSA
import * as eccryptoJS from 'eccrypto-js';
const keyPair = eccryptoJS.generateKeyPair();
const str = 'test message to hash';
const msg = eccryptoJS.utf8ToBuffer(str);
const hash = await eccryptoJS.sha256(str);
const sig = await eccryptoJS.sign(keyPair.privateKey, hash);
await eccryptoJS.verify(keyPair.publicKey, msg, sig);
ECDH
import * as eccryptoJS from 'eccrypto-js';
const keyPairA = eccryptoJS.generateKeyPair();
const keyPairB = eccryptoJS.generateKeyPair();
const sharedKey1 = await eccryptoJS.derive(
keyPairA.privateKey,
keyPairB.publicKey
);
const sharedKey2 = await eccryptoJS.derive(
keyPairB.privateKey,
keyPairA.publicKey
);
ECIES
import * as eccryptoJS from 'eccrypto-js';
const keyPair = eccryptoJS.generateKeyPair();
const str = 'test message to encrypt';
const msg = eccryptoJS.utf8ToBuffer(str);
const encrypted = await eccryptoJS.encrypt(keyPairB.publicKey, msg);
const decrypted = await eccryptoJS.decrypt(keyPairB.privateKey, encrypted);
License
MIT License