<commit_before>function subenc() {
  var promiseKey = crypto.subtle.generateKey({name: "AES-GCM", length: 256}, true, ['encrypt']);
  console.log(promiseKey);
  promiseKey.then(function(value) {
    console.log(value);
    encryptSubmit(value)
  });
}

function encryptSubmit(key) {
    // create key and iv
    var forme = document.getElementById('submission');
    // storing initialization vector as 'nonce'. yeah I know
    var pasteToEncrypt = forme.elements["pasteText"].value;
    var pasteUtf = new TextEncoder().encode(pasteToEncrypt);
    var iv = crypto.getRandomValues(new Uint8Array(12));
    console.log(iv);
    var nonce = JSON.stringify(iv);
    document.getElementById('nonce').value = nonce;
    var algorithm = { name: 'AES-GCM', iv: iv};
    // var encrypted = crypto.subtle.encrypt(algorithm, key, pasteUtf);
    var jdata = {"nonce": iv};// json data that will be used in ajax
    // encrypting the text
    crypto.subtle.encrypt(algorithm, key, pasteUtf).then(function(res) {
      jdata.pasteText = new Uint8Array(res);
      console.log(jdata.pasteText);
      console.log(res);
      crypto.subtle.exportKey('jwk', key).then(function(x){
        console.log(x);
        //decrypt(jdata.pasteText, x.k, jdata.nonce);
        sendPaste(jdata, x.k);
      });
    });


}

function sendPaste(jdata, key) {
  console.log(JSON.stringify(jdata));
  var forme = document.getElementById('submission');
    // inserts crsf token into headers of ajax request
    var csrf_token = forme.elements["csrf_token"].value;
    $.ajaxSetup({
       beforeSend: function(xhr, settings) {
           if (!/^(GET|HEAD|OPTIONS|TRACE)$/i.test(settings.type) && !this.crossDomain) {
               xhr.setRequestHeader("X-CSRFToken", csrf_token);
           }
       }
    });
    $.ajax({
    url: "/submit",
    type: "POST",
    dataType : 'json',
    contentType: "application/json",
    data: JSON.stringify(jdata),
    success: function (response) {
        console.log("We did it");
        console.log(response);
        $("#text").prop("disabled", true);
        $("#text").prop("rows", '')
        link = document.getElementById('link');
        link.innerHTML = window.location.href+response['id']+"#"+escape(key);
        link.href = response['id']+"#"+key;
        link.style.display = "block";
    },
    error: function (result) {
        console.log(result);
        console.log("☹");
    }
    });
    // testenc(encrypted, key, iv);<commit_msg>Fixed anchor text after a submitted clone paste<commit_after>function subenc() {
  var promiseKey = crypto.subtle.generateKey({name: "AES-GCM", length: 256}, true, ['encrypt']);
  console.log(promiseKey);
  promiseKey.then(function(value) {
    console.log(value);
    encryptSubmit(value)
  });
}

function encryptSubmit(key) {
    // create key and iv
    var forme = document.getElementById('submission');
    // storing initialization vector as 'nonce'. yeah I know
    var pasteToEncrypt = forme.elements["pasteText"].value;
    var pasteUtf = new TextEncoder().encode(pasteToEncrypt);
    var iv = crypto.getRandomValues(new Uint8Array(12));
    console.log(iv);
    var nonce = JSON.stringify(iv);
    document.getElementById('nonce').value = nonce;
    var algorithm = { name: 'AES-GCM', iv: iv};
    // var encrypted = crypto.subtle.encrypt(algorithm, key, pasteUtf);
    var jdata = {"nonce": iv};// json data that will be used in ajax
    // encrypting the text
    crypto.subtle.encrypt(algorithm, key, pasteUtf).then(function(res) {
      jdata.pasteText = new Uint8Array(res);
      console.log(jdata.pasteText);
      console.log(res);
      crypto.subtle.exportKey('jwk', key).then(function(x){
        console.log(x);
        //decrypt(jdata.pasteText, x.k, jdata.nonce);
        sendPaste(jdata, x.k);
      });
    });


}

function sendPaste(jdata, key) {
  console.log(JSON.stringify(jdata));
  var forme = document.getElementById('submission');
    // inserts crsf token into headers of ajax request
    var csrf_token = forme.elements["csrf_token"].value;
    $.ajaxSetup({
       beforeSend: function(xhr, settings) {
           if (!/^(GET|HEAD|OPTIONS|TRACE)$/i.test(settings.type) && !this.crossDomain) {
               xhr.setRequestHeader("X-CSRFToken", csrf_token);
           }
       }
    });
    $.ajax({
    url: "/submit",
    type: "POST",
    dataType : 'json',
    contentType: "application/json",
    data: JSON.stringify(jdata),
    success: function (response) {
        console.log("We did it");
        console.log(response);
        $("#text").prop("disabled", true);
        $("#text").prop("rows", '')
        link = document.getElementById('link');
        link.href = response['id']+"#"+key;
        link.innerHTML = link.href;
        link.style.display = "block";
    },
    error: function (result) {
        console.log(result);
        console.log("☹");
    }
    });
    // testenc(encrypted, key, iv);
{'fraction_non_alphanumeric': 0.16151970743600164, 'fraction_numerical': 0.0028443722064201544, 'mean_word_length': 2.9927007299270074, 'pattern_counts': {'":': 2, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2118012', 'n_tokens_mistral': 1583, 'n_tokens_neox': 1558, 'n_words': 314}