What can I do to get Oracle and Java hash codes to match?

lozina

Lifer
Sep 10, 2001
11,711
8
81
I want to be able to take an identical string and get the same hash code for it from both Oracle and Java.

If I use ora_hash ( string ) in Oracle
and String.hashCode() in Java

I get a different result

Do I have another option?
 

DaveSimmons

Elite Member
Aug 12, 2001
40,730
670
126
Does Oracle have any functions for a standard / known hashing scheme like MD5, or just their unknown hash code?

If not, can you write your own c++ / java / etc. extension / function for Oracle to add one? (I have no idea how extensible Oracle dbs are.)
 

Cogman

Lifer
Sep 19, 2000
10,284
138
106
What david said, there are literally 1000's of hashing algorithms out there. To get it to match, you need to be using the same hashing algorithm and the same seed.
 

Train

Lifer
Jun 22, 2000
13,581
80
91
www.bing.com
Check the docs, the common ones today, IIRC, are SHA-1 and SHA-2, or possibly MD5 like someone mentioned above.

Also make sure the "string" in Oracle is treated the same as a string in Java, i.e. is your Oracle string unicode and your Java string UTF? That could also make a difference
 

Crusty

Lifer
Sep 30, 2001
12,684
2
81
IIRC Java uses something like for a string hash.

for i = 0 -> n - 1
hash += string * 31 ^(n-1-i)

It should be pretty trivial to implement that in Oracle. I'm sure you could create some sort of UDF that does your own hashing instead of relying on the built-in one.
 

boran

Golden Member
Jun 17, 2001
1,526
0
76
Nobody has mentioned yet here that a string hash in Java is NOT guaranteed to be unique. Its only function is for use in a hash table to place strings in buckets.
I recommend like other posters to use a hashing algorithm that is designed to generate unique hashes.
 

Train

Lifer
Jun 22, 2000
13,581
80
91
www.bing.com
Nobody has mentioned yet here that a string hash in Java is NOT guaranteed to be unique. Its only function is for use in a hash table to place strings in buckets.
I recommend like other posters to use a hashing algorithm that is designed to generate unique hashes.

NO language or hashing algorithm can guarantee uniqueness. Hash collisions are something that need to be handled.

The only way to guarantee a 1:1 seed:hash is to have an exact match within the hash space. i.e. an 8 bit hash for an 8 bit input, wherein the algo never repeats its output. Of course this is only feasible for small inputs, Once you go to something like variable length strings, your hashspace is usually 128 bits or more, which is next to impossible to guarantee a 1:1 mapping
 

DaveSimmons

Elite Member
Aug 12, 2001
40,730
670
126
I recommend like other posters to use a hashing algorithm that is designed to generate unique hashes.

We aren't recommending a unique hash, we're recommending a documented hash that follows some standard like MD5 or SHA-1.

Crusty's approach of coding your own for Oracle and possibly also Java will work too if done carefully (making sure large values don't overflow differently for example).
 

EagleKeeper

Discussion Club Moderator<br>Elite Member
Staff member
Oct 30, 2000
42,589
5
0
Another thread rises from the grave.
Allowed only because the post contains useful info and this forum is not normally frequented by troll types., but by those that want to assist their fellow crazys():)

Common Courtesy
AT Admin