Files: 4e5895551993e8c4236b5e05cb6196315a19e2cb / util.js
937 bytesRaw
1 | //attempt to load node version. |
2 | u = require('./_util') |
3 | if(u.parse) return module.exports = u, console.log("loaded node version for testing") |
4 | |
5 | var decoder = new TextDecoder('utf8') |
6 | var encoder = new TextEncoder('utf8') |
7 | var u = exports |
8 | |
9 | u.toUtf8 = function (data) { |
10 | return 'string' == typeof data ? data : decoder.decode(data) |
11 | } |
12 | |
13 | u.toBuffer = function (data) { |
14 | return 'string' !== typeof data |
15 | ? data |
16 | : new Uint8Array(encoder.encode(data)) |
17 | } |
18 | |
19 | u.toBase64 = require('arraybuffer-base64') |
20 | |
21 | u.hash = function (data, cb) { |
22 | window.crypto.subtle.digest( |
23 | { name: "SHA-256" }, |
24 | u.toBuffer(data) |
25 | ) |
26 | .then(function(hash){ |
27 | return cb(null, exports.toBase64(new Uint8Array(hash))) |
28 | }) |
29 | .catch(function(err){ |
30 | cb(err) |
31 | }) |
32 | } |
33 | |
34 | u.parse = function (str) { |
35 | try { |
36 | return JSON.parse(str) |
37 | } catch (_) { } |
38 | } |
39 | |
40 | u.HashError = function (_id, id) { |
41 | return new Error('incorrect hash:'+_id+'\n expected:'+id) |
42 | } |
43 | |
44 | |
45 | |
46 | |
47 | |
48 | |
49 | |
50 | |
51 | |
52 | |
53 | |
54 | |
55 | |
56 |
Built with git-ssb-web