Files: 1762b8cc75ef682312486c7dc93f23eb3b02c59b / util.js
1253 bytesRaw
1 | var chloride = require('chloride') |
2 | |
3 | exports.box = function box (data, key) { |
4 | if(!data) return |
5 | var b = Buffer.from(JSON.stringify(data)) |
6 | return chloride.crypto_secretbox_easy(b, key.slice(0, 24), key).toString('base64') |
7 | } |
8 | |
9 | exports.unbox = function unbox (ctxt, key) { |
10 | var b = Buffer.from(ctxt, 'base64') |
11 | var ptxt = chloride.crypto_secretbox_open_easy(b, key.slice(0, 24), key) |
12 | if(!ptxt) return |
13 | try { |
14 | return JSON.parse(ptxt) |
15 | } catch(err) { |
16 | console.error(err) |
17 | } |
18 | } |
19 | |
20 | exports.hash = function hash (s) { |
21 | return chloride.crypto_hash_sha256( |
22 | 'string' === typeof s ? Buffer.from(s, 'utf8') : s |
23 | ) |
24 | } |
25 | |
26 | exports.parse = function parse (str) { |
27 | if(!/^inv\:/.test(str)) throw new Error('invites must start with "inv:", got '+JSON.stringify(str)) |
28 | var ary = str.substring(4).split(',') |
29 | return { |
30 | seed: ary[0], |
31 | invite: ary[1], |
32 | cap: ary[2], |
33 | pubs: ary.slice(3) |
34 | } |
35 | } |
36 | exports.stringify = function stringify (invite) { |
37 | return 'inv:'+[ |
38 | invite.seed, |
39 | invite.invite, |
40 | invite.cap || '' |
41 | ] |
42 | .concat(invite.pubs) |
43 | .join(',') |
44 | } |
45 | |
46 | exports.sort = function sort (found) { |
47 | return found.sort(function (a, b) { |
48 | return (!!b.willReplicate) - (!!a.willReplicate) || (b.availability - a.availability) |
49 | }) |
50 | } |
51 |
Built with git-ssb-web