Files
win32.run/static/js/libarchive.js/test/files/encryption.html
2023-02-13 19:32:10 +07:00

80 lines
2.8 KiB
HTML

<!DOCTYPE html>
<html>
<head>
<title>test webworker</title>
</head>
<body>
<input type="file" id="file" />
<script type="module" >
function hex(buffer) {
const hexCodes = [];
const view = new DataView(buffer);
for (let i = 0; i < view.byteLength; i += 4) {
const value = view.getUint32(i)
const stringValue = value.toString(16)
const padding = '00000000'
const paddedValue = (padding + stringValue).slice(-padding.length)
hexCodes.push(paddedValue);
}
return hexCodes.join("");
}
function getChecksum(file){
return new Promise((resolve,reject) => {
try{
const reader = new FileReader();
reader.onload = function() {
crypto.subtle.digest("SHA-256", reader.result).then(function (hash) {
resolve(hex(hash));
});
}
reader.readAsArrayBuffer(file);
}catch(err){
reject(err);
}
});
}
function finish(){
const d = document.createElement('div');
d.setAttribute('id','done');
d.textContent = 'Done.';
document.body.appendChild(d);
}
async function fileChecksums(obj){
for( const [key,val] of Object.entries(obj) ){
obj[key] = val instanceof File ?
await getChecksum(val) : await fileChecksums(val);
}
return obj;
}
import {Archive} from '../../src/libarchive.js';
Archive.init({
workerUrl: '../../dist/worker-bundle.js'
});
window.Archive = Archive;
document.getElementById('file').addEventListener('change', async (e) => {
let obj = null, encEntries = false;
try{
const file = e.currentTarget.files[0];
const archive = await Archive.open(file);
encEntries = await archive.hasEncryptedData();
await archive.usePassword("nika");
obj = await archive.extractFiles();
obj = await fileChecksums(obj);
}catch(err){
console.error(err);
}finally{
window.obj = {files: obj, encrypted: encEntries};
finish();
}
});
</script>
</body>
</html>