eccrypto-sync

JavaScript Elliptic curve cryptography library

eccrypto

JavaScript Elliptic curve cryptography library for both browserify and node.

Motivation

There is currently no any isomorphic ECC library which provides ECDSA, ECDH and ECIES for both Node.js and Browser and uses the fastest implementation available (e.g. secp256k1-nodeis much faster than other libraries but can be used only on Node.js). So eccryptois an attempt to create one.

Implementation details

With the help of browserify eccryptoprovides different implementations for Browser and Node.js with the same API. Because WebCryptoAPI defines asynchronous promise-driven API, implementation for Node needs to use promises too.

  • Use Node.js crypto module/library bindings where possible
  • Use WebCryptoAPI where possible
  • Promise-driven API
  • Only secp256k1 curve, only SHA-512 (KDF), HMAC-SHA-256 (HMAC) and AES-256-CBC for ECIES

Native crypto API limitations

crypto

ECDH only works in Node 0.11+ (see https://github.com/joyent/node/pull/5854), ECDSA only supports keys in PEM format (see https://github.com/joyent/node/issues/6904) and ECIES is not supported at all.

WebCryptoAPI

ECDSA and ECDH are supported in Chrome only on Windows(see also bug 338883), aren't supported by Firefox (fixed only in 36.0+, see bug 1034854; see also feature matrix) and ECIES is not defined at all in WebCryptoAPI draft. Also WebCryptoAPI currently definesonly curves recommended by NIST meaning that secp256k1 (K-256) curve is not supported (see also: [1], [2]).

So we use seck256k1library in Node for ECDSA, ellipticin Browser for ECDSA and ECDH and implement ECIES manually with the help of native crypto API.

Possible future goals

  • Support other curves/KDF/MAC/symmetric encryption schemes

Usage

ECDSA

var crypto = require("crypto");
var eccrypto = require("eccrypto");

// A new random 32-byte private key.
var privateKey = crypto.randomBytes(32);
// Corresponding uncompressed (65-byte) public key.
var publicKey = eccrypto.getPublic(privateKey);

var str = "message to sign";
// Always hash you message to sign!
var msg = crypto.createHash("sha256").update(str).digest();

eccrypto.sign(privateKey, msg).then(function(sig) {
  console.log("Signature in DER format:", sig);
  eccrypto.verify(publicKey, msg, sig).then(function() {
    console.log("Signature is OK");
  }).catch(function() {
    console.log("Signature is BAD");
  });
});

ECDH

var crypto = require("crypto");
var eccrypto = require("eccrypto");

var privateKeyA = crypto.randomBytes(32);
var publicKeyA = eccrypto.getPublic(privateKeyA);
var privateKeyB = crypto.randomBytes(32);
var publicKeyB = eccrypto.getPublic(privateKeyB);

eccrypto.derive(privateKeyA, publicKeyB).then(function(sharedKey1) {
  eccrypto.derive(privateKeyB, publicKeyA).then(function(sharedKey2) {
    console.log("Both shared keys are equal:", sharedKey1, sharedKey2);
  });
});

ECIES

var crypto = require("crypto");
var eccrypto = require("eccrypto");

var privateKeyA = crypto.randomBytes(32);
var publicKeyA = eccrypto.getPublic(privateKeyA);
var privateKeyB = crypto.randomBytes(32);
var publicKeyB = eccrypto.getPublic(privateKeyB);

// Encrypting the message for B.
eccrypto.encrypt(publicKeyB, Buffer("msg to b")).then(function(encrypted) {
  // B decrypting the message.
  eccrypto.decrypt(privateKeyB, encrypted).then(function(plaintext) {
    console.log("Message to part B:", plaintext.toString());
  });
});

// Encrypting the message for A.
eccrypto.encrypt(publicKeyA, Buffer("msg to a")).then(function(encrypted) {
  // A decrypting the message.
  eccrypto.decrypt(privateKeyA, encrypted).then(function(plaintext) {
    console.log("Message to part A:", plaintext.toString());
  });
});

License

eccrypto - JavaScript Elliptic curve cryptography library

Written in 2014-2015 by Kagami Hiiragi kagami@genshiken.org

To the extent possible under law, the author(s) have dedicated all copyright and related and neighboring rights to this software to the public domain worldwide. This software is distributed without any warranty.

You should have received a copy of the CC0 Public Domain Dedication along with this software. If not, see http://creativecommons.org/publicdomain/zero/1.0/.

HomePage

https://github.com/bitchan/eccrypto

Repository

https://github.com/bitchan/eccrypto.git


上一篇:elliptic-es
下一篇:three-model

相关推荐

  • 🙋Hanjst汉吉斯特升级:+showImageAsync及性能改进等

    自2019年元旦🙋Hanjst汉吉斯特 模板语言及其编译引擎发布,已经过去一年多了。 这期间随着 🙋Hanjst汉吉斯特 的推广应用,我们也陆续发布了如下一些更新内容: 🛠️Hanjst/汉吉...

    4 个月前
  • 🔥 Promise|async|Generator 实现&原理大解析 | 9k字

    笔者刚接触async/await时,就被其暂停执行的特性吸引了,心想在没有原生API支持的情况下,await居然能挂起当前方法,实现暂停执行,我感到十分好奇。好奇心驱使我一层一层剥开有关JS异步编程的...

    5 个月前
  • 面试官: 说说你对async的理解

    大家好,我是小雨小雨,致力于分享有趣的、实用的技术文章。 内容分为翻译和原创,如果有问题,欢迎随时评论或私信,希望和大家一起进步。 分享不易,希望能够得到大家的支持和关注。

    4 个月前
  • 重新理解async和await

    async 和 await 在干什么 任意一个名称都是有意义的,先从字面意思来理解。async 是“异步”的简写,而 await 可以认为是 async wait 的简写。

    1 年前
  • 重学JS:async/await

    前言 异步操作一直是JS中不可或缺的一环,从最开始回调函数,到后面的Promise,再到ES2017引入的async函数,异步操作逐渐进化,变得越来越简单方便,接下来就仔细看看在ES2017引入了...

    1 年前
  • 走近 Vue 的 .sync 修饰符

    写在前面 .sync 修饰符算是 Vue 的所有修饰符中较难理解的一个,本篇文章就带你走近 .sync 的世界,深入理解后会发现,其实也就那么回事。修饰符和指令息息相关,下面从 指令 > 修饰符...

    5 个月前
  • 谈谈 <script> 标签以及其加载顺序问题,包含 defer & async

    谈谈 JavaScritJavaScriptJavaScriptJavaScriptHTML5JavaScripthtmlJavaScriptJavaScriphtmlsrcJavaScriptJa...

    2 年前
  • 详谈Ajax请求中的async:false/true的作用(ajax 在外部调用问题)

    test.html tet.php 说明 async: false,(默认是true); 如上:false为同步,这个 testAsync()方法中的Ajax请...

    3 年前
  • 详解Node.js中的Async和Await函数

    在本文中,你将学习如何使用Node.js中的async函数(async/await)来简化callback或Promise. 异步语言结构在其他语言中已经存在了,像c的async/await、Kot...

    2 年前
  • 记一次小程序开发中如何使用async-await并封装公共异步请求

    前言 在平常的项目开发中肯定会遇到同步异步执行的问题,还有的就是当执行某一个操作依赖上一个执行所返回的结果,那么这个时候你会如何解决这个问题呢; 实现方案 regenerator(h...

    2 年前

官方社区

扫码加入 JavaScript 社区