git ssb

0+

dangerousbeans / web-bootloader



forked from Dominic / web-bootloader

Tree: 4e5895551993e8c4236b5e05cb6196315a19e2cb

Files: 4e5895551993e8c4236b5e05cb6196315a19e2cb / util.js

937 bytesRaw
1//attempt to load node version.
2u = require('./_util')
3if(u.parse) return module.exports = u, console.log("loaded node version for testing")
4
5var decoder = new TextDecoder('utf8')
6var encoder = new TextEncoder('utf8')
7var u = exports
8
9u.toUtf8 = function (data) {
10 return 'string' == typeof data ? data : decoder.decode(data)
11}
12
13u.toBuffer = function (data) {
14 return 'string' !== typeof data
15 ? data
16 : new Uint8Array(encoder.encode(data))
17}
18
19u.toBase64 = require('arraybuffer-base64')
20
21u.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
34u.parse = function (str) {
35 try {
36 return JSON.parse(str)
37 } catch (_) { }
38}
39
40u.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