1
$\begingroup$

While looking though some code base I found the function which has no explanation and does something like this (pseudocode)

func something(str, num) { // str is a string, num is a number from 0 to 100
   hash = 0
   prime = 17
   for b in byte_array_of_base64decode(str){
       hash = (prime * hash + b) % 100
   }
   return hash < num
}

There is no explanation what the function suppose to do and the name of it also does not give any clue.

I assume that if this does any useful thing, it has something to do with hashing or sampling. Does anyone know the point of this function?

$\endgroup$

closed as off-topic by CodesInChaos Jan 19 '17 at 8:18

  • This question does not appear to be about cryptography within the scope defined in the help center.
If this question can be reworded to fit the rules in the help center, please edit the question.

  • $\begingroup$ Where is the function called from? That should help you figure out what it is for. That aside, I'm not sure this qualifies as crypto. Not all hashes are cryptographic hashes, and that one certainly isn't (i.e. no length padding = trivial collisions) $\endgroup$ – Ella Rose Jan 19 '17 at 1:25
  • $\begingroup$ @EllaRose sorry, will remove it in a few hours. $\endgroup$ – Salvador Dali Jan 19 '17 at 1:30
  • 1
    $\begingroup$ that code looks terrible, for so many reasons. 1, if num=100 it always returns true. 2, its performing mod 100 on a value ranging from 0 to 1938 for every byte of data, leading to bias points and an internal hash value of 0. 3, its completely linear, and can easily be manipulated to give a target result $\endgroup$ – Richie Frame Jan 19 '17 at 1:49
  • $\begingroup$ I'm closing this question as off-topic because it is about a non cryptographic hash. $\endgroup$ – CodesInChaos Jan 19 '17 at 8:18
  • 1
    $\begingroup$ @RichieFrame The code is clearly designed to return true with probability num%. So you can't criticize 100 always returning true. $\endgroup$ – CodesInChaos Jan 19 '17 at 9:26
5
$\begingroup$

Distributing objects among buckets of a hash map

This looks like an (old) textbook algorithm for allocating elements in a hash map or hash table. Other than the last line, which doesn't really make sense, it would be used calculate which hash map 'bucket' to use for putting/getting this item, as you need to calculate a simple, cheap and fast (so, generally not a cryptographic hash, but a few multiplications) code that would be an integer in the range from 0 to length of the map backing array (probably 100 in this case) that preferably is different for nonidentical similar objects to reduce the number of collisions.

A textbook example from Algorithms 4th ed by Sedgewick&Wayne, the similarity should be obvious:

int hash = 0;
for (int i = 0; i < s.length(); i++)
    hash = (R * hash + s.charAt(i)) % M;

This has nothing to do with cryptography, though. Nowadays many standard libraries try to use a cryptographically unpredictable algorithm for hashmap hashes to prevent a risk of DoS attacks by data engineered to induce worst case performance, but this example is not one of these.

$\endgroup$
3
$\begingroup$

My guess would be that it is intended to return either 0 or 1 with controllable probabilities. In this case, str would be a 'random' (or, at least, unpredictable) string, and so:

i = something( random_string, 75 )

would set i to 1 75% of the time, and 0 25% of the time. This might be useful if the program you were looking at was a game or a simulation.

It might not be that good at that, but that intention would appear to be the most plausible.

$\endgroup$

Not the answer you're looking for? Browse other questions tagged or ask your own question.