Files: 89171dfc684b68a1ec0741e78b5c882a9e2be86a / util.js
1214 bytesRaw
1 | var cl = require('chloride') |
2 | |
3 | exports.hash = function (data, enc) { |
4 | data = ( |
5 | 'string' === typeof data && enc == null |
6 | ? new Buffer(data, 'binary') |
7 | : new Buffer(data, enc) |
8 | ) |
9 | return cl.crypto_hash_sha256(data).toString('base64')+'.sha256' |
10 | } |
11 | |
12 | exports.hasSigil = function hasSigil (s) { |
13 | return /^(@|%|&)/.test(s) |
14 | } |
15 | |
16 | function tag (key, tag) { |
17 | if(!tag) throw new Error('no tag for:' + key.toString('base64')) |
18 | return key.toString('base64')+'.' + tag.replace(/^\./, '') |
19 | } |
20 | |
21 | exports.keysToJSON = function keysToJSON(keys, curve) { |
22 | curve = (keys.curve || curve) |
23 | |
24 | var pub = tag(keys.public.toString('base64'), curve) |
25 | return { |
26 | curve: curve, |
27 | public: pub, |
28 | private: keys.private ? tag(keys.private.toString('base64'), curve) : undefined, |
29 | id: '@'+(curve === 'ed25519' ? pub : exports.hash(pub)) |
30 | } |
31 | } |
32 | |
33 | exports.getTag = function getTag (string) { |
34 | var i = string.indexOf('.') |
35 | return string.substring(i+1) |
36 | } |
37 | |
38 | exports.toBuffer = function (buf) { |
39 | if(buf == null) return buf |
40 | if(Buffer.isBuffer(buf)) throw new Error('already a buffer') |
41 | var i = buf.indexOf('.') |
42 | var start = (exports.hasSigil(buf)) ? 1 : 0 |
43 | return new Buffer(buf.substring(start, ~i ? i : buf.length), 'base64') |
44 | } |
45 |
Built with git-ssb-web