git ssb

1+

Dominic / ssb-keys



Commit 6b40180b4e3563ed9534a8d103e8c8c55c3ee66c

Merge pull request #17 from dominictarr/boxunbox

Box unbox
Dominic Tarr committed on 7/2/2015, 9:37:07 AM
Parent: 31fc81304b476aa179bd085b401d58d5ba417878
Parent: 17afc23651a3a861a684676695d0562d07145584

Files changed

index.jschanged
package.jsonchanged
test/box-unbox.jsadded
index.jsView
@@ -7,10 +7,13 @@
77 var createHmac = require('hmac')
88 var Blake2s = require('blake2s')
99
1010 var ecc = require('./eccjs')
11+var sodium = require('sodium').api
1112 var isRef = require('ssb-ref')
1213
14+var pb = require('private-box')
15+
1316 //UTILS
1417
1518 function clone (obj) {
1619 var _obj = {}
@@ -305,6 +308,26 @@
305308 // ts: Date.now(),
306309 // public: keys.public
307310 // })
308311 //}
309-//
310312
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.jsonView
@@ -1,8 +1,8 @@
11 {
22 "name": "ssb-keys",
33 "description": "keyfile operations for ssb",
4- "version": "2.0.0",
4+ "version": "2.0.1",
55 "homepage": "https://github.com/ssbc/ssb-keys",
66 "repository": {
77 "type": "git",
88 "url": "git://github.com/ssbc/ssb-crypto.git"
@@ -13,13 +13,14 @@
1313 "eccjs": "git://github.com/dominictarr/eccjs.git#586f6d47507184a2efe84684ed0a30605cbc43a5",
1414 "hmac": "~1.0.1",
1515 "libsodium-wrappers": "^0.2.8",
1616 "mkdirp": "~0.5.0",
17- "sodium": "^1.0.17",
17+ "private-box": "0.0.0",
18+ "sodium": "git://github.com/dominictarr/node-sodium.git",
1819 "ssb-ref": "~0.0.0"
1920 },
2021 "devDependencies": {
21- "tape": "~3.0.0"
22+ "tape": "^3.0.3"
2223 },
2324 "browser": {
2425 "./sodium": "./browser-sodium"
2526 },
@@ -27,5 +28,5 @@
2728 "test": "set -e; for t in test/*.js; do node $t; done"
2829 },
2930 "author": "Paul Frazee <pfrazee@gmail.com>",
3031 "license": "MIT"
31-}
32+}
test/box-unbox.jsView
@@ -1,0 +1,16 @@
1+
2+
3+var tape = require('tape')
4+var ssbkeys = require('../')
5+
6+tape('box, unbox', function (t) {
7+
8+ var alice = ssbkeys.generate()
9+ var bob = ssbkeys.generate()
10+
11+ var boxed = ssbkeys.box({okay: true}, [bob.public, alice.public])
12+ console.log('boxed')
13+ var msg = ssbkeys.unbox(boxed, alice.private)
14+ t.deepEqual(msg, {okay: true})
15+ t.end()
16+})

Built with git-ssb-web