({{, (RefLink[FileHash, paclet:ref/FileHash])["file"] 
gives an integer hash code for the contents of the specified file.}, {, (RefLink[FileHash, paclet:ref/FileHash])["file","type"]
gives an integer hash of the specified type.}, {, (RefLink[FileHash, paclet:ref/FileHash])["file","type",range]
gives an integer hash code for the specified range of bytes.}, {, (RefLink[FileHash, paclet:ref/FileHash])["file","type",range,"format"]
gives a hash code in the specified format.}})

OpenersNotesSection(Spacer1)Details

Values generated by (RefLink[FileHash, paclet:ref/FileHash]) are based on the raw bytes in a file.

Possible hash code types include:

(RefLink[FileHash, paclet:ref/FileHash]) by default uses 128-bit MD5 code.

(RefLink[FileHash, paclet:ref/FileHash]) supports the following range specifications:

Range specification (RefLink[All, paclet:ref/All]) is equivalent to {1,-1}.

(RefLink[FileHash, paclet:ref/FileHash]) works on files and streams.

(RefLink[FileHash, paclet:ref/FileHash])[obj,…] effectively extracts data at the specified range of byte positions in the stream obj, ignoring any previous stream position.

(RefLink[FileHash, paclet:ref/FileHash])[(RefLink[File, paclet:ref/File])["file"],…] is also supported.

Possible formats include:

OpenersPrimaryExamplesSection(Spacer1)Examples  (20)

OpenersExampleSection0(Spacer1)Basic Examples  (3)

The fingerprint of a file:

FileHash[FindFile["ExampleData/ocelot.jpg"]]

The "SHA512" hash code of a file:

FileHash[FindFile["ExampleData/ocelot.jpg"], "SHA512"]

The "MD5" hash code in hexadecimal form:

FileHash[FindFile["ExampleData/ocelot.jpg"], "MD5",All,"HexString"]

OpenersExampleSection1(Spacer1)Scope  (7)

Find the hash of the file specified by the (RefLink[File, paclet:ref/File]) object:

file=File[FindFile["ExampleData/spikey2.png"]];

FileHash[file]

Compare all the different hash codes:

Grid[{#, FileHash[FindFile["ExampleData/pearls.png"], #]}&/@{"Adler32","CRC32","MD2","MD4","MD5","RIPEMD160","RIPEMD160SHA256","SHA","SHA256","SHA256SHA256","SHA384","SHA512"},Alignment->Left,Dividers->All]

Compute a hash for the first 100 bytes of a file:

FileHash[FindFile["ExampleData/glasses.png"], "SHA",100]

Equivalently:

FileHash[FindFile["ExampleData/glasses.png"], "SHA",{1,100}]

Compute a hash for the last 100 bytes:

FileHash[FindFile["ExampleData/glasses.png"], "SHA",-100]

Equivalently:

FileHash[FindFile["ExampleData/glasses.png"], "SHA",{-100,-1}]

Compute a hash for bytes 100 through 200:

FileHash[FindFile["ExampleData/glasses.png"], "SHA",{100,200}]

Compute a hash for all bytes except the first 100 or the last 100:

FileHash[FindFile["ExampleData/glasses.png"], "SHA",{101,-101}]

Compute a hash for no bytes:

FileHash[FindFile["ExampleData/glasses.png"], "SHA",0]

512-bit SHA code given as an integer:

FileHash[FindFile["ExampleData/ocelot.jpg"],  "SHA512",All,"Integer"]

IntegerQ[%]

512-bit SHA code given as a decimal string, including leading zeros:

FileHash[FindFile["ExampleData/ocelot.jpg"],  "SHA512",All,"DecimalString"]

StringQ[%]

Compare the different string representations of a hash:

Grid[{#,FileHash[FindFile["ExampleData/ocelot.jpg"],  "SHA",All,#]}& /@{"DecimalString","HexString","Base36String","Base64Encoding"},Alignment->Left]

The double SHA code of the first 50 bytes of a file, given as a (RefLink[ByteArray, paclet:ref/ByteArray]):

ba=FileHash[FindFile["ExampleData/financialtimeseries.csv"], "SHA256SHA256",50,"ByteArray"]

The byte array contains the 256 bits of the result:

256==Length[ba]*8

View the individual bytes in the array:

ba//Normal

OpenersExampleSection3(Spacer1)Properties & Relations  (10)

The default format is "MD5":

FileHash[FindFile["ExampleData/atoms.wxf"]]==FileHash[FindFile["ExampleData/atoms.wxf"],"MD5"]

The default range specification is (RefLink[All, paclet:ref/All]):

FileHash[FindFile["ExampleData/atoms.wxf"],"MD5"]==FileHash[FindFile["ExampleData/atoms.wxf"],"MD5",All]

This is equivalent to {-1,1}:

FileHash[FindFile["ExampleData/atoms.wxf"],"MD5"]==FileHash[FindFile["ExampleData/atoms.wxf"],"MD5",{1,-1}]

Hashing zero bytes is equivalent to finding the hash of an empty file:

file=CreateFile[];

FileHash[FindFile["ExampleData/atoms.wxf"],"MD5",0]==FileHash[file,"MD5",0]

DeleteFile[file]

"Integer" is the default format:

FileHash[FindFile["ExampleData/atoms.wxf"],"MD5",All]==FileHash[FindFile["ExampleData/atoms.wxf"],"MD5",All,"Integer"]

"DecimalString" is the string version of "Integer", padded with zeros if necessary:

{FileHash[FindFile["ExampleData/atoms.wxf"],"Adler32",1],FileHash[FindFile["ExampleData/atoms.wxf"],"Adler32",1,"DecimalString"]}

"HexString" is a base-16 representation, padded with zeros if necessary:

{FileHash[FindFile["ExampleData/atoms.wxf"],"Adler32",1],FileHash[FindFile["ExampleData/atoms.wxf"],"Adler32",1,"HexString"]}

"Base36String" is a base-36 representation, padded with zeros if necessary:

{FileHash[FindFile["ExampleData/atoms.wxf"],"Adler32",1],FileHash[FindFile["ExampleData/atoms.wxf"],"Adler32",1,"Base36String"]}

"Base64Encoding" encodes bytes of the result using Base64 encoding:

FileHash[FindFile["ExampleData/atoms.wxf"],"Adler32",1,"Base64Encoding"]

%===BaseEncode[FileHash[FindFile["ExampleData/atoms.wxf"],"Adler32",1,"ByteArray"]]

(RefLink[FileHash, paclet:ref/FileHash])[file,code] is effectively equivalent to (RefLink[Hash, paclet:ref/Hash])[(RefLink[ReadByteArray, paclet:ref/ReadByteArray])[file],code]:

FileHash[FindFile["ExampleData/ocelot.jpg"],"MD5"]===Hash[ReadByteArray[FindFile["ExampleData/ocelot.jpg"]],"MD5"]

(RefLink[FileHash, paclet:ref/FileHash])[file,code] is effectively equivalent to (RefLink[Hash, paclet:ref/Hash])[(RefLink[ByteArray, paclet:ref/ByteArray])@(RefLink[Import, paclet:ref/Import])[file,(StringTypeLink["Byte", paclet:ref/format/Byte])]]:

FileHash[FindFile["ExampleData/ocelot.jpg"],"SHA"]==Hash[ByteArray@Import["ExampleData/ocelot.jpg","Byte"],"SHA"]

({{DocumentationFooterIconsRelatedFunction.png, ({{See Also}, {(RefLink[FileSize, paclet:ref/FileSize])  ▪  (RefLink[FileByteCount, paclet:ref/FileByteCount])  ▪  (RefLink[FileDate, paclet:ref/FileDate])  ▪  (RefLink[Hash, paclet:ref/Hash])  ▪  (RefLink[BinaryReadList, paclet:ref/BinaryReadList])  ▪  (RefLink[FileFormat, paclet:ref/FileFormat])}})}})

({{DocumentationFooterIconsRelatedGuide.png, ({{Related Guides}, {▪(RefLinkPlain[Cryptographic Number Theory, paclet:guide/CryptographicNumberTheory])}, {▪(RefLinkPlain[Compression and Archive Formats, paclet:guide/CompressionAndArchiveFormats])}, {▪(RefLinkPlain[File Operations, paclet:guide/FileOperations])}, {▪(RefLinkPlain[Files, paclet:guide/Files])}, {▪(RefLinkPlain[External Operations, paclet:guide/ExternalOperations])}})}})