Files: 999de37f163f57a7d9c3dd5d1b311434d7bc199b / util.js
679 bytesRaw
1 | var chloride = require('chloride') |
2 | |
3 | function box (data, key) { |
4 | if(!data) return |
5 | var b = new Buffer(JSON.stringify(data)) |
6 | return chloride.crypto_secretbox_easy(b, key.slice(0, 24), key).toString('base64') |
7 | } |
8 | |
9 | function unbox (ctxt, key) { |
10 | //var b = new Buffer(JSON.stringify(data)) |
11 | var b = new Buffer(ctxt, 'base64') |
12 | var ptxt = chloride.crypto_secretbox_open_easy(b, key.slice(0, 24), key) |
13 | if(!ptxt) return |
14 | try { |
15 | return JSON.parse(ptxt) |
16 | } catch(err) { |
17 | console.error(err) |
18 | } |
19 | } |
20 | |
21 | |
22 | function hash(s) { |
23 | return chloride.crypto_hash_sha256( |
24 | 'string' == typeof s ? new Buffer(s, 'utf8') : s |
25 | ) |
26 | } |
27 | |
28 | exports.hash = hash |
29 | exports.box = box |
30 | exports.unbox = unbox |
31 |
Built with git-ssb-web