eccrypto-js
Advanced tools
Comparing version 0.2.5 to 0.2.6
@@ -10,20 +10,24 @@ 'use strict'; | ||
var random = require('@ethersproject/random'); | ||
var bytes = require('@ethersproject/bytes'); | ||
var strings = require('@ethersproject/strings'); | ||
var bytes = require('@ethersproject/bytes'); | ||
var sha2 = require('@ethersproject/sha2'); | ||
function aesCbcEncrypt(iv, key, data) { | ||
return new Promise(function (resolve) { | ||
var aesCbcDecrypt = function aesCbcDecrypt(iv, key, data) { | ||
try { | ||
var aesCbc = new aesJs.ModeOfOperation.cbc(key, iv); | ||
var encryptedBytes = aesCbc.decrypt(data); | ||
return Promise.resolve(Buffer.from(encryptedBytes)); | ||
} catch (e) { | ||
return Promise.reject(e); | ||
} | ||
}; | ||
var aesCbcEncrypt = function aesCbcEncrypt(iv, key, data) { | ||
try { | ||
var aesCbc = new aesJs.ModeOfOperation.cbc(key, iv); | ||
var encryptedBytes = aesCbc.encrypt(data); | ||
resolve(Buffer.from(encryptedBytes)); | ||
}); | ||
} | ||
function aesCbcDecrypt(iv, key, data) { | ||
return new Promise(function (resolve) { | ||
var aesCbc = new aesJs.ModeOfOperation.cbc(key, iv); | ||
var encryptedBytes = aesCbc.decrypt(data); | ||
resolve(Buffer.from(encryptedBytes)); | ||
}); | ||
} | ||
return Promise.resolve(Buffer.from(encryptedBytes)); | ||
} catch (e) { | ||
return Promise.reject(e); | ||
} | ||
}; | ||
@@ -73,2 +77,39 @@ var EC_GROUP_ORDER = | ||
var verify = function verify(publicKey, msg, sig) { | ||
try { | ||
assert(publicKey.length === 65 || publicKey.length === 33, 'Bad public key'); | ||
if (publicKey.length === 65) { | ||
assert(publicKey[0] === 4, 'Bad public key'); | ||
} | ||
if (publicKey.length === 33) { | ||
assert(publicKey[0] === 2 || publicKey[0] === 3, 'Bad public key'); | ||
} | ||
assert(msg.length > 0, 'Message should not be empty'); | ||
assert(msg.length <= 32, 'Message is too long'); | ||
if (secp256k1curve.verify(msg, sig, publicKey)) { | ||
return Promise.resolve(null); | ||
} else { | ||
throw new Error('Bad signature'); | ||
} | ||
} catch (e) { | ||
return Promise.reject(e); | ||
} | ||
}; | ||
var sign = function sign(privateKey, msg) { | ||
try { | ||
assert(privateKey.length === 32, 'Bad private key'); | ||
assert(isValidPrivateKey(privateKey), 'Bad private key'); | ||
assert(msg.length > 0, 'Message should not be empty'); | ||
assert(msg.length <= 32, 'Message is too long'); | ||
return Promise.resolve(Buffer.from(secp256k1curve.sign(msg, privateKey, { | ||
canonical: true | ||
}).toDER())); | ||
} catch (e) { | ||
return Promise.reject(e); | ||
} | ||
}; | ||
var secp256k1curve = | ||
@@ -125,176 +166,152 @@ /*#__PURE__*/ | ||
} | ||
function sign(privateKey, msg) { | ||
return new Promise(function (resolve) { | ||
try { | ||
assert(privateKey.length === 32, 'Bad private key'); | ||
assert(isValidPrivateKey(privateKey), 'Bad private key'); | ||
assert(msg.length > 0, 'Message should not be empty'); | ||
assert(msg.length <= 32, 'Message is too long'); | ||
resolve(Buffer.from(secp256k1curve.sign(msg, privateKey, { | ||
canonical: true | ||
}).toDER())); | ||
return Promise.resolve(); | ||
} catch (e) { | ||
return Promise.reject(e); | ||
} | ||
}); | ||
} | ||
function verify(publicKey, msg, sig) { | ||
return new Promise(function (resolve, reject) { | ||
assert(publicKey.length === 65 || publicKey.length === 33, 'Bad public key'); | ||
if (publicKey.length === 65) { | ||
assert(publicKey[0] === 4, 'Bad public key'); | ||
} | ||
var derive = function derive(privateKeyA, publicKeyB) { | ||
try { | ||
assert(Buffer.isBuffer(privateKeyA), 'Bad private key'); | ||
assert(Buffer.isBuffer(publicKeyB), 'Bad public key'); | ||
assert(privateKeyA.length === 32, 'Bad private key'); | ||
assert(isValidPrivateKey(privateKeyA), 'Bad private key'); | ||
assert(publicKeyB.length === 65 || publicKeyB.length === 33, 'Bad public key'); | ||
if (publicKey.length === 33) { | ||
assert(publicKey[0] === 2 || publicKey[0] === 3, 'Bad public key'); | ||
if (publicKeyB.length === 65) { | ||
assert(publicKeyB[0] === 4, 'Bad public key'); | ||
} | ||
assert(msg.length > 0, 'Message should not be empty'); | ||
assert(msg.length <= 32, 'Message is too long'); | ||
if (secp256k1curve.verify(msg, sig, publicKey)) { | ||
resolve(null); | ||
} else { | ||
reject(new Error('Bad signature')); | ||
if (publicKeyB.length === 33) { | ||
assert(publicKeyB[0] === 2 || publicKeyB[0] === 3, 'Bad public key'); | ||
} | ||
}); | ||
} | ||
function derive(privateKeyA, publicKeyB) { | ||
return new Promise(function (resolve) { | ||
try { | ||
assert(Buffer.isBuffer(privateKeyA), 'Bad private key'); | ||
assert(Buffer.isBuffer(publicKeyB), 'Bad public key'); | ||
assert(privateKeyA.length === 32, 'Bad private key'); | ||
assert(isValidPrivateKey(privateKeyA), 'Bad private key'); | ||
assert(publicKeyB.length === 65 || publicKeyB.length === 33, 'Bad public key'); | ||
var keyA = getCurve().keyFromPrivate(privateKeyA); | ||
var keyB = getCurve().keyFromPublic(publicKeyB); | ||
var sharedKey = keyA.derive(keyB.getPublic()); // BN instance | ||
if (publicKeyB.length === 65) { | ||
assert(publicKeyB[0] === 4, 'Bad public key'); | ||
} | ||
return Promise.resolve(Buffer.from(sharedKey.toArray())); | ||
} catch (e) { | ||
return Promise.reject(e); | ||
} | ||
}; | ||
if (publicKeyB.length === 33) { | ||
assert(publicKeyB[0] === 2 || publicKeyB[0] === 3, 'Bad public key'); | ||
} | ||
function removeTrailing0x(str) { | ||
if (str.startsWith('0x')) { | ||
return str.substring(2); | ||
} else { | ||
return str; | ||
} | ||
} | ||
function addTrailing0x(str) { | ||
if (!str.startsWith('0x')) { | ||
return '0x' + str; | ||
} else { | ||
return str; | ||
} | ||
} | ||
var keyA = getCurve().keyFromPrivate(privateKeyA); | ||
var keyB = getCurve().keyFromPublic(publicKeyB); | ||
var Px = keyA.derive(keyB.getPublic()); // BN instance | ||
var hmacSha256Verify = function hmacSha256Verify(key, msg, sig) { | ||
try { | ||
return Promise.resolve(hmacSha256Sign(key, msg)).then(function (expectedSig) { | ||
return equalConstTime(expectedSig, sig); | ||
}); | ||
} catch (e) { | ||
return Promise.reject(e); | ||
} | ||
}; | ||
var hmacSha256Sign = function hmacSha256Sign(key, msg) { | ||
try { | ||
var result = sha2.computeHmac(sha2.SupportedAlgorithm.sha256, key, msg); | ||
return Promise.resolve(Buffer.from(result)); | ||
} catch (e) { | ||
return Promise.reject(e); | ||
} | ||
}; | ||
var hashSharedKey = function hashSharedKey(sharedKey) { | ||
try { | ||
var hash = sha2.sha512(sharedKey); | ||
resolve(Buffer.from(Px.toArray())); | ||
return Promise.resolve(); | ||
} catch (e) { | ||
return Promise.reject(e); | ||
} | ||
}); | ||
} | ||
return Promise.resolve(bytes.arrayify(hash)); | ||
} catch (e) { | ||
return Promise.reject(e); | ||
} | ||
}; | ||
var sha512 = function sha512(msg) { | ||
try { | ||
var bytes = strings.toUtf8Bytes(msg); | ||
function sha256(msg) { | ||
return new Promise(function (resolve) { | ||
try { | ||
var bytes$1 = strings.toUtf8Bytes(msg); | ||
var hash = sha2.sha512(bytes); | ||
var hash = sha2.sha256(bytes$1); | ||
return Promise.resolve(removeTrailing0x(hash)); | ||
} catch (e) { | ||
return Promise.reject(e); | ||
} | ||
}; | ||
var sha256 = function sha256(msg) { | ||
try { | ||
var bytes = strings.toUtf8Bytes(msg); | ||
resolve(bytes.arrayify(hash)); | ||
return Promise.resolve(); | ||
} catch (e) { | ||
return Promise.reject(e); | ||
} | ||
}); | ||
} | ||
function sha512(msg) { | ||
return new Promise(function (resolve) { | ||
try { | ||
var bytes$1 = strings.toUtf8Bytes(msg); | ||
var hash = sha2.sha256(bytes); | ||
var hash = sha2.sha512(bytes$1); | ||
return Promise.resolve(removeTrailing0x(hash)); | ||
} catch (e) { | ||
return Promise.reject(e); | ||
} | ||
}; | ||
resolve(bytes.arrayify(hash)); | ||
return Promise.resolve(); | ||
} catch (e) { | ||
return Promise.reject(e); | ||
} | ||
}); | ||
} | ||
function hmacSha256Sign(key, msg) { | ||
return new Promise(function (resolve) { | ||
try { | ||
var result = sha2.computeHmac(sha2.SupportedAlgorithm.sha256, key, msg); | ||
resolve(Buffer.from(result)); | ||
return Promise.resolve(); | ||
} catch (e) { | ||
return Promise.reject(e); | ||
} | ||
}); | ||
} | ||
function hmacSha256Verify(key, msg, sig) { | ||
return new Promise(function (resolve) { | ||
try { | ||
return Promise.resolve(hmacSha256Sign(key, msg)).then(function (expectedSig) { | ||
resolve(equalConstTime(expectedSig, sig)); | ||
var decrypt = function decrypt(privateKey, opts) { | ||
try { | ||
// Tmp variable to save context from flat promises; | ||
var encryptionKey; | ||
return Promise.resolve(derive(privateKey, opts.ephemPublicKey)).then(function (sharedKey) { | ||
return Promise.resolve(hashSharedKey(sharedKey)).then(function (hash) { | ||
encryptionKey = Buffer.from(hash.slice(0, 32)); | ||
var macKey = hash.slice(32); | ||
var dataToMac = Buffer.concat([opts.iv, opts.ephemPublicKey, opts.ciphertext]); | ||
return Promise.resolve(hmacSha256Verify(Buffer.from(macKey), dataToMac, opts.mac)).then(function (macGood) { | ||
assert(macGood, 'Bad MAC'); | ||
return Promise.resolve(aesCbcDecrypt(opts.iv, encryptionKey, opts.ciphertext)).then(function (msg) { | ||
return Buffer.from(new Uint8Array(msg)); | ||
}); | ||
}); | ||
}); | ||
} catch (e) { | ||
return Promise.reject(e); | ||
} | ||
}); | ||
} | ||
}); | ||
} catch (e) { | ||
return Promise.reject(e); | ||
} | ||
}; | ||
var encrypt = function encrypt(publicKeyTo, msg, opts) { | ||
try { | ||
opts = opts || {}; // Tmp variables to save context from flat promises; | ||
function encrypt(publicKeyTo, msg, opts) { | ||
opts = opts || {}; // Tmp variables to save context from flat promises; | ||
var iv; | ||
var ephemPublicKey; | ||
var ciphertext; | ||
var macKey; | ||
var ephemPrivateKey = randomBytes(32); // There is a very unlikely possibility that it is not a valid key | ||
var iv; | ||
var ephemPublicKey; | ||
var ciphertext; | ||
var macKey; | ||
return new Promise(function (resolve) { | ||
try { | ||
var ephemPrivateKey = randomBytes(32); // There is a very unlikely possibility that it is not a valid key | ||
ephemPublicKey = getPublic(ephemPrivateKey); | ||
return Promise.resolve(derive(ephemPrivateKey, publicKeyTo)).then(function (sharedKey) { | ||
return Promise.resolve(hashSharedKey(sharedKey)).then(function (hash) { | ||
var _opts; | ||
ephemPublicKey = getPublic(ephemPrivateKey); | ||
resolve(derive(ephemPrivateKey, publicKeyTo)); | ||
return Promise.resolve(); | ||
} catch (e) { | ||
return Promise.reject(e); | ||
} | ||
}).then(function (Px) { | ||
return sha512(Px); | ||
}).then(function (hash) { | ||
iv = opts.iv || randomBytes(16); | ||
var encryptionKey = hash.slice(0, 32); | ||
macKey = Buffer.from(hash.slice(32)); | ||
return aesCbcEncrypt(iv, Buffer.from(encryptionKey), msg); | ||
}).then(function (data) { | ||
ciphertext = data; | ||
var dataToMac = Buffer.concat([iv, ephemPublicKey, ciphertext]); | ||
return hmacSha256Sign(macKey, dataToMac); | ||
}).then(function (mac) { | ||
return { | ||
iv: iv, | ||
ephemPublicKey: ephemPublicKey, | ||
ciphertext: ciphertext, | ||
mac: mac | ||
}; | ||
}); | ||
} | ||
function decrypt(privateKey, opts) { | ||
// Tmp variable to save context from flat promises; | ||
var encryptionKey; | ||
return derive(privateKey, opts.ephemPublicKey).then(function (Px) { | ||
return sha512(Px); | ||
}).then(function (hash) { | ||
encryptionKey = Buffer.from(hash.slice(0, 32)); | ||
var macKey = hash.slice(32); | ||
var dataToMac = Buffer.concat([opts.iv, opts.ephemPublicKey, opts.ciphertext]); | ||
return hmacSha256Verify(Buffer.from(macKey), dataToMac, opts.mac); | ||
}).then(function (macGood) { | ||
assert(macGood, 'Bad MAC'); | ||
return aesCbcDecrypt(opts.iv, encryptionKey, opts.ciphertext); | ||
}).then(function (msg) { | ||
return Buffer.from(new Uint8Array(msg)); | ||
}); | ||
} | ||
iv = ((_opts = opts) === null || _opts === void 0 ? void 0 : _opts.iv) || randomBytes(16); | ||
var encryptionKey = hash.slice(0, 32); | ||
macKey = Buffer.from(hash.slice(32)); | ||
return Promise.resolve(aesCbcEncrypt(iv, Buffer.from(encryptionKey), msg)).then(function (data) { | ||
ciphertext = data; | ||
var dataToMac = Buffer.concat([iv, ephemPublicKey, ciphertext]); | ||
return Promise.resolve(hmacSha256Sign(macKey, dataToMac)).then(function (mac) { | ||
return { | ||
iv: iv, | ||
ephemPublicKey: ephemPublicKey, | ||
ciphertext: ciphertext, | ||
mac: mac | ||
}; | ||
}); | ||
}); | ||
}); | ||
}); | ||
} catch (e) { | ||
return Promise.reject(e); | ||
} | ||
}; | ||
exports.addTrailing0x = addTrailing0x; | ||
exports.aesCbcDecrypt = aesCbcDecrypt; | ||
@@ -313,2 +330,3 @@ exports.aesCbcEncrypt = aesCbcEncrypt; | ||
exports.getPublicCompressed = getPublicCompressed; | ||
exports.hashSharedKey = hashSharedKey; | ||
exports.hmacSha256Sign = hmacSha256Sign; | ||
@@ -319,2 +337,3 @@ exports.hmacSha256Verify = hmacSha256Verify; | ||
exports.randomBytes = randomBytes; | ||
exports.removeTrailing0x = removeTrailing0x; | ||
exports.sha256 = sha256; | ||
@@ -321,0 +340,0 @@ exports.sha512 = sha512; |
@@ -1,2 +0,2 @@ | ||
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e,r=(e=require("aes-js"))&&"object"==typeof e&&"default"in e?e.default:e,t=require("elliptic"),n=require("@ethersproject/random"),o=require("@ethersproject/strings"),f=require("@ethersproject/bytes"),i=require("@ethersproject/sha2");function u(e,t,n){return new Promise((function(o){var f=new r.ModeOfOperation.cbc(t,e).encrypt(n);o(Buffer.from(f))}))}function c(e,t,n){return new Promise((function(o){var f=new r.ModeOfOperation.cbc(t,e).decrypt(n);o(Buffer.from(f))}))}var a=Buffer.from("fffffffffffffffffffffffffffffffebaaedce6af48a03bbfd25e8cd0364141","hex"),s=Buffer.alloc(32,0);function p(e,r){if(!e)throw new Error(r||"Assertion failed")}function l(e){return Buffer.isBuffer(e)&&32===e.length}function h(e){return!!l(e)&&e.compare(s)>0&&e.compare(a)<0}function m(e,r){if(e.length!==r.length)return!1;for(var t=0,n=0;n<e.length;n++)t|=e[n]^r[n];return 0===t}function y(e){var r=n.randomBytes(e);return Buffer.from(r)}var v=new t.ec("secp256k1");function B(){return v}function P(){for(var e=y(32);!h(e);)e=y(32);return e}function g(e){p(32===e.length,"Bad private key"),p(h(e),"Bad private key")}function d(e){return g(e),Buffer.from(v.keyFromPrivate(e).getPublic("hex"),"hex")}function x(e,r){return new Promise((function(t){try{p(Buffer.isBuffer(e),"Bad private key"),p(Buffer.isBuffer(r),"Bad public key"),p(32===e.length,"Bad private key"),p(h(e),"Bad private key"),p(65===r.length||33===r.length,"Bad public key"),65===r.length&&p(4===r[0],"Bad public key"),33===r.length&&p(2===r[0]||3===r[0],"Bad public key");var n=B().keyFromPrivate(e),o=B().keyFromPublic(r),f=n.derive(o.getPublic());return t(Buffer.from(f.toArray())),Promise.resolve()}catch(e){return Promise.reject(e)}}))}function b(e){return new Promise((function(r){try{var t=o.toUtf8Bytes(e),n=i.sha512(t);return r(f.arrayify(n)),Promise.resolve()}catch(e){return Promise.reject(e)}}))}function k(e,r){return new Promise((function(t){try{var n=i.computeHmac(i.SupportedAlgorithm.sha256,e,r);return t(Buffer.from(n)),Promise.resolve()}catch(e){return Promise.reject(e)}}))}function w(e,r,t){return new Promise((function(n){try{return Promise.resolve(k(e,r)).then((function(e){n(m(e,t))}))}catch(e){return Promise.reject(e)}}))}exports.aesCbcDecrypt=c,exports.aesCbcEncrypt=u,exports.assert=p,exports.checkPrivateKey=g,exports.decrypt=function(e,r){var t;return x(e,r.ephemPublicKey).then((function(e){return b(e)})).then((function(e){t=Buffer.from(e.slice(0,32));var n=e.slice(32),o=Buffer.concat([r.iv,r.ephemPublicKey,r.ciphertext]);return w(Buffer.from(n),o,r.mac)})).then((function(e){return p(e,"Bad MAC"),c(r.iv,t,r.ciphertext)})).then((function(e){return Buffer.from(new Uint8Array(e))}))},exports.derive=x,exports.encrypt=function(e,r,t){var n,o,f,i;return t=t||{},new Promise((function(r){try{var t=y(32);return o=d(t),r(x(t,e)),Promise.resolve()}catch(e){return Promise.reject(e)}})).then((function(e){return b(e)})).then((function(e){n=t.iv||y(16);var o=e.slice(0,32);return i=Buffer.from(e.slice(32)),u(n,Buffer.from(o),r)})).then((function(e){f=e;var r=Buffer.concat([n,o,f]);return k(i,r)})).then((function(e){return{iv:n,ephemPublicKey:o,ciphertext:f,mac:e}}))},exports.equalConstTime=m,exports.generateKeyPair=function(){var e=P();return{privateKey:e,publicKey:d(e)}},exports.generatePrivate=P,exports.getCurve=B,exports.getPublic=d,exports.getPublicCompressed=function(e){return g(e),Buffer.from(v.keyFromPrivate(e).getPublic(!0,"hex"),"hex")},exports.hmacSha256Sign=k,exports.hmacSha256Verify=w,exports.isScalar=l,exports.isValidPrivateKey=h,exports.randomBytes=y,exports.sha256=function(e){return new Promise((function(r){try{var t=o.toUtf8Bytes(e),n=i.sha256(t);return r(f.arrayify(n)),Promise.resolve()}catch(e){return Promise.reject(e)}}))},exports.sha512=b,exports.sign=function(e,r){return new Promise((function(t){try{return p(32===e.length,"Bad private key"),p(h(e),"Bad private key"),p(r.length>0,"Message should not be empty"),p(r.length<=32,"Message is too long"),t(Buffer.from(v.sign(r,e,{canonical:!0}).toDER())),Promise.resolve()}catch(e){return Promise.reject(e)}}))},exports.verify=function(e,r,t){return new Promise((function(n,o){p(65===e.length||33===e.length,"Bad public key"),65===e.length&&p(4===e[0],"Bad public key"),33===e.length&&p(2===e[0]||3===e[0],"Bad public key"),p(r.length>0,"Message should not be empty"),p(r.length<=32,"Message is too long"),v.verify(r,t,e)?n(null):o(new Error("Bad signature"))}))}; | ||
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e,r=(e=require("aes-js"))&&"object"==typeof e&&"default"in e?e.default:e,t=require("elliptic"),n=require("@ethersproject/random"),o=require("@ethersproject/bytes"),i=require("@ethersproject/strings"),f=require("@ethersproject/sha2"),u=function(e,t,n){try{var o=new r.ModeOfOperation.cbc(t,e).decrypt(n);return Promise.resolve(Buffer.from(o))}catch(e){return Promise.reject(e)}},s=function(e,t,n){try{var o=new r.ModeOfOperation.cbc(t,e).encrypt(n);return Promise.resolve(Buffer.from(o))}catch(e){return Promise.reject(e)}},c=Buffer.from("fffffffffffffffffffffffffffffffebaaedce6af48a03bbfd25e8cd0364141","hex"),a=Buffer.alloc(32,0);function l(e,r){if(!e)throw new Error(r||"Assertion failed")}function h(e){return Buffer.isBuffer(e)&&32===e.length}function p(e){return!!h(e)&&e.compare(a)>0&&e.compare(c)<0}function m(e,r){if(e.length!==r.length)return!1;for(var t=0,n=0;n<e.length;n++)t|=e[n]^r[n];return 0===t}function v(e){var r=n.randomBytes(e);return Buffer.from(r)}var y=new t.ec("secp256k1");function P(){return y}function d(){for(var e=v(32);!p(e);)e=v(32);return e}function B(e){l(32===e.length,"Bad private key"),l(p(e),"Bad private key")}function g(e){return B(e),Buffer.from(y.keyFromPrivate(e).getPublic("hex"),"hex")}var x=function(e,r){try{l(Buffer.isBuffer(e),"Bad private key"),l(Buffer.isBuffer(r),"Bad public key"),l(32===e.length,"Bad private key"),l(p(e),"Bad private key"),l(65===r.length||33===r.length,"Bad public key"),65===r.length&&l(4===r[0],"Bad public key"),33===r.length&&l(2===r[0]||3===r[0],"Bad public key");var t=P().keyFromPrivate(e),n=P().keyFromPublic(r),o=t.derive(n.getPublic());return Promise.resolve(Buffer.from(o.toArray()))}catch(e){return Promise.reject(e)}};function b(e){return e.startsWith("0x")?e.substring(2):e}var k=function(e,r,t){try{return Promise.resolve(j(e,r)).then((function(e){return m(e,t)}))}catch(e){return Promise.reject(e)}},j=function(e,r){try{var t=f.computeHmac(f.SupportedAlgorithm.sha256,e,r);return Promise.resolve(Buffer.from(t))}catch(e){return Promise.reject(e)}},K=function(e){try{var r=f.sha512(e);return Promise.resolve(o.arrayify(r))}catch(e){return Promise.reject(e)}};exports.addTrailing0x=function(e){return e.startsWith("0x")?e:"0x"+e},exports.aesCbcDecrypt=u,exports.aesCbcEncrypt=s,exports.assert=l,exports.checkPrivateKey=B,exports.decrypt=function(e,r){try{var t;return Promise.resolve(x(e,r.ephemPublicKey)).then((function(e){return Promise.resolve(K(e)).then((function(e){t=Buffer.from(e.slice(0,32));var n=e.slice(32),o=Buffer.concat([r.iv,r.ephemPublicKey,r.ciphertext]);return Promise.resolve(k(Buffer.from(n),o,r.mac)).then((function(e){return l(e,"Bad MAC"),Promise.resolve(u(r.iv,t,r.ciphertext)).then((function(e){return Buffer.from(new Uint8Array(e))}))}))}))}))}catch(e){return Promise.reject(e)}},exports.derive=x,exports.encrypt=function(e,r,t){try{var n,o,i,f;t=t||{};var u=v(32);return o=g(u),Promise.resolve(x(u,e)).then((function(e){return Promise.resolve(K(e)).then((function(e){var u;n=(null===(u=t)||void 0===u?void 0:u.iv)||v(16);var c=e.slice(0,32);return f=Buffer.from(e.slice(32)),Promise.resolve(s(n,Buffer.from(c),r)).then((function(e){i=e;var r=Buffer.concat([n,o,i]);return Promise.resolve(j(f,r)).then((function(e){return{iv:n,ephemPublicKey:o,ciphertext:i,mac:e}}))}))}))}))}catch(e){return Promise.reject(e)}},exports.equalConstTime=m,exports.generateKeyPair=function(){var e=d();return{privateKey:e,publicKey:g(e)}},exports.generatePrivate=d,exports.getCurve=P,exports.getPublic=g,exports.getPublicCompressed=function(e){return B(e),Buffer.from(y.keyFromPrivate(e).getPublic(!0,"hex"),"hex")},exports.hashSharedKey=K,exports.hmacSha256Sign=j,exports.hmacSha256Verify=k,exports.isScalar=h,exports.isValidPrivateKey=p,exports.randomBytes=v,exports.removeTrailing0x=b,exports.sha256=function(e){try{var r=i.toUtf8Bytes(e),t=f.sha256(r);return Promise.resolve(b(t))}catch(e){return Promise.reject(e)}},exports.sha512=function(e){try{var r=i.toUtf8Bytes(e),t=f.sha512(r);return Promise.resolve(b(t))}catch(e){return Promise.reject(e)}},exports.sign=function(e,r){try{return l(32===e.length,"Bad private key"),l(p(e),"Bad private key"),l(r.length>0,"Message should not be empty"),l(r.length<=32,"Message is too long"),Promise.resolve(Buffer.from(y.sign(r,e,{canonical:!0}).toDER()))}catch(e){return Promise.reject(e)}},exports.verify=function(e,r,t){try{if(l(65===e.length||33===e.length,"Bad public key"),65===e.length&&l(4===e[0],"Bad public key"),33===e.length&&l(2===e[0]||3===e[0],"Bad public key"),l(r.length>0,"Message should not be empty"),l(r.length<=32,"Message is too long"),y.verify(r,t,e))return Promise.resolve(null);throw new Error("Bad signature")}catch(e){return Promise.reject(e)}}; | ||
//# sourceMappingURL=eccrypto-js.cjs.production.min.js.map |
import aesJs from 'aes-js'; | ||
import { ec } from 'elliptic'; | ||
import { randomBytes as randomBytes$1 } from '@ethersproject/random'; | ||
import { arrayify } from '@ethersproject/bytes'; | ||
import { toUtf8Bytes } from '@ethersproject/strings'; | ||
import { arrayify } from '@ethersproject/bytes'; | ||
import { sha256 as sha256$1, sha512 as sha512$1, computeHmac, SupportedAlgorithm } from '@ethersproject/sha2'; | ||
import { computeHmac, SupportedAlgorithm, sha512 as sha512$1, sha256 as sha256$1 } from '@ethersproject/sha2'; | ||
function aesCbcEncrypt(iv, key, data) { | ||
return new Promise(function (resolve) { | ||
var aesCbcDecrypt = function aesCbcDecrypt(iv, key, data) { | ||
try { | ||
var aesCbc = new aesJs.ModeOfOperation.cbc(key, iv); | ||
var encryptedBytes = aesCbc.decrypt(data); | ||
return Promise.resolve(Buffer.from(encryptedBytes)); | ||
} catch (e) { | ||
return Promise.reject(e); | ||
} | ||
}; | ||
var aesCbcEncrypt = function aesCbcEncrypt(iv, key, data) { | ||
try { | ||
var aesCbc = new aesJs.ModeOfOperation.cbc(key, iv); | ||
var encryptedBytes = aesCbc.encrypt(data); | ||
resolve(Buffer.from(encryptedBytes)); | ||
}); | ||
} | ||
function aesCbcDecrypt(iv, key, data) { | ||
return new Promise(function (resolve) { | ||
var aesCbc = new aesJs.ModeOfOperation.cbc(key, iv); | ||
var encryptedBytes = aesCbc.decrypt(data); | ||
resolve(Buffer.from(encryptedBytes)); | ||
}); | ||
} | ||
return Promise.resolve(Buffer.from(encryptedBytes)); | ||
} catch (e) { | ||
return Promise.reject(e); | ||
} | ||
}; | ||
@@ -66,2 +70,39 @@ var EC_GROUP_ORDER = | ||
var verify = function verify(publicKey, msg, sig) { | ||
try { | ||
assert(publicKey.length === 65 || publicKey.length === 33, 'Bad public key'); | ||
if (publicKey.length === 65) { | ||
assert(publicKey[0] === 4, 'Bad public key'); | ||
} | ||
if (publicKey.length === 33) { | ||
assert(publicKey[0] === 2 || publicKey[0] === 3, 'Bad public key'); | ||
} | ||
assert(msg.length > 0, 'Message should not be empty'); | ||
assert(msg.length <= 32, 'Message is too long'); | ||
if (secp256k1curve.verify(msg, sig, publicKey)) { | ||
return Promise.resolve(null); | ||
} else { | ||
throw new Error('Bad signature'); | ||
} | ||
} catch (e) { | ||
return Promise.reject(e); | ||
} | ||
}; | ||
var sign = function sign(privateKey, msg) { | ||
try { | ||
assert(privateKey.length === 32, 'Bad private key'); | ||
assert(isValidPrivateKey(privateKey), 'Bad private key'); | ||
assert(msg.length > 0, 'Message should not be empty'); | ||
assert(msg.length <= 32, 'Message is too long'); | ||
return Promise.resolve(Buffer.from(secp256k1curve.sign(msg, privateKey, { | ||
canonical: true | ||
}).toDER())); | ||
} catch (e) { | ||
return Promise.reject(e); | ||
} | ||
}; | ||
var secp256k1curve = | ||
@@ -118,177 +159,152 @@ /*#__PURE__*/ | ||
} | ||
function sign(privateKey, msg) { | ||
return new Promise(function (resolve) { | ||
try { | ||
assert(privateKey.length === 32, 'Bad private key'); | ||
assert(isValidPrivateKey(privateKey), 'Bad private key'); | ||
assert(msg.length > 0, 'Message should not be empty'); | ||
assert(msg.length <= 32, 'Message is too long'); | ||
resolve(Buffer.from(secp256k1curve.sign(msg, privateKey, { | ||
canonical: true | ||
}).toDER())); | ||
return Promise.resolve(); | ||
} catch (e) { | ||
return Promise.reject(e); | ||
} | ||
}); | ||
} | ||
function verify(publicKey, msg, sig) { | ||
return new Promise(function (resolve, reject) { | ||
assert(publicKey.length === 65 || publicKey.length === 33, 'Bad public key'); | ||
if (publicKey.length === 65) { | ||
assert(publicKey[0] === 4, 'Bad public key'); | ||
} | ||
var derive = function derive(privateKeyA, publicKeyB) { | ||
try { | ||
assert(Buffer.isBuffer(privateKeyA), 'Bad private key'); | ||
assert(Buffer.isBuffer(publicKeyB), 'Bad public key'); | ||
assert(privateKeyA.length === 32, 'Bad private key'); | ||
assert(isValidPrivateKey(privateKeyA), 'Bad private key'); | ||
assert(publicKeyB.length === 65 || publicKeyB.length === 33, 'Bad public key'); | ||
if (publicKey.length === 33) { | ||
assert(publicKey[0] === 2 || publicKey[0] === 3, 'Bad public key'); | ||
if (publicKeyB.length === 65) { | ||
assert(publicKeyB[0] === 4, 'Bad public key'); | ||
} | ||
assert(msg.length > 0, 'Message should not be empty'); | ||
assert(msg.length <= 32, 'Message is too long'); | ||
if (secp256k1curve.verify(msg, sig, publicKey)) { | ||
resolve(null); | ||
} else { | ||
reject(new Error('Bad signature')); | ||
if (publicKeyB.length === 33) { | ||
assert(publicKeyB[0] === 2 || publicKeyB[0] === 3, 'Bad public key'); | ||
} | ||
}); | ||
} | ||
function derive(privateKeyA, publicKeyB) { | ||
return new Promise(function (resolve) { | ||
try { | ||
assert(Buffer.isBuffer(privateKeyA), 'Bad private key'); | ||
assert(Buffer.isBuffer(publicKeyB), 'Bad public key'); | ||
assert(privateKeyA.length === 32, 'Bad private key'); | ||
assert(isValidPrivateKey(privateKeyA), 'Bad private key'); | ||
assert(publicKeyB.length === 65 || publicKeyB.length === 33, 'Bad public key'); | ||
var keyA = getCurve().keyFromPrivate(privateKeyA); | ||
var keyB = getCurve().keyFromPublic(publicKeyB); | ||
var sharedKey = keyA.derive(keyB.getPublic()); // BN instance | ||
if (publicKeyB.length === 65) { | ||
assert(publicKeyB[0] === 4, 'Bad public key'); | ||
} | ||
return Promise.resolve(Buffer.from(sharedKey.toArray())); | ||
} catch (e) { | ||
return Promise.reject(e); | ||
} | ||
}; | ||
if (publicKeyB.length === 33) { | ||
assert(publicKeyB[0] === 2 || publicKeyB[0] === 3, 'Bad public key'); | ||
} | ||
function removeTrailing0x(str) { | ||
if (str.startsWith('0x')) { | ||
return str.substring(2); | ||
} else { | ||
return str; | ||
} | ||
} | ||
function addTrailing0x(str) { | ||
if (!str.startsWith('0x')) { | ||
return '0x' + str; | ||
} else { | ||
return str; | ||
} | ||
} | ||
var keyA = getCurve().keyFromPrivate(privateKeyA); | ||
var keyB = getCurve().keyFromPublic(publicKeyB); | ||
var Px = keyA.derive(keyB.getPublic()); // BN instance | ||
var hmacSha256Verify = function hmacSha256Verify(key, msg, sig) { | ||
try { | ||
return Promise.resolve(hmacSha256Sign(key, msg)).then(function (expectedSig) { | ||
return equalConstTime(expectedSig, sig); | ||
}); | ||
} catch (e) { | ||
return Promise.reject(e); | ||
} | ||
}; | ||
var hmacSha256Sign = function hmacSha256Sign(key, msg) { | ||
try { | ||
var result = computeHmac(SupportedAlgorithm.sha256, key, msg); | ||
return Promise.resolve(Buffer.from(result)); | ||
} catch (e) { | ||
return Promise.reject(e); | ||
} | ||
}; | ||
var hashSharedKey = function hashSharedKey(sharedKey) { | ||
try { | ||
var hash = sha512$1(sharedKey); | ||
resolve(Buffer.from(Px.toArray())); | ||
return Promise.resolve(); | ||
} catch (e) { | ||
return Promise.reject(e); | ||
} | ||
}); | ||
} | ||
return Promise.resolve(arrayify(hash)); | ||
} catch (e) { | ||
return Promise.reject(e); | ||
} | ||
}; | ||
var sha512 = function sha512(msg) { | ||
try { | ||
var bytes = toUtf8Bytes(msg); | ||
function sha256(msg) { | ||
return new Promise(function (resolve) { | ||
try { | ||
var bytes = toUtf8Bytes(msg); | ||
var hash = sha512$1(bytes); | ||
var hash = sha256$1(bytes); | ||
return Promise.resolve(removeTrailing0x(hash)); | ||
} catch (e) { | ||
return Promise.reject(e); | ||
} | ||
}; | ||
var sha256 = function sha256(msg) { | ||
try { | ||
var bytes = toUtf8Bytes(msg); | ||
resolve(arrayify(hash)); | ||
return Promise.resolve(); | ||
} catch (e) { | ||
return Promise.reject(e); | ||
} | ||
}); | ||
} | ||
function sha512(msg) { | ||
return new Promise(function (resolve) { | ||
try { | ||
var bytes = toUtf8Bytes(msg); | ||
var hash = sha256$1(bytes); | ||
var hash = sha512$1(bytes); | ||
return Promise.resolve(removeTrailing0x(hash)); | ||
} catch (e) { | ||
return Promise.reject(e); | ||
} | ||
}; | ||
resolve(arrayify(hash)); | ||
return Promise.resolve(); | ||
} catch (e) { | ||
return Promise.reject(e); | ||
} | ||
}); | ||
} | ||
function hmacSha256Sign(key, msg) { | ||
return new Promise(function (resolve) { | ||
try { | ||
var result = computeHmac(SupportedAlgorithm.sha256, key, msg); | ||
resolve(Buffer.from(result)); | ||
return Promise.resolve(); | ||
} catch (e) { | ||
return Promise.reject(e); | ||
} | ||
}); | ||
} | ||
function hmacSha256Verify(key, msg, sig) { | ||
return new Promise(function (resolve) { | ||
try { | ||
return Promise.resolve(hmacSha256Sign(key, msg)).then(function (expectedSig) { | ||
resolve(equalConstTime(expectedSig, sig)); | ||
var decrypt = function decrypt(privateKey, opts) { | ||
try { | ||
// Tmp variable to save context from flat promises; | ||
var encryptionKey; | ||
return Promise.resolve(derive(privateKey, opts.ephemPublicKey)).then(function (sharedKey) { | ||
return Promise.resolve(hashSharedKey(sharedKey)).then(function (hash) { | ||
encryptionKey = Buffer.from(hash.slice(0, 32)); | ||
var macKey = hash.slice(32); | ||
var dataToMac = Buffer.concat([opts.iv, opts.ephemPublicKey, opts.ciphertext]); | ||
return Promise.resolve(hmacSha256Verify(Buffer.from(macKey), dataToMac, opts.mac)).then(function (macGood) { | ||
assert(macGood, 'Bad MAC'); | ||
return Promise.resolve(aesCbcDecrypt(opts.iv, encryptionKey, opts.ciphertext)).then(function (msg) { | ||
return Buffer.from(new Uint8Array(msg)); | ||
}); | ||
}); | ||
}); | ||
} catch (e) { | ||
return Promise.reject(e); | ||
} | ||
}); | ||
} | ||
}); | ||
} catch (e) { | ||
return Promise.reject(e); | ||
} | ||
}; | ||
var encrypt = function encrypt(publicKeyTo, msg, opts) { | ||
try { | ||
opts = opts || {}; // Tmp variables to save context from flat promises; | ||
function encrypt(publicKeyTo, msg, opts) { | ||
opts = opts || {}; // Tmp variables to save context from flat promises; | ||
var iv; | ||
var ephemPublicKey; | ||
var ciphertext; | ||
var macKey; | ||
var ephemPrivateKey = randomBytes(32); // There is a very unlikely possibility that it is not a valid key | ||
var iv; | ||
var ephemPublicKey; | ||
var ciphertext; | ||
var macKey; | ||
return new Promise(function (resolve) { | ||
try { | ||
var ephemPrivateKey = randomBytes(32); // There is a very unlikely possibility that it is not a valid key | ||
ephemPublicKey = getPublic(ephemPrivateKey); | ||
return Promise.resolve(derive(ephemPrivateKey, publicKeyTo)).then(function (sharedKey) { | ||
return Promise.resolve(hashSharedKey(sharedKey)).then(function (hash) { | ||
var _opts; | ||
ephemPublicKey = getPublic(ephemPrivateKey); | ||
resolve(derive(ephemPrivateKey, publicKeyTo)); | ||
return Promise.resolve(); | ||
} catch (e) { | ||
return Promise.reject(e); | ||
} | ||
}).then(function (Px) { | ||
return sha512(Px); | ||
}).then(function (hash) { | ||
iv = opts.iv || randomBytes(16); | ||
var encryptionKey = hash.slice(0, 32); | ||
macKey = Buffer.from(hash.slice(32)); | ||
return aesCbcEncrypt(iv, Buffer.from(encryptionKey), msg); | ||
}).then(function (data) { | ||
ciphertext = data; | ||
var dataToMac = Buffer.concat([iv, ephemPublicKey, ciphertext]); | ||
return hmacSha256Sign(macKey, dataToMac); | ||
}).then(function (mac) { | ||
return { | ||
iv: iv, | ||
ephemPublicKey: ephemPublicKey, | ||
ciphertext: ciphertext, | ||
mac: mac | ||
}; | ||
}); | ||
} | ||
function decrypt(privateKey, opts) { | ||
// Tmp variable to save context from flat promises; | ||
var encryptionKey; | ||
return derive(privateKey, opts.ephemPublicKey).then(function (Px) { | ||
return sha512(Px); | ||
}).then(function (hash) { | ||
encryptionKey = Buffer.from(hash.slice(0, 32)); | ||
var macKey = hash.slice(32); | ||
var dataToMac = Buffer.concat([opts.iv, opts.ephemPublicKey, opts.ciphertext]); | ||
return hmacSha256Verify(Buffer.from(macKey), dataToMac, opts.mac); | ||
}).then(function (macGood) { | ||
assert(macGood, 'Bad MAC'); | ||
return aesCbcDecrypt(opts.iv, encryptionKey, opts.ciphertext); | ||
}).then(function (msg) { | ||
return Buffer.from(new Uint8Array(msg)); | ||
}); | ||
} | ||
iv = ((_opts = opts) === null || _opts === void 0 ? void 0 : _opts.iv) || randomBytes(16); | ||
var encryptionKey = hash.slice(0, 32); | ||
macKey = Buffer.from(hash.slice(32)); | ||
return Promise.resolve(aesCbcEncrypt(iv, Buffer.from(encryptionKey), msg)).then(function (data) { | ||
ciphertext = data; | ||
var dataToMac = Buffer.concat([iv, ephemPublicKey, ciphertext]); | ||
return Promise.resolve(hmacSha256Sign(macKey, dataToMac)).then(function (mac) { | ||
return { | ||
iv: iv, | ||
ephemPublicKey: ephemPublicKey, | ||
ciphertext: ciphertext, | ||
mac: mac | ||
}; | ||
}); | ||
}); | ||
}); | ||
}); | ||
} catch (e) { | ||
return Promise.reject(e); | ||
} | ||
}; | ||
export { aesCbcDecrypt, aesCbcEncrypt, assert, checkPrivateKey, decrypt, derive, encrypt, equalConstTime, generateKeyPair, generatePrivate, getCurve, getPublic, getPublicCompressed, hmacSha256Sign, hmacSha256Verify, isScalar, isValidPrivateKey, randomBytes, sha256, sha512, sign, verify }; | ||
export { addTrailing0x, aesCbcDecrypt, aesCbcEncrypt, assert, checkPrivateKey, decrypt, derive, encrypt, equalConstTime, generateKeyPair, generatePrivate, getCurve, getPublic, getPublicCompressed, hashSharedKey, hmacSha256Sign, hmacSha256Verify, isScalar, isValidPrivateKey, randomBytes, removeTrailing0x, sha256, sha512, sign, verify }; | ||
//# sourceMappingURL=eccrypto-js.esm.js.map |
@@ -20,2 +20,2 @@ /// <reference types="node" /> | ||
export declare function sign(privateKey: Buffer, msg: Buffer): Promise<Buffer>; | ||
export declare function verify(publicKey: Buffer, msg: Buffer, sig: Buffer): Promise<unknown>; | ||
export declare function verify(publicKey: Buffer, msg: Buffer, sig: Buffer): Promise<null>; |
/// <reference types="node" /> | ||
import { Encrypted } from './types'; | ||
export declare function encrypt(publicKeyTo: Buffer, msg: Buffer, opts: Encrypted): Promise<{ | ||
export declare function encrypt(publicKeyTo: Buffer, msg: Buffer, opts?: Encrypted): Promise<{ | ||
iv: Buffer; | ||
@@ -5,0 +5,0 @@ ephemPublicKey: Buffer; |
@@ -8,2 +8,3 @@ export * from './aes'; | ||
export * from './types'; | ||
export * from './util'; | ||
export * from './validators'; |
/// <reference types="node" /> | ||
export declare function sha256(msg: string): Promise<Uint8Array>; | ||
export declare function sha512(msg: string): Promise<Uint8Array>; | ||
export declare function sha256(msg: string): Promise<string>; | ||
export declare function sha512(msg: string): Promise<string>; | ||
export declare function hashSharedKey(sharedKey: Buffer): Promise<Uint8Array>; | ||
export declare function hmacSha256Sign(key: Buffer, msg: Buffer): Promise<Buffer>; | ||
export declare function hmacSha256Verify(key: Buffer, msg: Buffer, sig: Buffer): Promise<boolean>; |
@@ -13,1 +13,7 @@ /// <reference types="node" /> | ||
}>; | ||
export declare function testEncrypt(publicKey: Buffer): Promise<{ | ||
iv: Buffer; | ||
ephemPublicKey: Buffer; | ||
ciphertext: Buffer; | ||
mac: Buffer; | ||
}>; |
{ | ||
"name": "eccrypto-js", | ||
"description": "Pure JavaScript Elliptic curve cryptography library", | ||
"version": "0.2.5", | ||
"version": "0.2.6", | ||
"author": "Pedro Gomes <github.com/pedrouid>", | ||
@@ -6,0 +6,0 @@ "license": "MIT", |
@@ -21,5 +21,5 @@ # eccrypto-js [![npm version](https://badge.fury.io/js/eccrypto-js.svg)](https://badge.fury.io/js/eccrypto-js) | ||
const str = 'message to sign'; | ||
const hash = await eccryptoJS.sha256(str); | ||
const msg = Buffer.from(hash, 'hex'); | ||
const msg = eccryptoJS.sha256(str); | ||
const sig = await eccryptoJS.sign(keyPair.privateKey, msg); | ||
@@ -26,0 +26,0 @@ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
82141
22
684