Hash
Details
- Hash[expr,…] will always give the same result for the same expression expr.
- Possible hash types include:
-
"Adler32" Adler 32-bit cyclic redundancy check "BLAKE2b-512" 512-bit BLAKE2b code "BLAKE2s-256" 256-bit BLAKE2s code {"BLAKE2b",b} BLAKE2b code with arbitrary length b up to 512 bits {"BLAKE2s",b} BLAKE2s code with arbitrary length b up to 256 bits {"BLAKE2bp",b} multicore parallel version of BLAKE2b {"BLAKE2sp",b} multicore parallel version of BLAKE2s {"BLAKE2xb",b} BLAKE2xb code with arbitrary length b up to 256 GiB {"BLAKE2xs",b} BLAKE2xs code with arbitrary length b up to 128 GiB "CRC32" 32-bit cyclic redundancy check "Keccak224" 224-bit Keccak code "Keccak256" 256-bit Keccak code "Keccak384" 384-bit Keccak code "Keccak512" 512-bit Keccak code {"Keccak",b} Keccak code with length b of 224, 256, 384, 512 bits "MD2" 128-bit MD2 code "MD4" 128-bit MD4 code "MD5" 128-bit MD5 code "RIPEMD160" 160-bit RIPEMD code "RIPEMD160SHA256" RIPEMD-160 following SHA-256 (as used in Bitcoin) "SHA1" 160-bit SHA-1 code "SHA224" 224-bit SHA code "SHA256" 256-bit SHA code "SHA256SHA256" double SHA-256 code (as used in Bitcoin) "SHA384" 384-bit SHA code "SHA512" 512-bit SHA code {"SHA",b} SHA code with length b of 224, 256, 384, 512 bits "SHA3-224" 224-bit SHA3 code "SHA3-256" 256-bit SHA3 code "SHA3-384" 384-bit SHA3 code "SHA3-512" 512-bit SHA3 code {"SHA3",b} SHA3 code with length b of 224, 256, 384, 512 bits "Expression" expression hash code (default) - For a variable-length hash, length b must be a multiple of 8.
- The "Expression" hash is computed from the internal representation of an expression and may vary between computer systems and from one version of the Wolfram Language to another.
- For hashes that operate on sequences of bytes (such as "SHA"), Hash[expr,…] first converts expr to bytes according to:
-
expr bytes based on ToString[FullForm[expr]] "string" bytes in the UTF-8 representation of string ByteArray[…] literal bytes in the byte array - Possible formats include:
-
"Integer" integer (default) "DecimalString" decimal string "HexString" hexadecimal string "HexStringLittleEndian" hexadecimal string with little-endian byte order "Base36String" base-36 alphanumeric string "Base64Encoding" Base64 encoding "ByteArray" hash code as an explicit byte array
Examples
open all close allBasic Examples (4)
Hash["abcdef"]Hash[ByteArray[{123, 45, 67, 89, 98, 76, 54, 32, 1}], "SHA"]SHA256 hash given in hexadecimal form:
Hash["The quick brown fox jumps over the lazy dog", "SHA256", "HexString"]Specify output length of the SHA hash:
Hash["The quick brown fox jumps over the lazy dog", {"SHA", 256}, "HexString"]Scope (12)
Hash[Graphics[Disk[]]]Hash[Graphics[Disk[]], "Expression"]Compare all the different hash codes:
Grid[{#, Hash[Graphics[Disk[]], #]}& /@ {"Adler32", "CRC32", "MD2", "MD4", "MD5", "RIPEMD160", "RIPEMD160SHA256", "SHA", "SHA256", "SHA256SHA256", "SHA384", "SHA512", "SHA3-224", "SHA3-256", "SHA3-384", "SHA3-512", "Keccak224", "Keccak256", "Keccak384", "Keccak512", "Expression"}, Alignment -> Left, Dividers -> All]Calculate "BLAKE2b" hashes with different lengths:
Grid[{#, Hash["abc", {"BLAKE2b", #}, "HexString"]}& /@ {8, 16, 24, 32, 48, 128, 136, 144, 256, 512}, Alignment -> Left, Dividers -> All]512-bit SHA code given as an integer:
Hash[x ^ 2 + 10, "SHA512", "Integer"]IntegerQ[%]512-bit SHA code given as a decimal string, including leading zeroes:
Hash[x ^ 2 + 10, "SHA512", "DecimalString"]StringQ[%]Compare the different string representations of a hash:
Grid[{#, Hash["363", "MD5", #]}& /@ {"DecimalString", "HexString", "Base36String", "Base64Encoding"}, Alignment -> Left]The double SHA code given as a ByteArray:
ba = Hash[Sin[x], "SHA256SHA256", "ByteArray"]The byte array contains the 256 bits of the result:
256 == Length[ba] * 8View the individual bytes in the array:
ba//NormalRIPEMD160SHA256 hash is essentially a repeated hash:
Hash[1, "RIPEMD160SHA256"]Hash[Hash[1, "SHA256", "ByteArray"], "RIPEMD160"]When using ByteArray or a string, literal bytes are hashed:
Hash[ByteArray[ToCharacterCode["abc"]], "MD5"]Hash["abc", "MD5"]For non-ASCII characters, UTF-8 representation is used for hashing:
ToCharacterCode["ሴ"]ToCharacterCode["ሴ", "UTF-8"]Hash["ሴ", "MD5"]Hash[ByteArray[{225, 136, 180}], "MD5"]Compute a cryptographic hash of zero bytes:
Hash["", "SHA"]Calculate BLAKE2xs hash of desired length:
Hash["x", {"BLAKE2xs", 2400}, "HexString"]Applications (2)
Provide a "checksum" to validate data integrity:
data = RandomInteger[10, {1000}];check = Hash[data, "CRC32"]data[[500]] = data[[500]] + 1;Hash[data, "CRC32"]% == checkA concatenated cryptographic hash function:
h[e_] := BitOr[Hash[e, "MD5"], Hash[e, "SHA"]]h["abcdef"]Properties & Relations (12)
The hash is always the same for identical expressions:
{e1, e2} = {x ^ 2 + 10, x^2 + 10};SameQ[e1, e2]{Hash[e1], Hash[e2]}Distinct hash codes come from distinct inputs:
{Hash[123], Hash[124]}{Hash[x ^ 2 + 10], Hash[x ^ 2. + 10]}The default hash code is "Expression":
Hash["test"] === Hash["test", "Expression"]The "Expression" hash fits in a machine word:
hash = Hash[2, "Expression", "ByteArray"];Length[hash] == $SystemWordLength / 8BitAnd[128, First[hash]]"Integer" is the default format:
Hash["test", "SHA"] === Hash["test", "SHA", "Integer"]"DecimalString" is the string version of "Integer", padded with zeros if necessary:
{Hash["test", "SHA", "Integer"], Hash["test", "SHA", "DecimalString"]}//InputForm"HexString" is a base 16 representation, padded with zeros if necessary:
{IntegerString[Hash["363", "MD5"], 16], Hash["363", "MD5", "HexString"]}"Base36String" is a base 36 representation, padded with zeros if necessary:
{IntegerString[Hash["351", "SHA"], 36], Hash["351", "SHA", "Base36String"]}"Base64Encoding" encodes bytes of the result using Base64 encoding:
Hash["test", "SHA", "Base64Encoding"]% === BaseEncode[Hash["test", "SHA", "ByteArray"]]"ByteArray" is a base 256 representation:
ba = Hash["test", "SHA", "ByteArray"]ba//NormalConvert from base 256 to an integer:
Dot[Normal[ba], Reverse@Table[256 ^ i, {i, 0, Length[ba] - 1}]]Hash["test", "SHA"]Repeated hash can be obtained by using ByteArray as an intermediate result:
Hash[Hash["abc", "SHA256", "ByteArray"], "SHA256", "HexString"]Hash["abc", "SHA256SHA256", "HexString"]FileHash[file,code] is effectively equivalent to Hash[ReadByteArray[file],code]:
FileHash[FindFile["ExampleData/ocelot.jpg"], "MD5"] === Hash[ReadByteArray[FindFile["ExampleData/ocelot.jpg"]], "MD5"]Possible Issues (3)
Hash of a List of integers uses a serialized version of the list as an expression:
Hash[{65, 66, 67}, "MD5"]To hash literal bytes, use a ByteArray:
Hash[ByteArray[{65, 66, 67}], "MD5"]ASCII strings can be used to hash 7-bit byte values:
str = FromCharacterCode[{65, 66, 67}, "ASCII"]Hash[str, "MD5"]The Hash of an expression and a string containing the FullForm of the expression are different:
Hash[{65, 66, 67}, "MD5"]Hash[ToString[FullForm[{65, 66, 67}]], "MD5"]Hash takes into account the full symbol name, including its Context, which may result in different hash values:
Hash[x ^ 2 + 10, "SHA512", "Integer"]Hash[Global`x ^ 2 + 10, "SHA512", "Integer"]Neat Examples (2)
Hash collisions are very rare, but possible. Here are two lists of bytes:
a = {77, 201, 104, 255, 14, 227, 92, 32, 149, 114, 212, 119, 123, 114, 21, 135, 211, 111, 167, 178, 27, 220, 86, 183, 74, 61, 192, 120, 62, 123, 149, 24, 175, 191, 162, 0, 168, 40, 75, 243, 110, 142, 75, 85, 179, 95, 66, 117, 147, 216, 73, 103, 109, 160, 209, 85, 93, 131, 96, 251, 95, 7, 254, 162};b = {77, 201, 104, 255, 14, 227, 92, 32, 149, 114, 212, 119, 123, 114, 21, 135, 211, 111, 167, 178, 27, 220, 86, 183, 74, 61, 192, 120, 62, 123, 149, 24, 175, 191, 162, 2, 168, 40, 75, 243, 110, 142, 75, 85, 179, 95, 66, 117, 147, 216, 73, 103, 109, 160, 209, 213, 93, 131, 96, 251, 95, 7, 254, 162};a === bSequenceAlignment[a, b]Hash the two sequences of bytes:
hasha = Hash[ByteArray[a], "MD5", "HexString"]hashb = Hash[ByteArray[b], "MD5", "HexString"]hasha === hashbDistribution of hash values for different types:
ListPlot[Table[Hash[i], {i, 1000}]]ListPlot[Table[Hash[i, "Adler32"], {i, 1000}]]ListPlot[Table[Hash[i, "CRC32"], {i, 1000}]]ListPlot[Table[Hash[i, "MD2"], {i, 1000}]]ListPlot[Table[Hash[i, "MD5"], {i, 1000}]]ListPlot[Table[Hash[i, "SHA"], {i, 1000}]]See Also
FileHash GenerateDerivedKey GenerateSymmetricKey GenerateDigitalSignature LeafCount ByteCount Dispatch BaseEncode BinarySerialize
Data Structures: HashTable HashSet
Function Repository: HMAC FowlerNollVoHash
History
Introduced in 1988 (1.0) | Updated in 2007 (6.0) ▪ 2016 (11.0) ▪ 2018 (11.3) ▪ 2019 (12.0) ▪ 2022 (13.1)
Text
Wolfram Research (1988), Hash, Wolfram Language function, https://reference.wolfram.com/language/ref/Hash.html (updated 2022).
CMS
Wolfram Language. 1988. "Hash." Wolfram Language & System Documentation Center. Wolfram Research. Last Modified 2022. https://reference.wolfram.com/language/ref/Hash.html.
APA
Wolfram Language. (1988). Hash. Wolfram Language & System Documentation Center. Retrieved from https://reference.wolfram.com/language/ref/Hash.html
BibTeX
@misc{reference.wolfram_2026_hash, author="Wolfram Research", title="{Hash}", year="2022", howpublished="\url{https://reference.wolfram.com/language/ref/Hash.html}", note=[Accessed: 13-June-2026]}
BibLaTeX
@online{reference.wolfram_2026_hash, organization={Wolfram Research}, title={Hash}, year={2022}, url={https://reference.wolfram.com/language/ref/Hash.html}, note=[Accessed: 13-June-2026]}