git ssb

1+

Dominic / ssb-keys



Tree: 3140362268783cb7c3ff1d691717a484dc2d1943

Files: 3140362268783cb7c3ff1d691717a484dc2d1943 / util.js

1214 bytesRaw
1var cl = require('chloride')
2
3exports.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
12exports.hasSigil = function hasSigil (s) {
13 return /^(@|%|&)/.test(s)
14}
15
16function tag (key, tag) {
17 if(!tag) throw new Error('no tag for:' + key.toString('base64'))
18 return key.toString('base64')+'.' + tag.replace(/^\./, '')
19}
20
21exports.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
33exports.getTag = function getTag (string) {
34 var i = string.indexOf('.')
35 return string.substring(i+1)
36}
37
38exports.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