Jump to content

Is it possible to work with Mega.co.nz and AutoIT


Recommended Posts

I was wondering if its possible to make anything like this in AutoIT

 

function toArr(str)
{
    var ret = Array((str.length + 3) >> 2);

    // String to array of 32 bit ints
    for (var i = 0; i < str.length; i += 4)
    {
        // FYI:
        // NaN << x === 0
        // x | NaN === x
        ret[i >> 2] =
            (str.charCodeAt(i  ) << 24) |
            (str.charCodeAt(i+1) << 16) |
            (str.charCodeAt(i+2) <<  8) |
             str.charCodeAt(i+3);
    }
    return ret;
}

function megaKdf(pw)
{
    var pwkey = [0x93c467e3, 0x7db0c7a4, 0xd1be3f81, 0x0152cb56];
    var arrPw = toArr(pw);
    var aes   = Array((arrPw + 3) >> 2);

    // Init aes keys
    for (var i = 0; i < arrPw.length; i += 4)
    {
        aes[i >> 2] = new sjcl.cipher.aes([arrPw[i]|0, arrPw[i+1]|0, arrPw[i+2]|0, arrPw[i+3]|0]);
    }

    // Generate password key
    for (var i = 0; i < 65536; i++)
    {
        for (var j = 0; j < aes.length; j++)
        {
            pwkey = aes[j].encrypt(pwkey);
        }
    }

    return pwkey;
}

function loginHash(pwKey, email)
{
    var aes = new sjcl.cipher.aes(pwKey);
    var arrEmail = toArr(email);
    var hash = [0, 0, 0, 0];

    // Compress email with xor
    for (var i = 0; i < arrEmail.length; i++)
    {
        hash[i & 3] ^= arrEmail[i];
    }

    // Generate hash
    for (var i = 0; i < 16384; i++)
    {
        hash = aes.encrypt(hash);
    }

    return sjcl.codec.base64.fromBits([hash[0], hash[2]], true, true);
}

function badRand32()
{
    return Math.floor(0x100000000 * Math.random());
}

function generateMasterKey()
{
    return [badRand32(), badRand32(), badRand32(), badRand32()];
}

function confirmationLinkHash(pwKey)
{
    var aes = new sjcl.cipher.aes(pwKey);
    return aes.encrypt([badRand32(), 0, 0, badRand32()]);
}

function updateHashes(email, pw)
{
    var pwKey = megaKdf(pw);
    var masterKey = generateMasterKey();
    var clHash = confirmationLinkHash(pwKey, masterKey);
    var lHash = loginHash(pwKey, email);
    var aes = new sjcl.cipher.aes(pwKey);

    return lHash;
}

function htmlentities(str)
{
    return str.replace(/&/g,"&amp;").replace(/"/g,"&quot;").replace(/</g,"&lt;").replace(/>/g,"&gt;").replace(/'/g,"&#39;");
}

function analyzeLink(link, pw)
{
    var linkHex      = "N/A";
    var masterKey    = "N/A";
    var encMasterKey = "N/A";
    var pwHash       = "N/A";
    var unknown      = "N/A";
    var email        = "N/A";
    var spacer       = "N/A";
    var name         = "N/A";
    var unknown2     = "N/A";

    if (link.substr(0, 27) == "https://mega.co.nz/#confirm")
    {
        link = link.substr(27);
    }
    if (link.substr(0, 19) == "mega.co.nz/#confirm")
    {
        link = link.substr(19);
    }
    if (link.substr(0, 8) == "#confirm")
    {
        link = link.substr(8);
    }
    if (link.search(/^[0-9A-Za-z\-_]*$/) == 0)
    {
        link = sjcl.codec.hex.fromBits(sjcl.codec.base64.toBits(link, true));

        var pos;
        for (pos = 94; pos < link.length; pos += 2)
        {
            if (link.substr(pos, 2) == "09")
            {
                break;
            }
        }
        if (pos < link.length)
        {
            var pwKey = megaKdf(pw);
            var aes = new sjcl.cipher.aes(pwKey);
            var linkHash = aes.decrypt(sjcl.codec.hex.toBits(link.substr(32, 32)));
            if (linkHash[1] == 0 && linkHash[2] == 0)
            {
                masterKey = (new sjcl.cipher.aes(pwKey)).decrypt(sjcl.codec.hex.toBits(link.substr(0, 32)));
                masterKey = 'Base64: <span style="color:#f00; font-family:monospace;">' + sjcl.codec.base64.fromBits(masterKey, true, true) + '</span> (hex: <span style="color:#f00; font-family:monospace;">' + sjcl.codec.hex.fromBits(masterKey) + '</span>)';
            }
            else if (pw != "")
            {
                masterKey = "Wrong password";
            }
            encMasterKey = '<span style="color:#f00; font-family:monospace;">' + link.substr( 0, 32) + '</span>';
            pwHash       = '<span style="color:#080; font-family:monospace;">' + link.substr(32, 32) + '</span>';
            unknown      = '<span style="color:#00f; font-family:monospace;">' + link.substr(64, 30) + '</span>';
            email        = link.substr(94, pos - 94);
            spacer       = '<span style="color:#0cc; font-family:monospace;">09</span>';
            name         = link.substr(pos + 2, link.length - 16 - pos - 2);
            unknown2     = '<span style="color:#000; font-family:monospace;">' + link.substr(link.length - 16) + '</span>';
            linkHex = '<span style="font-family:monospace;">' + encMasterKey + " " + pwHash + " " + unknown +
                ' <span style="color:#888; font-family:monospace;">' + email + '</span> ' + spacer +
                ' <span style="color:#f0f; font-family:monospace;">' + name + '</span> ' + unknown2 + '</span>';
            email = htmlentities(sjcl.codec.utf8String.fromBits(sjcl.codec.hex.toBits(email))) + ' (<span style="color:#888; font-family:monospace;">' + email + '</span>)';
            name  = htmlentities(sjcl.codec.utf8String.fromBits(sjcl.codec.hex.toBits(name)))  + ' (<span style="color:#f0f; font-family:monospace;">' + name  + '</span>)';
        }
    }
    ge("linkHex").innerHTML          = linkHex;
    ge("linkMasterKey").innerHTML    = masterKey;
    ge("linkEncMasterKey").innerHTML = encMasterKey;
    ge("linkPwHash").innerHTML       = pwHash;
    ge("linkUnknown").innerHTML      = unknown;
    ge("linkEmail").innerHTML        = email;
    ge("linkSpacer").innerHTML       = spacer;
    ge("linkName").innerHTML         = name;
    ge("linkUnknown2").innerHTML     = unknown2;
}

 

Edited by RyukShini
Link to comment
Share on other sites

Link to comment
Share on other sites

Not being too familiar with the Javascript language reference, I don't know what some of the functions are doing (like how the comma separated values are handled/fed to the AES func...but it looks plausible.  As far as the HTML rendering, you'd have to embed some sort of browser/renderer unless you can afford to use other UI means/elements.

Link to comment
Share on other sites

@RyukShini,

it would be wiser not to try to translate the code, but to describe the task, and then try to implement it in AutoIt. then, if you need help with your AutoIt script, you can ask the forum.

but when you are asking AutoIt forum members to read and understand an enigmatic and lengthy piece of code written in another language, it is not surprising you have not yet got any help.

Signature - my forum contributions:

Spoiler

UDF:

LFN - support for long file names (over 260 characters)

InputImpose - impose valid characters in an input control

TimeConvert - convert UTC to/from local time and/or reformat the string representation

AMF - accept multiple files from Windows Explorer context menu

DateDuration -  literal description of the difference between given dates

Apps:

Touch - set the "modified" timestamp of a file to current time

Show For Files - tray menu to show/hide files extensions, hidden & system files, and selection checkboxes

SPDiff - Single-Pane Text Diff

 

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...