-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathhardbin.js
58 lines (51 loc) · 1.47 KB
/
hardbin.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
/* data encryption + storage functions for hardbin
* make sure to also load jquery + aes.js
*/
function encrypt(data, key) {
return CryptoJS.AES.encrypt(data, key).toString();
}
function decrypt(data, key) {
return CryptoJS.AES.decrypt(data, key).toString(CryptoJS.enc.Utf8);
}
function generate_key() {
var alphabet = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
var randombytes = new Uint8Array(32);
crypto.getRandomValues(randombytes);
var k = '';
for (var i = 0; i < 32; i++) {
var idx = Math.floor(randombytes[i] * alphabet.length / 256);
k += alphabet.charAt(idx);
}
return k;
}
function is_local_gateway() {
return window.location.hostname == 'localhost';
}
function write(content, cb) {
$.ajax({
url: "content",
type: "DELETE",
success: function(data, status, xhr) {
$.ajax({
url: "/ipfs/" + xhr.getResponseHeader('Ipfs-Hash') + "/content",
type: "PUT",
data: content,
success: function(data, status, xhr) {
cb(xhr.getResponseHeader('Ipfs-Hash'));
},
timeout: function() {
cb("");
},
error: function() {
cb("");
},
});
},
timeout: function() {
cb("");
},
error: function() {
cb("");
},
});
}