Checksum

Description

Generates checksum for files. This task can also be used to perform checksum verifications.

Note that many popular message digest functions—including MD5 and SHA-1—have been broken recently. If you are going to use the task to create checksums used in an environment where security is important, please take some time to investigate the algorithms offered by your JCE provider. Note also that some JCE providers like the one by The Legion of the Bouncy Castle, the GNU Crypto project or the Technical University Graz offer more digest algorithms than those built-in into your JDK.

Warning: the case of the extension is that of the algorithm used. If you ask for SHA1, you get a .SHA1 extension; if you ask for sha1, you get a file ending in .sha1. The Java Crypto Engines are case-insensitive in matching algorithms, so choose a name to match your desired output extension, or set the fileext attribute. The names of common hashing algorithms can be located on the Cryptography Architecture Standard Algorithm Name Documentation

Parameters

Attribute Description Required
file The file to generate checksum for. Yes, unless at least one nested (filesystem-only) resource collection is specified.
todir The root directory where checksums should be written. No; by default, checksum files will be written to the same directory as the original files. since Apache Ant 1.6
algorithm Specifies the algorithm to be used to compute the checksum. Please check the documentation for available algorithm names, like SHA-1 or SHA-512. No; defaults to MD5
provider Specifies the provider of the algorithm. No
fileext The generated checksum file's name will be the original filename with the fileext added to it. No; defaults to a . and the algorithm name being used
property This attribute can mean two different things, it depends on the presence of the verifyproperty attribute.
If you don't set the verifyproperty attribute, property specifies the name of the property to be set with the generated checksum value.
If you set the verifyproperty attribute, property specifies the checksum you expect to be generated (the checksum itself, not a name of a property containing the checksum).
This cannot be specified when fileext is being used or when the number of files for which checksums are to be generated is greater than 1.
No
pattern Specifies the pattern to use as a pattern suitable for MessageFormat where {0} is replaced with the checksum and {1} with the file name. Since Ant 1.7.0
Since Ant 1.8.2 {2} is replaced by the path of the file relative to the checksum file being written, {3} with the path of the file relative to the project's basedir and {4} with the absolute path of the file.
No; default is {0}
format Specifies the pattern to use as one of a well-known format. Supported values are
name pattern description
CHECKSUM {0} only the checksum itself
MD5SUM {0} *{1} the format of GNU textutils md5sum
SVF MD5 ({1}) = {0} the format of BSD md5 command
Since Ant 1.7.0
No; default is CHECKSUM
totalproperty If specified, this attribute specifies the name of the property that will hold a checksum of all the checksums and file paths. The individual checksums and the relative paths to the files within the resource collections in which they are defined will be used to compute this checksum. (The file separators in the paths will be converted to / before computation to ensure platform portability). since Ant 1.6 No
forceoverwrite Overwrite existing files even if the destination files are newer. No; defaults to no
verifyproperty Specifies the name of the property to be set with true or false depending upon whether the generated checksum matches the existing checksum. When this is set, the generated checksum is not written to a file or property, but rather, the content of the file or property is used to check against the generated checksum. No
readbuffersize The size of the buffer (in bytes) to use when reading a file. No; defaults to 8192—you may get a better performance on big files if you increase this value

Parameters specified as nested elements

resource collections

Resource collections are used to select files for which checksums should be generated.

Examples

Example 1

Generate a MD5 checksum for foo.bar and store the checksum in the destination file foo.bar.MD5. foo.bar.MD5 is overwritten only if foo.bar is newer than itself.

<checksum file="foo.bar"/>

Example 2

Generate a MD5 checksum for foo.bar and store the checksum in foo.bar.MD5. If foo.bar.MD5 already exists, it is overwritten.

<checksum file="foo.bar" forceOverwrite="yes"/>

Example 3

Generate a MD5 checksum for foo.bar and store it in the project property foobarMD5.

<checksum file="foo.bar" property="foobarMD5"/>

Example 4

Generate a MD5 checksum for foo.bar, compare it against foo.bar.MD5 and set isMD5ok to either true or false, depending upon the result.

<checksum file="foo.bar" verifyProperty="isMD5ok"/>

Example 5

Generate a SHA-512 checksum for foo.bar and store the checksum in the destination file foo.bar.asc. foo.bar.asc is overwritten only if foo.bar is newer than itself.

<checksum file="foo.bar" algorithm="SHA-512" fileext="asc"/>

Example 6

Generate a MD5 checksum for foo.bar, compare it against the value of the property md5, and set isEqual to either true or false, depending upon the result.

<checksum file="foo.bar" property="${md5}" verifyProperty="isEqual"/>

Example 7

Just like Example 1, but generates a .MD5 file for every file that begins with the name foo.

<checksum>
  <fileset dir=".">
    <include name="foo*"/>
  </fileset>
</checksum>

Example 8

Just like Example 4, but only sets isChecksumEqual to true, if the checksum matches—it will never be set to false. This example demonstrates use with the condition task.

<condition property="isChecksumEqual">
  <checksum>
    <fileset dir=".">
      <include name="foo.bar"/>
    </fileset>
  </checksum>
</condition>

Note

When working with more than one file, if condition and/or verifyproperty is used, the result will be true only if the checksums matched correctly for all files being considered.