git ssb

1+

Dominic / ssb-keys



Tree: ceaabdc05ea7cb746df59f258d3ae7885b2fa9f3

Files: ceaabdc05ea7cb746df59f258d3ae7885b2fa9f3 / util.js

1569 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
38//crazy hack to make electron not crash
39function base64ToBuffer(s) {
40 var l = s.length * 6 / 8
41 if(s[s.length - 2] == '=')
42 l = l - 2
43 else
44 if(s[s.length - 1] == '=')
45 l = l - 1
46
47 var b = new Buffer(l)
48 b.write(s, 'base64')
49 return b
50}
51
52exports.toBuffer = function (buf) {
53 if(buf == null) return buf
54 if(Buffer.isBuffer(buf)) throw new Error('already a buffer')
55 var i = buf.indexOf('.')
56 var start = (exports.hasSigil(buf)) ? 1 : 0
57 return new Buffer(buf.substring(start, ~i ? i : buf.length), 'base64')
58// return base64ToBuffer()
59}
60
61//function toUint8(buf) {
62// return new Uint8Array(toBuffer(buf))
63//}
64
65
66
67
68

Built with git-ssb-web