Skip to content

Instantly share code, notes, and snippets.

@witoldsz
Created March 21, 2018 11:28
Show Gist options
  • Save witoldsz/e692b7f3218cca747982170ec687747e to your computer and use it in GitHub Desktop.
Save witoldsz/e692b7f3218cca747982170ec687747e to your computer and use it in GitHub Desktop.
const example_key = {
pub: "-----BEGIN PUBLIC KEY-----\n" +
"MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA9kDZw/3AtZ2FSY1byp9A\n" +
"0kkJ1DJdX2iKDmE5YAaloWA92FNxQ9txd5ircSyjciv6RJ+CYF1k9G/0p6B2oV2i\n" +
"hSXHbiH5Bby7K6KSogD0toevhrLJovMegLrnrGUzzK1fBsYwjTFlsQPji6Nhbx5l\n" +
"woSbA4Ddz7VlLKUnnDA2gG3+FNSFSPCiY7e1+D/SIzBKynv2xp6Q2a1d1lT2uYpy\n" +
"NykYHH6VYVle544mL48RUR3JP9n0tgq27GQeotCIgMFia6Us811WVXz1/145u4d2\n" +
"GGtN5NRWxs5OC4NyKF28nkfj3ZB0/F2CWCv02Aogeziw/9eX9FoUlyDoD2/FUeVD\n" +
"HQIDAQAB\n" +
"-----END PUBLIC KEY-----\n",
priv: "-----BEGIN RSA PRIVATE KEY-----\n" +
"MIIEpQIBAAKCAQEA9kDZw/3AtZ2FSY1byp9A0kkJ1DJdX2iKDmE5YAaloWA92FNx\n" +
"Q9txd5ircSyjciv6RJ+CYF1k9G/0p6B2oV2ihSXHbiH5Bby7K6KSogD0toevhrLJ\n" +
"ovMegLrnrGUzzK1fBsYwjTFlsQPji6Nhbx5lwoSbA4Ddz7VlLKUnnDA2gG3+FNSF\n" +
"SPCiY7e1+D/SIzBKynv2xp6Q2a1d1lT2uYpyNykYHH6VYVle544mL48RUR3JP9n0\n" +
"tgq27GQeotCIgMFia6Us811WVXz1/145u4d2GGtN5NRWxs5OC4NyKF28nkfj3ZB0\n" +
"/F2CWCv02Aogeziw/9eX9FoUlyDoD2/FUeVDHQIDAQABAoIBAQDuloym+Q+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PTmV0GreHNbLR/LrB+avrj8gjrOPlq\n" +
"BinyWsECgYEA4f3CxXZ/TFiOPJgf4jLwIFVd5r6swBu43sk/7DDJFcD26ptyNUqi\n" +
"iJXxsTlCqLlDW2H0CHE3/8JmO+oFMesuaH7QaLeAWIHNiie5GdVaMgiTEty1bXmv\n" +
"alRh1eIWthuhykgmSCQESKZTi82Jh93YO1nUbbkVaJ8jol1DKllgsKM=\n" +
"-----END RSA PRIVATE KEY-----\n"
};
const sshpk = require('sshpk');
const REPEAT = 1000;
const someData = JSON.stringify(example_key);
function verify(data, sign) {
const keyInfo = sshpk.parseKey(example_key.pub);
return keyInfo
.createVerify('sha256')
.update(new Buffer(data, 'utf8'))
.verify(sign, 'base64');
}
function sign(data) {
const privateRSA = sshpk.parsePrivateKey(example_key.priv);
return privateRSA.createSign('sha256').update(data).sign().toString();
}
for (let i = 0; i < 100; ++i) verify(someData + i, sign(someData + i));
console.log()
const someDataSigned = [];
console.time(`sign message ${REPEAT} times`)
for (let i = 1; i <= REPEAT; ++i) {
someDataSigned[i] = sign(i + someData + i);
}
console.timeEnd(`sign message ${REPEAT} times`)
console.time(`verify message ${REPEAT} times`)
for (let i = 1; i <= REPEAT; ++i) {
verify(i + someData + i, someDataSigned[i]);
}
console.timeEnd(`verify message ${REPEAT} times`);
@witoldsz
Copy link
Author

On my PC the results are:

sign message 1000 times: 1393.250ms
verify message 1000 times: 135.105ms

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment