Polyglot CheatSheet - Hash

Updated: 2021-11-19

Hash

C++

std::hash

Python

Python's hash is randomized, the hash value stays the same in one process, but will change in another invocation.

>>> hash("asdf")
-6638445151105855253
>>> hash("asdf")
-6638445151105855253

# Restart

>>> hash("asdf")
5905973160914177863

Java

Integer.valueOf(value.hashCode());

Message Digest

Read more about crypto hash function here

Python

>>> hashlib.md5(b"hello").hexdigest()
'5d41402abc4b2a76b9719d911017c592'

Java

jshell> import java.security.MessageDigest

jshell> MessageDigest md = MessageDigest.getInstance("MD5")
md ==> MD5 Message Digest from SUN, <initialized>

Get digest as byte array:

jshell> byte[] a = md.digest("hello".getBytes())
a ==> byte[16] { 93, 65, 64, 42, -68, 75, 42, 118, -71, ...  -111, 16, 23, -59, -110 }

Convert the byte array to Hex String:

jshell> IntStream.range(0, a.length).mapToObj(i -> String.format("%02X", a[i])).collect(Collectors.joining())
$40 ==> "5D41402ABC4B2A76B9719D911017C592"