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