implement and use prototype curve encryptors

This commit is contained in:
ansuz
2017-07-06 10:22:20 +02:00
parent d293ba4d44
commit df53166b37
2 changed files with 42 additions and 41 deletions

View File

@@ -2,35 +2,36 @@ define([
'/bower_components/tweetnacl/nacl-fast.min.js',
], function () {
var Nacl = window.nacl;
var Curve = {};
// nacl.box(message, nonce, theirPublicKey, mySecretKey)
Curve.encrypt = function (message, theirPub, mySecret) {
var buffer = Nacl.util.decodeUTF8(message);
var nonce = Nacl.randomBytes(24);
var box = Nacl.box(buffer, nonce, theirPub, mySecret);
return [Nacl.util.encodeBase64(nonce), Nacl.util.encodeBase64(box)].join('|');
return Nacl.util.encodeBase64(nonce) + '|' + Nacl.util.encodeBase64(box);
};
// nacl.box.open(box, nonce, theirPublicKey, mySecretKey)
Curve.decrypt = function (packed, theirPub, mySecret) {
var unpacked = packed.split('|');
var nonce = Nacl.util.decodeBase64(unpacked[0]);
var box = Nacl.util.decodeBase64(unpacked[1]);
var message = Nacl.box.open(box, nonce, theirPub, mySecret);
return Nacl.util.encodeUTF8(message);
};
Curve.createEncryptor = function () {
console.log("PEWPEW");
throw new Error("E_NOT_IMPL");
Curve.createEncryptor = function (theirPublic, mySecret) {
var theirs = Nacl.util.decodeBase64(theirPublic);
var mine = Nacl.util.decodeBase64(mySecret);
return {
encrypt: function (msg) {
return Curve.encrypt(msg, theirs, mine);
},
decrypt: function (packed) {
return Curve.decrypt(packed, theirs, mine);
}
};
};
return Curve;