import '@/lib/sjcl.js'
import '@/lib/hmacsha1.js'
import store from '@/store'

import Util from '@/utils/util.js'

export default class Crypto {

    static setupAes() {
        // Setup key
        var combinedKey = store.state.account_id + ":" + store.state.hash + "\n"
        var key = sjcl.misc.pbkdf2(combinedKey, store.state.salt, 10000, 256, hmacSHA1)

        store.dispatch('aes', new sjcl.cipher.aes(key)); // Store aes
        sjcl.beware["CBC mode is dangerous because it doesn't protect message integrity."]();
    }

    //TODO make - not static?
    static decryptConversation (convo) {
        // Removes miliiseconds from timestamp
        convo.timestamp = convo.timestamp / 1000 >> 0; // Remove ms
        convo.timestamp = convo.timestamp * 1000; // Add back zero timestamp

        try {
            convo.title = Crypto.decrypt(convo.title);

            convo.snippet = Crypto.decrypt(convo.snippet)
            convo.snippet = Util.entityEncode(convo.snippet)
        } catch (err) {
            return null
        }

        try {
            convo.phone_numbers = Crypto.decrypt(convo.phone_numbers);
        } catch (err) {
            convo.phone_numbers = "";
        }

        return convo;
    }

    static decryptMessage (message) {
        // Removes miliiseconds from timestamp
        message.timestamp = message.timestamp / 1000 >> 0; // Remove ms
        message.timestamp = message.timestamp * 1000; // Add back zero timestamp

        // Decrypt
        try {
            message.mime_type = decrypt(message.mime_type);

            message.data = decrypt(message.data)
            message.data = entityEncode(message.data); // encode message

            message.message_from = decrypt(message.message_from);
        } catch (err) {
            return null
        }

        return message;
    }
    static decrypt (data) {

        if (data == null) 
            return "";

        var parts = data.split("-:-");
        return sjcl.codec.utf8String.fromBits(
            sjcl.mode.cbc.decrypt(store.state.aes, sjcl.codec.base64.toBits(parts[1]),
            sjcl.codec.base64.toBits(parts[0]), null));
    }

    static encrypt (data) {
        var iv = sjcl.codec.hex.toBits(random128Hex());
        var cipherbits = sjcl.mode.cbc.encrypt(
            store.state.aes, sjcl.codec.utf8String.toBits(data), iv, null
        );
        return sjcl.codec.base64.fromBits(iv) + "-:-" + sjcl.codec.base64.fromBits(cipherbits);
    }

    static encryptData (data) {
        var iv = sjcl.codec.hex.toBits(random128Hex());
        var bits = toBitArrayCodec(data);
        var cipherbits = sjcl.mode.cbc.encrypt(store.state.aes, bits, iv, null);
        return sjcl.codec.base64.fromBits(iv) + "-:-" + sjcl.codec.base64.fromBits(cipherbits);
    }
}

@@ -138,16 +138,93 @@
 rypto %7B%0A
+    /**%0A     * setupAes%0A     *%0A     * Sets up AES key and starts sjcl%0A     */
 %0A    sta
@@ -605,38 +605,179 @@
   %7D%0A
+    
 %0A    /
-/TODO make - not static?
+**%0A     * decryptConversation%0A     *%0A     * Decryptes conversation object%0A     * @param convo - conversation object%0A     * @return decrypted conversation%0A     */
 %0A   
@@ -999,32 +999,68 @@
 zero timestamp%0A%0A
+        // Handle title and snippet%0A
         try %7B%0A  
@@ -1048,32 +1048,33 @@
 et%0A        try %7B
+ 
 %0A            con
@@ -1283,32 +1283,88 @@
  null%0A        %7D%0A
+        %0A        // Handle phone number(s) if applicable
 %0A        try %7B%0A 
@@ -2141,24 +2141,193 @@
 ssage;%0A    %7D
+%0A%0A    /**%0A     * decrypt%0A     *%0A     * Decrypts encoded and encrypted string using stored aes%0A     * %0A     * @param data - string%0A     * @return decrypted string%0A     */
 %0A    static 
@@ -2621,25 +2621,199 @@
 ), null)
-);%0A    %7D%0A
+%0A        );%0A    %7D%0A    %0A    /**%0A     * encrypt%0A     * Encrypts arbitrary string data using stored aes%0A     *%0A     * @param data - arbitrary string%0A     * @return encrypted data%0A     */
 %0A    sta
@@ -3117,32 +3117,188 @@
 erbits);%0A    %7D%0A%0A
+    /**%0A     * encryptData%0A     * Encrypts arbitrary data using stored aes%0A     *%0A     * @param data - arbitrary data%0A     * @return encrypted data%0A     */%0A
     static encry
@@ -3596,8 +3596,9 @@
     %7D%0A%7D%0A
+%0A

Add comments