Comparing version 0.2.1 to 0.3.0
/// <reference types="node" /> | ||
import { aesDecrypt, aesEncrypt, decodeHex, getValidSecret, remove0x } from "./utils"; | ||
export declare function encrypt(receiverPubhex: string, msg: Buffer): Buffer; | ||
export declare function decrypt(receiverPrvhex: string, msg: Buffer): Buffer; | ||
export declare function encrypt(receiverRawPub: string | Buffer, msg: Buffer): Buffer; | ||
export declare function decrypt(receiverRawPrv: string | Buffer, msg: Buffer): Buffer; | ||
export { PrivateKey, PublicKey } from "./keys"; | ||
@@ -6,0 +6,0 @@ export declare const utils: { |
@@ -5,14 +5,17 @@ "use strict"; | ||
var utils_1 = require("./utils"); | ||
function encrypt(receiverPubhex, msg) { | ||
var disposableKey = new keys_1.PrivateKey(); | ||
var receiverPubkey = keys_1.PublicKey.fromHex(receiverPubhex); | ||
var aesKey = disposableKey.encapsulate(receiverPubkey); | ||
var UNCOMPRESSED_PUBLIC_KEY_SIZE = 65; | ||
function encrypt(receiverRawPub, msg) { | ||
var ephemeralKey = new keys_1.PrivateKey(); | ||
var receiverPubkey = receiverRawPub instanceof Buffer ? | ||
new keys_1.PublicKey(receiverRawPub) : keys_1.PublicKey.fromHex(receiverRawPub); | ||
var aesKey = ephemeralKey.encapsulate(receiverPubkey); | ||
var encrypted = utils_1.aesEncrypt(aesKey, msg); | ||
return Buffer.concat([disposableKey.publicKey.uncompressed, encrypted]); | ||
return Buffer.concat([ephemeralKey.publicKey.uncompressed, encrypted]); | ||
} | ||
exports.encrypt = encrypt; | ||
function decrypt(receiverPrvhex, msg) { | ||
var receiverPrvkey = keys_1.PrivateKey.fromHex(receiverPrvhex); | ||
var senderPubkey = new keys_1.PublicKey(msg.slice(0, 65)); | ||
var encrypted = msg.slice(65); | ||
function decrypt(receiverRawPrv, msg) { | ||
var receiverPrvkey = receiverRawPrv instanceof Buffer ? | ||
new keys_1.PrivateKey(receiverRawPrv) : keys_1.PrivateKey.fromHex(receiverRawPrv); | ||
var senderPubkey = new keys_1.PublicKey(msg.slice(0, UNCOMPRESSED_PUBLIC_KEY_SIZE)); | ||
var encrypted = msg.slice(UNCOMPRESSED_PUBLIC_KEY_SIZE); | ||
var aesKey = senderPubkey.decapsulate(receiverPrvkey); | ||
@@ -19,0 +22,0 @@ return utils_1.aesDecrypt(aesKey, encrypted); |
@@ -13,13 +13,5 @@ "use strict"; | ||
var utils_1 = require("../utils"); | ||
var ETH_PRVHEX = "0x95d3c5e483e9b1d4f5fc8e79b2deaf51362980de62dbb082a9a4257eef653d7d"; | ||
var ETH_PUBHEX = "0x98afe4f150642cd05cc9d2fa36458ce0a58567daeaf5fde7333ba9b403011140" | ||
+ "a4e28911fcf83ab1f457a30b4959efc4b9306f514a4c3711a16a80e3b47eb58b"; | ||
var PYTHON_BACKEND = "https://eciespy.herokuapp.com/"; | ||
describe("test encrypt and decrypt", function () { | ||
var text = "helloworld"; | ||
var config = { | ||
headers: { | ||
"Content-Type": "application/x-www-form-urlencoded", | ||
}, | ||
}; | ||
var TEXT = "helloworld"; | ||
it("tests aes with random key", function () { | ||
@@ -30,3 +22,3 @@ var key = crypto_1.randomBytes(32); | ||
}); | ||
it("tests aes decrypt with known key and text", function () { | ||
it("tests aes decrypt with known key and TEXT", function () { | ||
var key = Buffer.from(utils_1.decodeHex("0000000000000000000000000000000000000000000000000000000000000000")); | ||
@@ -38,15 +30,31 @@ var nonce = Buffer.from(utils_1.decodeHex("f3e1ba810d2c8900b11312b7c725565f")); | ||
var decrypted = utils_1.aesDecrypt(key, data); | ||
chai_1.expect(decrypted.toString()).to.be.equal(text); | ||
chai_1.expect(decrypted.toString()).to.be.equal(TEXT); | ||
}); | ||
it("test encrypt/decrypt against python version", function () { | ||
var prv = new keys_1.PrivateKey(utils_1.decodeHex(ETH_PRVHEX)); | ||
it("tests encrypt/decrypt buffer", function () { | ||
var prv1 = new keys_1.PrivateKey(); | ||
var encrypted1 = index_1.encrypt(prv1.publicKey.uncompressed, Buffer.from(TEXT)); | ||
chai_1.expect(index_1.decrypt(prv1.secret, encrypted1).toString()).to.be.equal(TEXT); | ||
var prv2 = new keys_1.PrivateKey(); | ||
var encrypted2 = index_1.encrypt(prv2.publicKey.compressed, Buffer.from(TEXT)); | ||
chai_1.expect(index_1.decrypt(prv2.secret, encrypted2).toString()).to.be.equal(TEXT); | ||
}); | ||
it("tests encrypt/decrypt hex", function () { | ||
var prv1 = new keys_1.PrivateKey(); | ||
var encrypted1 = index_1.encrypt(prv1.publicKey.toHex(), Buffer.from(TEXT)); | ||
chai_1.expect(index_1.decrypt(prv1.toHex(), encrypted1).toString()).to.be.equal(TEXT); | ||
var prv2 = new keys_1.PrivateKey(); | ||
var encrypted2 = index_1.encrypt(prv2.publicKey.toHex(), Buffer.from(TEXT)); | ||
chai_1.expect(index_1.decrypt(prv2.toHex(), encrypted2).toString()).to.be.equal(TEXT); | ||
}); | ||
it("tests encrypt/decrypt against python version", function () { | ||
var prv = new keys_1.PrivateKey(); | ||
axios_1.default.post(PYTHON_BACKEND, querystring_1.stringify({ | ||
data: text, | ||
pub: ETH_PUBHEX, | ||
data: TEXT, | ||
pub: prv.publicKey.toHex(), | ||
})).then(function (res) { | ||
var encryptedKnown = Buffer.from(utils_1.decodeHex(res.data)); | ||
var decrypted = index_1.decrypt(prv.toHex(), encryptedKnown); | ||
chai_1.expect(decrypted.toString()).to.be.equal(text); | ||
chai_1.expect(decrypted.toString()).to.be.equal(TEXT); | ||
}); | ||
var encrypted = index_1.encrypt(prv.publicKey.toHex(), Buffer.from(text)); | ||
var encrypted = index_1.encrypt(prv.publicKey.toHex(), Buffer.from(TEXT)); | ||
axios_1.default.post(PYTHON_BACKEND, querystring_1.stringify({ | ||
@@ -56,43 +64,6 @@ data: encrypted.toString("hex"), | ||
})).then(function (res) { | ||
chai_1.expect(text).to.be.equal(res.data); | ||
chai_1.expect(TEXT).to.be.equal(res.data); | ||
}); | ||
}); | ||
}); | ||
describe("test keys", function () { | ||
it("test invalid", function () { | ||
// 0 < private key < group order int | ||
var groupOrderInt = "fffffffffffffffffffffffffffffffebaaedce6af48a03bbfd25e8cd0364141"; | ||
chai_1.expect(function () { return new keys_1.PrivateKey(utils_1.decodeHex(groupOrderInt)); }).to.throw(Error); | ||
var groupOrderIntAdd1 = "fffffffffffffffffffffffffffffffebaaedce6af48a03bbfd25e8cd0364142"; | ||
chai_1.expect(function () { return new keys_1.PrivateKey(utils_1.decodeHex(groupOrderIntAdd1)); }).to.throw(Error); | ||
chai_1.expect(function () { return new keys_1.PrivateKey(utils_1.decodeHex("0")); }).to.throw(Error); | ||
}); | ||
it("tests equal", function () { | ||
var prv = new keys_1.PrivateKey(); | ||
var pub = keys_1.PublicKey.fromHex(prv.publicKey.toHex(false)); | ||
var isPubEqual = pub.uncompressed.equals(prv.publicKey.uncompressed); | ||
chai_1.expect(isPubEqual).to.be.equal(true); | ||
var isFromHexWorking = prv.equals(keys_1.PrivateKey.fromHex(prv.toHex())); | ||
chai_1.expect(isFromHexWorking).to.be.equal(true); | ||
}); | ||
it("tests eth key compatibility", function () { | ||
var ethPrv = keys_1.PrivateKey.fromHex(ETH_PRVHEX); | ||
var ethPub = keys_1.PublicKey.fromHex(ETH_PUBHEX); | ||
chai_1.expect(ethPub.equals(ethPrv.publicKey)).to.be.equal(true); | ||
}); | ||
it("tests multiply and hkdf", function () { | ||
var two = Buffer.from(new Uint8Array(32)); | ||
two[31] = 2; | ||
var three = Buffer.from(new Uint8Array(32)); | ||
three[31] = 3; | ||
var k1 = new keys_1.PrivateKey(two); | ||
var k2 = new keys_1.PrivateKey(three); | ||
chai_1.expect(k1.multiply(k2.publicKey).equals(k2.multiply(k1.publicKey))).to.be.equal(true); | ||
var derived = k1.encapsulate(k2.publicKey); | ||
var anotherDerived = k1.publicKey.decapsulate(k2); | ||
var knownDerived = Buffer.from(utils_1.decodeHex("6f982d63e8590c9d9b5b4c1959ff80315d772edd8f60287c9361d548d5200f82")); | ||
chai_1.expect(derived.equals(knownDerived)).to.be.equal(true); | ||
chai_1.expect(anotherDerived.equals(knownDerived)).to.be.equal(true); | ||
}); | ||
}); | ||
//# sourceMappingURL=crypt.test.js.map |
@@ -29,3 +29,3 @@ { | ||
}, | ||
"version": "0.2.1", | ||
"version": "0.3.0", | ||
"devDependencies": { | ||
@@ -32,0 +32,0 @@ "@types/chai": "^4.2.2", |
@@ -9,5 +9,5 @@ # eciesjs | ||
Elliptic Curve Integrated Encryption Scheme for secp256k1, written in TypeScript with **minimal** dependencies. | ||
Elliptic Curve Integrated Encryption Scheme for secp256k1 in TypeScript. | ||
This is the JavaScript/TypeScript version of [eciespy](https://github.com/kigawas/eciespy) with a built-in class-like secp256k1 [API](#privatekey), you may go there for detailed documentation of the mechanism under the hood. | ||
This is the JavaScript/TypeScript version of [eciespy](https://github.com/kigawas/eciespy) with a built-in class-like secp256k1 [API](#privatekey), you may go there for detailed documentation and learn the mechanism under the hood. | ||
@@ -30,7 +30,7 @@ ## Install | ||
### `encrypt(receiverPubhex: string, msg: Buffer): Buffer` | ||
### `encrypt(receiverRawPub: string | Buffer, msg: Buffer): Buffer` | ||
Parameters: | ||
- **receiverPubhex** - Receiver's secp256k1 public key hex string | ||
- **receiverRawPub** - Receiver's secp256k1 public key, hex string or buffer | ||
- **msg** - Data to encrypt | ||
@@ -40,7 +40,7 @@ | ||
### `decrypt(receiverPrvhex: string, msg: Buffer): Buffer` | ||
### `decrypt(receiverRawPrv: string | Buffer, msg: Buffer): Buffer` | ||
Parameters: | ||
- **receiverPrvhex** - Receiver's secp256k1 private key hex string | ||
- **receiverRawPrv** - Receiver's secp256k1 private key, hex string or buffer | ||
- **msg** - Data to decrypt | ||
@@ -47,0 +47,0 @@ |
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
20164
19
345