Adding XOR function to bigint library

Posted by Jason Gooner on Stack Overflow See other posts from Stack Overflow or by Jason Gooner
Published on 2010-06-09T16:30:32Z Indexed on 2010/06/09 17:32 UTC
Read the original article Hit count: 279

Filed under:

Hi,

I'm using this Big Integer library for Javascript: http://www.leemon.com/crypto/BigInt.js and I need to be able to XOR two bigInts together and sadly the library doesn't include such a function. The library is relatively simple so I don't think it's a huge task, just confusing.

I've been trying to hack one together but not having much luck, would be very grateful if someone could lend me a hand. This is what I've attempted (might be wrong). But im guessing the structure is going to be quite similar to some of the other functions in there.

function xor(x, y)
{
    var c, k, i;
    var result = new Array(0); // big int for result

    k=x.length>y.length ? x.length : y.length; // array length of the larger num

    // Make sure result is the correct array size? maybe:
    result = expand(result, k); // ?

    for (c=0, i=0; i < k; i++)
    {
        // Do some xor here
    }

    // return the bigint xor result
    return result;
}

What confuses me is I don't really understand how it stores numbers in the array blocks for the bigInt. I don't think it's a case of simply bigintC[i] = bigintA[i] ^ bigintB[i], then most other functions have some masking operation at the end that I don't understand. I would really appreciate any help getting this working.

Thanks

© Stack Overflow or respective owner

Related posts about JavaScript