<commit_before>//lib for creating a shared secret and RSA pub/pri keys
// The length of the RSA key, in bits.
var bitlength = 1024;
// default to 256-bit aes key
var aesBitLength = 256;

//generate a random passphrase
//used to generate passphrases
function randString(length) {
    length = length ? length : aesBitLength / 8;
    randBuf = new Uint8Array(length);
    window.crypto.getRandomValues(randBuf);
    return arrayBufferToString(randBuf);
}

function arrayBufferToString(buf) {
    /*
    from http://updates.html5rocks.com/2012/06/How-to-convert-ArrayBuffer-to-and-from-String
    */
    return String.fromCharCode.apply(null, new Uint16Array(buf));
}

//generates and returns a public/private key pair
//for a user
function genKeys() {
    var pri_key = cryptico.generateRSAKey(randString(), bitlength);
    var pub_key = cryptico.publicKeyString(pri_key);
    return {
        "pri_key": pri_key,
        "pub_key": pub_key,
    }
}

//convert json_parsed_key into an RSA object
//returns deserlized RSA obj
function deserializePriKey(json_parsed_key) {
    var pri_key = new RSAKey();
    for (k1 in json_parsed_key) {
        var nbi = new BigInteger();
        var val = json_parsed_key[k1];
        for (k2 in val) {
            nbi[k2] = val[k2];
        }
        pri_key[k1] = nbi;
    }
    return pri_key
}

// creates a pub key and a pri key for user, and encrypts the pri key
// with the inputted password
// and then stores all 3 to local storage
// registration stores pub/pri/encrypted_pri_key which have not been uploaded yet
function userInitialize(username, password) {
    // debugger;
    var key_pair = genKeys();
    var encrypted_pri_key = encryptPriKey(key_pair.pri_key, password);
    var registration = {
        "pri_key": key_pair.pri_key,
        "pub_key": key_pair.pub_key,
        "encrypted_pri_key": encrypted_pri_key,
        "username": username,
        "completed": false
    };
    writeLocalStorage("registration", registration);
    return registration;
}

// returns a password encrypted version of a private key
function encryptPriKey(pri_key, password) {
    return encryptAES(JSON.stringify(pri_key), password);
}


// if there is non-completed user data in registration in local storage,
// then it uploads this data to the server, and sets registration to completed
function uploadUserData() {
    var registration_data = loadLocalStore("registration");
    if (!("completed" in registration_data)) {
        // they have accessed an initialize page from a different computer, or cleared local storage
        // therefore we do not have their password
        // prompt them to login
        window.location.href = "/login/";
        return
    } else if (registration_data["completed"]) {
        // TODO: we have a problem, they have already uploaded their shit,
        // ... maybe not a problem, but would like to be warned
        return
    } else {
        var pri_key = registration_data["pri_key"]; // TODO stringify pri_key
        var pub_key = registration_data["pub_key"];
        var encrypted_pri_key = registration_data["encrypted_pri_key"];
        var username = registration_data["username"];
        var success = uploadPubKey(username, pub_key);
        var success2 = uploadPriKey(username, encrypted_pri_key);
        // TODO: check if both uploads were successful
        var userMeta = {
            "username": username,
            "pri_key": pri_key,
            "defaultEncrypt": true
        };
        writeLocalStorage("userMeta", userMeta);
        registration_data["completed"] = true;
        writeLocalStorage("registration", registration_data);
    }
}

// logout, clears localstorage !!!
function parseltongueLogout() {
    localStorage.clear();
    localStorage.first = "true";
}<commit_msg>turn default encrypt to off on first install<commit_after>//lib for creating a shared secret and RSA pub/pri keys
// The length of the RSA key, in bits.
var bitlength = 1024;
// default to 256-bit aes key
var aesBitLength = 256;

//generate a random passphrase
//used to generate passphrases
function randString(length) {
    length = length ? length : aesBitLength / 8;
    randBuf = new Uint8Array(length);
    window.crypto.getRandomValues(randBuf);
    return arrayBufferToString(randBuf);
}

function arrayBufferToString(buf) {
    /*
    from http://updates.html5rocks.com/2012/06/How-to-convert-ArrayBuffer-to-and-from-String
    */
    return String.fromCharCode.apply(null, new Uint16Array(buf));
}

//generates and returns a public/private key pair
//for a user
function genKeys() {
    var pri_key = cryptico.generateRSAKey(randString(), bitlength);
    var pub_key = cryptico.publicKeyString(pri_key);
    return {
        "pri_key": pri_key,
        "pub_key": pub_key,
    }
}

//convert json_parsed_key into an RSA object
//returns deserlized RSA obj
function deserializePriKey(json_parsed_key) {
    var pri_key = new RSAKey();
    for (k1 in json_parsed_key) {
        var nbi = new BigInteger();
        var val = json_parsed_key[k1];
        for (k2 in val) {
            nbi[k2] = val[k2];
        }
        pri_key[k1] = nbi;
    }
    return pri_key
}

// creates a pub key and a pri key for user, and encrypts the pri key
// with the inputted password
// and then stores all 3 to local storage
// registration stores pub/pri/encrypted_pri_key which have not been uploaded yet
function userInitialize(username, password) {
    // debugger;
    var key_pair = genKeys();
    var encrypted_pri_key = encryptPriKey(key_pair.pri_key, password);
    var registration = {
        "pri_key": key_pair.pri_key,
        "pub_key": key_pair.pub_key,
        "encrypted_pri_key": encrypted_pri_key,
        "username": username,
        "completed": false
    };
    writeLocalStorage("registration", registration);
    return registration;
}

// returns a password encrypted version of a private key
function encryptPriKey(pri_key, password) {
    return encryptAES(JSON.stringify(pri_key), password);
}


// if there is non-completed user data in registration in local storage,
// then it uploads this data to the server, and sets registration to completed
function uploadUserData() {
    var registration_data = loadLocalStore("registration");
    if (!("completed" in registration_data)) {
        // they have accessed an initialize page from a different computer, or cleared local storage
        // therefore we do not have their password
        // prompt them to login
        window.location.href = "/login/";
        return
    } else if (registration_data["completed"]) {
        // TODO: we have a problem, they have already uploaded their shit,
        // ... maybe not a problem, but would like to be warned
        return
    } else {
        var pri_key = registration_data["pri_key"]; // TODO stringify pri_key
        var pub_key = registration_data["pub_key"];
        var encrypted_pri_key = registration_data["encrypted_pri_key"];
        var username = registration_data["username"];
        var success = uploadPubKey(username, pub_key);
        var success2 = uploadPriKey(username, encrypted_pri_key);
        // TODO: check if both uploads were successful
        var userMeta = {
            "username": username,
            "pri_key": pri_key,
            "defaultEncrypt": false 
        };
        writeLocalStorage("userMeta", userMeta);
        registration_data["completed"] = true;
        writeLocalStorage("registration", registration_data);
    }
}

// logout, clears localstorage !!!
function parseltongueLogout() {
    localStorage.clear();
    localStorage.first = "true";
}
{'fraction_non_alphanumeric': 0.09736255084634562, 'fraction_numerical': 0.0076105497966146175, 'mean_word_length': 3.00525486074619, 'pattern_counts': {'":': 20, '<': 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': '4905412', 'n_tokens_mistral': 2294, 'n_tokens_neox': 2170, 'n_words': 679}