aboutsummaryrefslogtreecommitdiffhomepage
path: root/File-decryption/webapp/assets/js/main.js
blob: 6d7c71fbf91d25ea2a247c11f408b1cd7f1b2c9c (plain)
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
let decryptedBlobUrl = null;
let currentFileName = null;
let currentClearText = null;
let decodedProtobuf = null;

document.addEventListener("DOMContentLoaded", function () {
    // handle file uploads
    let fileInput = document.getElementById('file-upload');
    let fileInputLabel = document.getElementById('file-upload-label');
    let fileDownloadBtn = document.getElementById('file-download');
    let decryptToggle = document.getElementById('decrypt-toggle');
    let serialForm = document.getElementById('serial-form');
    var jsonRenderer = document.querySelector('.target');
    var liveDecoder = document.getElementById('live-decoder');
    let acknowledgement = document.querySelector('.ethics-message-ack');


    // Don't display the download button until a file is selected
    fileDownloadBtn.style.display = 'none';
    serialForm.style.display = 'none';
    liveDecoder.style.display = 'none';
    fileInput.disabled = true;

    // Etics message
    acknowledgement.addEventListener('click', function () {
        document.querySelector('.ethics-message').style.display = 'none';
        // Enable the file input
        console.log(fileInput)
        fileInput.disabled = false;
        fileInputLabel.classList.remove('upload-disabled');
    });

    fileInput.onchange = () => {
        const reader = new FileReader()
        reader.addEventListener('load', async (e) => {
            currentFileName = e.target.fileName;

            const serial = document.getElementById('serial-input').value;

            let main_key = null;
            if (serial.length > 0) {
                // local decryption, use master key + serial
                main_key = new Uint8Array([...MASTER_KEY, ...new TextEncoder("utf-8").encode(serial)]);
            } else {
                // global decryption, use master key only
                main_key = MASTER_KEY;
            }

            console.log("Decrypting file: " + currentFileName + " with key: " + main_key + " and serial: " + serial);
            let blob = await decrypt(e.target.result, main_key);
            console.log(blob);
            decryptedBlobUrl = window.URL.createObjectURL(blob);
            
            // If the filename ends with .cns or pb, we can assume it's a protobuf file
            if(currentFileName.endsWith('.cns') || currentFileName.endsWith('.pb')){
                // Enable live decoding
                liveDecoder.style.display = 'block';
            }
        });

        for (let file of fileInput.files) {
            // https://stackoverflow.com/questions/24245105/how-to-get-the-filename-from-the-javascript-filereader
            reader.fileName = file.name;
            reader.readAsArrayBuffer(file);
        }
        if(fileInput.files.length > 0){
            fileDownloadBtn.style.display = 'flex';
        }else{
            fileDownloadBtn.style.display = 'none';
        }
    };

    fileDownloadBtn.onclick = () => {
        const link = document.createElement('a');
        link.href = decryptedBlobUrl
        link.download = currentFileName + '.dec';
        link.click();
    };

    decryptToggle.onchange = () => {
        if (decryptToggle.checked) {
            serialForm.style.display = 'block';
        } else {
            serialForm.style.display = 'none';
        }
    };

    document.getElementById('protobuf-list').onchange = (e)=>{
        processFileDecode(currentClearText);
        console.log(decodedProtobuf);

        // Decode protobuf is a class, so we need to convert it to a JSON object
        var json = JSON.parse(JSON.stringify(decodedProtobuf));

        jsonRenderer.innerHTML = jsonViewer(json, true);
    } 
});

var protoPaths = ["assets/proto/Capture.proto","assets/proto/Preset.proto"]
var protoTypes = []

function loadTypesFromRoot(current) {
    if (current instanceof protobuf.Type){
        protoTypes.push(current);
        
        document.getElementById('protobuf-list').add(new Option(current.name, current.name))
    }

    if (current.nestedArray){
        current.nestedArray.forEach(function(nested) {
            loadTypesFromRoot(nested);
        });
    }
}

/* ProtoBuf */
protoPaths.forEach((protoPath)=>{
    protobuf.load(protoPath).then((root)=>{
        loadTypesFromRoot(root)
    })
})