-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsasha.html
201 lines (172 loc) · 5.93 KB
/
sasha.html
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
<html>
<head>
<meta charset="utf-8">
<style>
body,html{display:flex;align-items:center;justify-content:center;height:100%;font-family:sans-serif}.box{background-color:#ccc;text-align:center;margin:4px;padding:30px}input[type="button"]{margin:4px}
</style>
<script>
var DATA_STRING_HEX = '';
var DATA_IV = '';
var DATA_TYPE = '';
</script>
</head>
<body>
<div class="box">
<h1>( ͡° ͜ʖ ͡°)</h1>
<h2>New file</h2>
<div>
<p>Set password for new encrypted file:</p>
<input id="password-new" type="password"></div>
<p>Than select the files:</p>
<input type="file" id="fileRead" onChange="sashaIn(this);" multiple>
</div>
</div>
<div id="thisData" class="box">
<h1>( ͡° ͜ʖ ͡°)</h1>
<h2>Open this file</h2>
<div>
<p>Password:</p>
<input id="password-file" type="password">
</div>
<input type="button" onClick="sashaOut()" value="View">
</div>
<script>
'use strict';
if (!window.File && !window.FileReader && !window.FileList && !window.Blob) {
alert('Erro: Your browser does not suport Sasha.');
}
DATA_STRING_HEX=='' ? document.getElementById('thisData').hidden = true : document.getElementById('thisData').hidden = false
//Main functions
const sashaIn = (element) => {
Promise.resolve(getFile(element))
.then(encriptFile)
.then(createVaultFile)
.then(downloadVaultFile)
.catch(e => {console.log(e)})
}
const sashaOut = () => {
Promise.resolve({data: window.DATA_STRING_HEX, iv: window.DATA_IV, type: window.DATA_TYPE})
.then(decriptFile)
.catch(e => {
throw alert("Incorrect password.");
})
.then(downloadFile)
.catch(e => {console.log(e)})
}
//Major functions
const getFile = (element) => {
return new Promise((resolve, reject) => {
let fr = new FileReader()
fr.onloadend = () => {
resolve({filedata: fr.result, type: element.files[0].type})
}
fr.readAsArrayBuffer(element.files[0])
})
}
const encriptFile = async (file) => {
const password = document.getElementById('password-new').value;
const iv = util.getRandomBytes(12);
const alg = {name: 'AES-GCM', iv: iv};
const passwordHash = await util.sha256(util.textEncoder(password));
const key = await util.importKey('raw', passwordHash, alg, false, ['encrypt']);
const encriptedData = await util.encrypt(alg, key, file.filedata);
return { data: util.hex2base64(util.buf2hex(encriptedData)), iv: util.hex2base64(util.buf2hex(iv)), type: file.type}
}
const decriptFile = async (data) => {
const password = document.getElementById('password-file').value;
const alg = {name: 'AES-GCM', iv: util.hex2buf(util.base642hex(data.iv))};
const passwordHash = await util.sha256(util.textEncoder(password));
const key = await util.importKey('raw', passwordHash, alg, false, ['decrypt']);
const alter_data = util.hex2buf(util.base642hex(data.data))
const decryptedData = await util.decrypt(alg, key, alter_data);
return decryptedData
}
const createVaultFile = (encriptedData) => {
let vault = document.getElementsByTagName('html')[0].innerHTML;
let vaultWithData = vault
.replace(/var DATA_STRING_HEX = '.*'/, `var DATA_STRING_HEX = '${encriptedData.data}'` )
.replace(/var DATA_IV = '.*'/, `var DATA_IV = '${encriptedData.iv}'`)
.replace(/var DATA_TYPE = '.*'/, `var DATA_TYPE = '${encriptedData.type}'`)
return vaultWithData
}
const downloadVaultFile = (data) =>{
var elem = window.document.createElement('a');
elem.href = 'data:/html;charset=utf-8,' + encodeURIComponent(data);
elem.download = 'EncryptedFile.html';
document.body.appendChild(elem);
elem.click();
document.body.removeChild(elem);
}
const downloadFile = (decryptedData) => {
var elem = window.document.createElement('a');
let blob = new Blob( [decryptedData], { type: window.DATA_TYPE });
elem.href = window.URL.createObjectURL(blob);
document.body.appendChild(elem);
elem.click();
document.body.removeChild(elem);
}
//Ultility functions
var util = {}
util.getRandomBytes = (bytes) => {
return crypto.getRandomValues(new Uint8Array(bytes));
}
util.sha256 = async (data) => {
return await crypto.subtle.digest({name:"SHA-256"}, data)
}
util.importKey = async(type, key, algo , isExtractable, applications) => {
return await crypto.subtle.importKey(type, key, algo , isExtractable, applications)
}
util.encrypt = async (algo, password , data) => {
return await crypto.subtle.encrypt(algo, password, data)
}
util.decrypt = async (algo, password , data) => {
return await crypto.subtle.decrypt(algo, password, data)
}
util.textEncoder = (string) => {
return new TextEncoder("utf-8").encode(string)
}
util.textDecoder = (ArrayBuffer) => {
return new TextDecoder("utf-8").decode(ArrayBuffer)
}
util.buf2hex = (arrayBuffer) => {
if (typeof arrayBuffer !== 'object' || arrayBuffer === null || typeof arrayBuffer.byteLength !== 'number') {
throw new TypeError('Expected input to be an ArrayBuffer')
}
var view = new Uint8Array(arrayBuffer)
var result = ''
var value
for (var i = 0; i < view.length; i++) {
value = view[i].toString(16)
result += (value.length === 1 ? '0' + value : value)
}
return result
}
util.hex2buf = (hex) => {
if (typeof hex !== 'string') {
throw new TypeError('Expected input to be a string')
}
if ((hex.length % 2) !== 0) {
throw new RangeError('Expected string to be an even number of characters')
}
var view = new Uint8Array(hex.length / 2)
for (var i = 0; i < hex.length; i += 2) {
view[i / 2] = parseInt(hex.substring(i, i + 2), 16)
}
return view.buffer
}
util.hex2base64 = (hex) => {
return btoa(String.fromCharCode.apply(null,
hex.replace(/\r|\n/g, "").replace(/([\da-fA-F]{2}) ?/g, "0x$1 ").replace(/ +$/, "").split(" "))
);
}
util.base642hex = (base64) => {
for (var i = 0, bin = atob(base64.replace(/[ \r\n]+$/, "")), hex = []; i < bin.length; ++i) {
let tmp = bin.charCodeAt(i).toString(16);
if (tmp.length === 1) tmp = "0" + tmp;
hex[hex.length] = tmp;
}
return hex.join(" ");
}
</script>
</body>
</html>