0

I borrowed this code from another post recently, and it works great for my needs... But I'm trying to reproduce the same results from Python code so the two language functions agree, and I've been struggling... These two so far do NOT produce the same results like I need... I suspect it's an issue with the unsigned integers in Python vs JavaScript.

JavaScript Code:

//Credits (modified code): Bob Jenkins (http://www.burtleburtle.net/bob/hash/doobs.html)
//See also: https://en.wikipedia.org/wiki/Jenkins_hash_function
//Takes a string of any size and returns an avalanching hash string of 8 hex characters.
function jenkinsOneAtATimeHash(keyString)
{
  let hash = 0;
  for (charIndex = 0; charIndex < keyString.length; ++charIndex)
  {
    hash += keyString.charCodeAt(charIndex);
    hash += hash << 10;
    hash ^= hash >> 6;
  }
  hash += hash << 3;
  hash ^= hash >> 11;
  //4,294,967,295 is FFFFFFFF, the maximum 32 bit unsigned integer value, used here as a mask.
  return (((hash + (hash << 15)) & 4294967295) >>> 0).toString(16)
};

Python code:

def calcuulateChecksum(keyString: str):
    # Credits(modified code): Bob Jenkins (http://www.burtleburtle.net/bob/hash/doobs.html)
    # See also: https://en.wikipedia.org/wiki/Jenkins_hash_function
    # Takes a string of any size and returns an avalanching hash string of 8 hex characters.
    hash = 0
    # for (charIndex = 0; charIndex < keyString.length; ++charIndex):
    for char in keyString:
        hash += ord(char.encode("utf-8"))
        hash &= 0xFFFFFFFF
        hash += hash << 10
        hash &= 0xFFFFFFFF
        hash ^= hash >> 6
        hash &= 0xFFFFFFFF
    hash += hash << 3
    hash &= 0xFFFFFFFF
    hash ^= hash >> 11
    hash &= 0xFFFFFFFF
    hash += hash << 15
    hash &= 0xFFFFFFFF
    # # 4,294,967,295 is 0xffffffff, the maximum 32 bit unsigned integer value, used here as a mask.
    return hex((hash & 4294967295))

Any help on making the Python code match the JavaScript function would be appreciated... On the other hand, I believe the Python code matches results shown in the Wiki, so why doesn't the JavaScript code?

  • I'm not completely sure I understand why you're using `.encode('utf8')` on individual characters and if that even makes sense. Maybe you to encode the entire string rather than individual characters? Can you give an example string where they give different results? – Frank Yellin Dec 01 '21 at 01:03
  • No, I cannot.. :) That was one thing I tried in my attempts to get the two languages functions to come up with the same result, because of the number of bits some characters could or might require to be represented. I clearly believe that is not the issue.. I feel the issue is in how JavaScript allows signed integers where Python would seem not to. – Khile T. Klock Dec 01 '21 at 16:06

1 Answers1

0

MAYBE I just figured this out.... just am not sure why it works.... If I change the JavaScript code to use only unsigned right shifts, it would seem to result in the same output as the Python (and original C code) does.... I'll have to do further testing across a range of keyString values.

function jenkinsOneAtATimeHash(keyString)
{
  let hash = 0;
  for (charIndex = 0; charIndex < keyString.length; ++charIndex)
  {
    hash += keyString.charCodeAt(charIndex);
    hash += hash << 10;
    hash ^= hash >>> 6;
  }
  hash += hash << 3;
  hash ^= hash >>> 11;
  //4,294,967,295 is FFFFFFFF, the maximum 32 bit unsigned integer value, used here as a mask.
  return (((hash + (hash << 15)) & 4294967295) >>> 0).toString(16)
};