Files: ef1a2eaa163709173d29cc9f3fc446695e466fe7 / _util.js
639 bytesRaw
1 | |
2 | var u = exports |
3 | |
4 | u.toUtf8 = function (data) { |
5 | return 'string' == typeof data ? data : new Buffer(data).toString('utf8') |
6 | } |
7 | |
8 | u.toBuffer = function (data) { |
9 | return 'string' !== typeof data |
10 | ? data |
11 | : new Buffer(data) |
12 | } |
13 | |
14 | u.toBase64 = require('arraybuffer-base64') |
15 | |
16 | var crypto = require('crypto') |
17 | |
18 | u.hash = function (data, cb) { |
19 | setImmediate(function () { |
20 | cb(null, |
21 | crypto.createHash('sha256').update(data).digest('base64') |
22 | ) |
23 | }) |
24 | } |
25 | |
26 | u.parse = function (str) { |
27 | try { |
28 | return JSON.parse(str) |
29 | } catch (_) { } |
30 | } |
31 | |
32 | u.HashError = function (_id, id) { |
33 | return new Error('incorrect hash:'+_id+'\n expected:'+id) |
34 | } |
35 | |
36 | |
37 | |
38 | |
39 |
Built with git-ssb-web