Files: 8a12215fcc9750aca9870712c5fcf43509b8fa54 / util.js
1349 bytesRaw
1 | var cl = require('chloride') |
2 | |
3 | //var rx = require('is-canonical-base64')(null, null, 32) |
4 | exports.isUnboxKey = function (s) { |
5 | return s === Buffer.from(s, 'base64').toString('base64') |
6 | // return rx.test(s) |
7 | } |
8 | exports.id2Buffer = function (id) { |
9 | return Buffer.from(id.substring(1, id.indexOf('.')), 'base64') |
10 | } |
11 | |
12 | exports.isBox2 = function (ctxt) { |
13 | //we can just do a fairly lax check here, don't check the content |
14 | //is canonical base64, because that check has already been done. |
15 | return 'string' == typeof ctxt && /\.box2$/.test(ctxt) |
16 | } |
17 | |
18 | exports.isCurvePair = function (keys) { |
19 | return keys.public && keys.private |
20 | } |
21 | |
22 | exports.ctxt2Buffer = function (ctxt) { |
23 | return exports.isBox2(ctxt) && Buffer.from(ctxt.substring(0, ctxt.indexOf('.')), 'base64') |
24 | } |
25 | |
26 | function toBuffer(b) { |
27 | return Buffer.isBuffer(b) ? b : Buffer.from(b, 'base64') |
28 | } |
29 | |
30 | exports.hmac = function (a, b) { |
31 | return cl.crypto_auth(toBuffer(a), toBuffer(b)) |
32 | } |
33 | |
34 | exports.getGroupMsgKey = function (previous, group) { |
35 | //or would it be better to use generic hash (with key?) |
36 | return exports.hmac(Buffer.concat([previous, exports.id2Buffer(group.id)]), Buffer.from(group.unbox, 'base64')) |
37 | } |
38 | |
39 | exports.scalarmultKeys = function (a_key, recps) { |
40 | return recps.map(function (curve) { |
41 | return cl.crypto_scalarmult( |
42 | toBuffer(curve.private), |
43 | toBuffer(a_key) |
44 | ) |
45 | }) |
46 | } |
47 | |
48 | |
49 | |
50 | |
51 |
Built with git-ssb-web