If I want to combine two numbers (Int,Long,...) n1,n2
in a non-commutative way, p*n1 + n2
where p
is an arbitrary prime seems reasonable enough a choice.
As many hashing options return a byte array, though, I am now trying to substitute the numbers with byte arrays.
Assume a,b:Array[Byte]
are of the same length.
+
simply becomes an xor
but what should I use as a "Multiplication"?
p:Long
a(n arbitrary) prime, a:Array[Byte]
of arbitrary length
I could, of course, convert a
to a long, multiply, then convert the result back to an Array of Bytes. The problem with that is that I will need "p*a
" to be of the same length as a
for the subsequent xor to make sense. I could circumvent this by zero-extending the shorter of the two byte arrays, but then the byte arrays quickly grow in length.
I could, on the other hand, convert p
to a byte array and xor it with a
. Here, the issue is that then (p*(p*a+b)+c)
becomes (a+b+c)
, which is commutative, which we don't want.
I could add p to every byte in the array (throwing away the overflow).
I could add p to every byte in the array (not throwing away the overflow).
I could circular shift a
by some f(p)
bits (and hope it doesn't end up becoming a
again)
And I could think of a lot more nonsense. But what should I do? What actually makes sense?