Code Monkey home page Code Monkey logo

eccrypto-js's Introduction

eccrypto-js npm version

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);

// key.length === 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);

// decrypted.toString() === str

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);

// result will return true if match

SHA2

import * as eccryptoJS from 'eccrypto-js';

// SHA256
const str = 'test message to hash';
const msg = eccryptoJS.utf8ToBuffer(str);
const hash = await eccryptoJS.sha256(str);

// SHA512
const str = 'test message to hash';
const msg = eccryptoJS.utf8ToBuffer(str);
const hash = await eccryptoJS.sha512(str);

SHA3

import * as eccryptoJS from 'eccrypto-js';

// SHA3
const str = 'test message to hash';
const msg = eccryptoJS.utf8ToBuffer(str);
const hash = await eccryptoJS.sha3(str);

// KECCAK256
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);

// verify will throw if signature is BAD

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
);

// sharedKey1.toString('hex') === sharedKey2.toString('hex')

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);

// decrypted.toString() === str

PBKDF2

import * as eccryptoJS from 'eccrypto-js';

const password = 'password';
const buffer = eccryptoJS.utf8ToBuffer(str);

const key = await eccryptoJS.pbkdf2(buffer);

// key.length === 32

React-Native Support

This library is intended for use in a Browser or NodeJS environment, however it is possible to use in a React-Native environment if NodeJS modules are polyfilled with react-native-crypto, read more here.

License

MIT License

eccrypto-js's People

Contributors

pedrouid avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

eccrypto-js's Issues

Buffer is not defined in browser

Hi, I use this lib in node with no problem, but in browser, it give me an error of

index.ts:49 Uncaught ReferenceError: Buffer is not defined
at node_modules/eccrypto-js/dist/cjs/constants/index.js (index.ts:49)

it seems to be the constants/index.js has a few const to Buffer, but need some help to figure it out.

Thanks in advance

Unable to use private key in the browser

The example from the README works in NodeJS but not in the browser. Try running the code below in the browser yourself:

let eccryptoJS = window.eccryptoJS;
let publicKey = hexToString('...');
let privateKey = hexToString('...');
keyPair = { publicKey, privateKey};

const str = 'test message to encrypt';
const msg = eccryptoJS.utf8ToBuffer(str);

const encrypted = await eccryptoJS.encrypt(keyPair.publicKey, msg);
console.log('encrypted:', encrypted);
const decrypted = await eccryptoJS.decrypt(keyPair.privateKey, encrypted);
console.log('decrypted:', decrypted);
function hexToString(hexString) {
	return new Uint8Array(hexString.match(/.{1,2}/g).map(byte => parseInt(byte, 16)));
}

Bonty: bitchan/eccrypto#72

[Question]: ed25519 vs secp256k1

Hallo!
First of all, awesome port! Thank you! :D

For my current system I'm using ed25519 keys everywhere.

  • would it easily be possible to add ed25519 into this project?
  • what is your take on secp256k1 compared to ed25519

As far as I know there is a vulnerability on ed25519 keys if the adversary has control over the hardware while the key is generated.

As for secp256k1:
https://crypto.stackexchange.com/questions/68269/does-secp256k1-have-any-known-weaknesses

So far it seems for diffie-hellman is not a "good idea" with secp256k1.
Which would be kind of my primary usecase.^^

Cheers!

ecies.ts decryptSync method is not sync

Hey @pedrouid, thanks for the library.

Wanted to report that the decrypt method found in src/ecies.ts is not sync.

export async function decrypt(
  privateKey: Buffer,
  opts: Encrypted
): Promise<Buffer> {
  const { ephemPublicKey, iv, mac, ciphertext } = opts;
  const { encryptionKey, macKey } = await getEciesKeys(
    privateKey,
    ephemPublicKey
  );
  const dataToMac = concatBuffers(iv, ephemPublicKey, ciphertext);
  const macTest = await hmacSha256Verify(macKey, dataToMac, mac);
  assert(macTest, ERROR_BAD_MAC);
  const msg = await aesCbcDecrypt(opts.iv, encryptionKey, opts.ciphertext);
  return msg;
}

Different results in react-native and Node environment

ecies.ts decrypt method is returning different results on React Native and Node environments. I am getting the valid result in Node but in React Native env, it returns ERROR_BAD_MAC. Also, on React Native, the method works most of the time and only fails in some cases.

Help on this would be appreciated.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.