Skip to content

Instantly share code, notes, and snippets.

@sponnet
Last active March 29, 2018 04:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sponnet/f91d826dca97321df0e3bb629ba1c621 to your computer and use it in GitHub Desktop.
Save sponnet/f91d826dca97321df0e3bb629ba1c621 to your computer and use it in GitHub Desktop.
const {
performance
} = require('perf_hooks');
const EthCrypto = require('eth-crypto');
const identity = EthCrypto.createIdentity();
function randomString(string_length) {
var chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXTZabcdefghiklmnopqrstuvwxyz";
//var string_length = 8;
var randomstring = '';
for (var i = 0; i < string_length; i++) {
var rnum = Math.floor(Math.random() * chars.length);
randomstring += chars.substring(rnum, rnum + 1);
}
return randomstring;
}
const msg = randomString(100000);
performance.mark('A');
EthCrypto.encryptWithPublicKey(
identity.publicKey,
msg // message
).then((encrypted) => {
performance.mark('B');
performance.measure('A to B', 'A', 'B');
console.log(encrypted);
performance.mark('C');
EthCrypto.decryptWithPrivateKey(
identity.privateKey,
encrypted)
.then((message) => {
performance.mark('D');
performance.measure('C to D', 'C', 'D');
console.log('message length ' + message.length);
console.log('message encrypted. Duration ' + performance.getEntriesByName('A to B')[0].duration + ' ms');
console.log('message decrypted. Duration ' + performance.getEntriesByName('C to D')[0].duration + ' ms');
});
});
{
"name": "encrypttest",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"dependencies": {
"eth-crypto": "^0.4.0",
"perf_hooks": "0.0.1"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment