Commit 17afc23651a3a861a684676695d0562d07145584
implement encryption, decryption
Dominic Tarr committed on 7/1/2015, 10:53:00 PMParent: 1a34ba680e6e6efa7e5c46b8684644ecba505534
Files changed
index.js | changed |
package.json | changed |
index.js | ||
---|---|---|
@@ -7,10 +7,13 @@ | ||
7 | 7 | var createHmac = require('hmac') |
8 | 8 | var Blake2s = require('blake2s') |
9 | 9 | |
10 | 10 | var ecc = require('./eccjs') |
11 | +var sodium = require('sodium').api | |
11 | 12 | var isRef = require('ssb-ref') |
12 | 13 | |
14 | +var pb = require('private-box') | |
15 | + | |
13 | 16 | //UTILS |
14 | 17 | |
15 | 18 | function clone (obj) { |
16 | 19 | var _obj = {} |
@@ -305,6 +308,26 @@ | ||
305 | 308 | // ts: Date.now(), |
306 | 309 | // public: keys.public |
307 | 310 | // }) |
308 | 311 | //} |
309 | -// | |
310 | 312 | |
313 | +exports.box = function (msg, recipients) { | |
314 | + msg = new Buffer(JSON.stringify(msg)) | |
315 | + | |
316 | + recipients = recipients.map(function (keys) { | |
317 | + var public = keys.public || keys | |
318 | + return sodium.crypto_sign_ed25519_pk_to_curve25519(toBuffer(public)) | |
319 | + }) | |
320 | + | |
321 | + //it's since the nonce is 24 bytes (a multiple of 3) | |
322 | + //it's possible to concatenate the base64 strings | |
323 | + //and still have a valid base64 string. | |
324 | + return pb.multibox(msg, recipients).toString('base64')+'.box' | |
325 | +} | |
326 | + | |
327 | +exports.unbox = function (boxed, keys) { | |
328 | + boxed = toBuffer(boxed) | |
329 | + var sk = sodium.crypto_sign_ed25519_sk_to_curve25519(toBuffer(keys.private || keys)) | |
330 | + | |
331 | + var msg = pb.multibox_open(boxed, sk) | |
332 | + if(msg) return JSON.parse(''+msg) | |
333 | +} |
package.json | ||
---|---|---|
@@ -13,13 +13,14 @@ | ||
13 | 13 | "eccjs": "git://github.com/dominictarr/eccjs.git#586f6d47507184a2efe84684ed0a30605cbc43a5", |
14 | 14 | "hmac": "~1.0.1", |
15 | 15 | "libsodium-wrappers": "^0.2.8", |
16 | 16 | "mkdirp": "~0.5.0", |
17 | - "sodium": "^1.0.17", | |
17 | + "private-box": "0.0.0", | |
18 | + "sodium": "git://github.com/dominictarr/node-sodium.git", | |
18 | 19 | "ssb-ref": "~0.0.0" |
19 | 20 | }, |
20 | 21 | "devDependencies": { |
21 | - "tape": "~3.0.0" | |
22 | + "tape": "^3.0.3" | |
22 | 23 | }, |
23 | 24 | "browser": { |
24 | 25 | "./sodium": "./browser-sodium" |
25 | 26 | }, |
Built with git-ssb-web