[Python] Hash function converted from JS to python hangs up on large strings

Discussão em 'Python' iniciado por Stack, Maio 28, 2025.

  1. Stack

    Stack Membro Participativo

    I've been using this cyrb53 hash function in js from this answer: Generate a Hash from string in Javascript

    The idea is to create hashes from image strings like this:

    data:image/png;base64,iVBORI12FAZ6k2AAAAABJRU5ErkJggg=


    I'm moving my project over to python (my first python project) and already have thousands of hashes defined using this algorithm, so for consistency I need to keep using that same function in Python. It'll work with the example string above, but on a full string of image data it hangs up. Whereas JS tooks milliseconds, python sits for several minutes without completing.

    Here's the JS function:

    const cyrb53 = (str, seed = 0) => {
    let h1 = 0xdeadbeef ^ seed, h2 = 0x41c6ce57 ^ seed;
    for(let i = 0, ch; i < str.length; i++) {
    ch = str.charCodeAt(i);
    h1 = Math.imul(h1 ^ ch, 2654435761);
    h2 = Math.imul(h2 ^ ch, 1597334677);
    }
    h1 = Math.imul(h1 ^ (h1 >>> 16), 2246822507);
    h1 ^= Math.imul(h2 ^ (h2 >>> 13), 3266489909);
    h2 = Math.imul(h2 ^ (h2 >>> 16), 2246822507);
    h2 ^= Math.imul(h1 ^ (h1 >>> 13), 3266489909);

    return 4294967296 * (2097151 & h2) + (h1 >>> 0);
    };


    And here's my python conversion:

    def unsigned_right_shift(n, shift):
    # Create a mask to simulate a 32-bit unsigned integer
    mask = 0xFFFFFFFF
    # Apply the mask to ensure the number is treated as unsigned
    n &= mask
    # Perform the right shift
    result = n >> shift
    return result


    def cyrb53x(str, seed=0):
    h1 = 0xdeadbeef ^ seed
    h2 = 0x41c6ce57 ^ seed

    for ch in str:
    h1 = (h1 ^ ord(ch)) * 2654435761
    h2 = (h2 ^ ord(ch)) * 1597334677

    h1 = (h1 ^ unsigned_right_shift(h1 , 16)) * 2246822507
    h1 ^= (h2 ^ unsigned_right_shift(h2 , 13)) * 3266489909
    h2 = (h2 ^ unsigned_right_shift(h2 , 16)) * 2246822507
    h2 ^= (h1 ^ unsigned_right_shift(h1 , 13)) * 3266489909

    return 4294967296 * (2097151 & h2) + (h1 & 0xFFFFFFFF)


    The strings passed to the function is very large, as they are base64 encoded images. Is this just a limitation of python performance? With the short example input above, the result is 85900107939316 for both JS and python so the function does technically work. For one of my real world examples, the string is over 1m characters.

    Continue reading...

Compartilhe esta Página