Created
July 21, 2014 14:51
-
-
Save emoran/5511d28a37a094a99a3c to your computer and use it in GitHub Desktop.
HashCode generator Apex code
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class HashCodeGenerator{ | |
public static integer getHashCode(object obj){ | |
Map<string, integer> vals = new Map<string, integer>{'0' => 0, '1' => 1, '2' => 2, '3' => 3, '4' => 4, '5' => 5, '6' => 6, '7' => 7, '8' => 8, '9' => 9, 'A' => 10, 'B' => 11, 'C' => 12, 'D' => 13, 'E' => 14, 'F' => 15}; | |
string objJS = JSON.serialize(obj); | |
Blob b = Blob.valueOf(objJS); | |
Blob bHash = Crypto.generateMac('hmacSHA1', b, blob.valueOf('a key that does not matter')); | |
string objHex = EncodingUtil.convertToHex(bHash); | |
long hash = long.valueOf('2166136261'); | |
long prime = long.valueOf('16777619'); | |
for(string s : objHex.toUpperCase().split('')){ | |
if(string.isEmpty(s)) | |
continue; | |
hash = ((hash ^ vals.get(s)) * prime); | |
} | |
return (integer)hash; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment