You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

checksum.html 10 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. <!DOCTYPE html>
  2. <!--
  3. Licensed to the Apache Software Foundation (ASF) under one or more
  4. contributor license agreements. See the NOTICE file distributed with
  5. this work for additional information regarding copyright ownership.
  6. The ASF licenses this file to You under the Apache License, Version 2.0
  7. (the "License"); you may not use this file except in compliance with
  8. the License. You may obtain a copy of the License at
  9. https://www.apache.org/licenses/LICENSE-2.0
  10. Unless required by applicable law or agreed to in writing, software
  11. distributed under the License is distributed on an "AS IS" BASIS,
  12. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. See the License for the specific language governing permissions and
  14. limitations under the License.
  15. -->
  16. <html lang="en">
  17. <head>
  18. <link rel="stylesheet" type="text/css" href="../stylesheets/style.css">
  19. <title>Checksum Task</title>
  20. </head>
  21. <body>
  22. <h2 id="checksum">Checksum</h2>
  23. <h3>Description</h3>
  24. <p>Generates checksum for files. This task can also be used to perform checksum verifications.</p>
  25. <p>Note that many popular message digest functions&mdash;including MD5 and SHA-1&mdash;have been
  26. broken recently. If you are going to use the task to create checksums used in an environment where
  27. security is important, please take some time to investigate the algorithms offered by your JCE
  28. provider. Note also that some JCE providers like the one by <a href="https://www.bouncycastle.org/"
  29. target="_top">The Legion of the Bouncy Castle</a>,
  30. the <a href="https://www.gnu.org/software/gnu-crypto/" target="_top">GNU Crypto project</a>
  31. or <a href="https://jce.iaik.tugraz.at/sic/Products" target="_top">the Technical University Graz</a>
  32. offer more digest algorithms than those built-in into your JDK.</p>
  33. <p>Warning: the case of the extension is that of the algorithm used. If you ask
  34. for <samp>SHA1</samp>, you get a <samp>.SHA1</samp> extension; if you ask for <samp>sha1</samp>, you
  35. get a file ending in <samp>.sha1</samp>. The Java Crypto Engines are case-insensitive in matching
  36. algorithms, so choose a name to match your desired output extension, or set the <var>fileext</var>
  37. attribute. The names of common hashing algorithms can be located on
  38. the <a href="https://docs.oracle.com/javase/8/docs/technotes/guides/security/StandardNames.html#MessageDigest"
  39. target="_top">Cryptography Architecture Standard Algorithm Name Documentation</a></p>
  40. <h3>Parameters</h3>
  41. <table class="attr">
  42. <tr>
  43. <th scope="col">Attribute</th>
  44. <th scope="col">Description</th>
  45. <th scope="col">Required</th>
  46. </tr>
  47. <tr>
  48. <td>file</td>
  49. <td>The file to generate checksum for.</td>
  50. <td>Yes, unless at least one nested (filesystem-only) resource collection is specified.</td>
  51. </tr>
  52. <tr>
  53. <td>todir</td>
  54. <td>The root directory where checksums should be written.</td>
  55. <td>No; by default, checksum files will be written to the same directory as the original
  56. files. <em>since Apache Ant 1.6</em>
  57. </td>
  58. </tr>
  59. <tr>
  60. <td>algorithm</td>
  61. <td>Specifies the algorithm to be used to compute the checksum. Please check
  62. the <a href="https://docs.oracle.com/javase/8/docs/technotes/guides/security/StandardNames.html#MessageDigest"
  63. target="_top">documentation</a> for available algorithm names, like <q>SHA-1</q>
  64. or <q>SHA-512</q>.
  65. </td>
  66. <td>No; defaults to <q>MD5</q></td>
  67. </tr>
  68. <tr>
  69. <td>provider</td>
  70. <td>Specifies the provider of the algorithm.</td>
  71. <td>No</td>
  72. </tr>
  73. <tr>
  74. <td>fileext</td>
  75. <td>The generated checksum file's name will be the original filename with the <var>fileext</var>
  76. added to it.
  77. </td>
  78. <td>No; defaults to a <q>.</q> and the algorithm name being used</td>
  79. </tr>
  80. <tr>
  81. <td>property</td>
  82. <td>This attribute can mean two different things, it depends on the presence of
  83. the <var>verifyproperty</var> attribute.<br/><strong>If you don't set</strong>
  84. the <var>verifyproperty</var> attribute, property specifies the name of the property to be set
  85. with the generated checksum value.<br/><strong>If you set</strong>
  86. the <var>verifyproperty</var> attribute, property specifies the checksum you expect to be
  87. generated (the checksum itself, not a name of a property containing the checksum).<br/> This
  88. cannot be specified when <var>fileext</var> is being used or when the number of files for
  89. which checksums are to be generated is greater than 1.
  90. </td>
  91. <td>No</td>
  92. </tr>
  93. <tr>
  94. <td>pattern</td>
  95. <td>Specifies the pattern to use as a pattern suitable
  96. for <a href="https://docs.oracle.com/javase/8/docs/api/java/text/MessageFormat.html"
  97. target="_top">MessageFormat</a> where <code>{0}</code> is replaced with the checksum
  98. and <code>{1}</code> with the file name. <em>Since Ant 1.7.0</em><br/><em>Since Ant
  99. 1.8.2</em> <code>{2}</code> is replaced by the path of the file relative to the checksum file
  100. being written, <code>{3}</code> with the path of the file relative to the
  101. project's <var>basedir</var> and <code>{4}</code> with the absolute path of the file.</td>
  102. <td>No; default is <q>{0}</q></td>
  103. </tr>
  104. <tr>
  105. <td>format</td>
  106. <td>Specifies the pattern to use as one of a well-known format. Supported values are
  107. <table>
  108. <tr>
  109. <th scope="col">name</th>
  110. <th scope="col">pattern</th>
  111. <th scope="col">description</th>
  112. </tr>
  113. <tr>
  114. <td>CHECKSUM</td>
  115. <td><code>{0}</code></td>
  116. <td>only the checksum itself</td>
  117. </tr>
  118. <tr>
  119. <td>MD5SUM</td>
  120. <td><code>{0} *{1}</code></td>
  121. <td>the format of GNU textutils <code>md5sum</code></td>
  122. </tr>
  123. <tr>
  124. <td>SVF</td>
  125. <td><code>MD5 ({1}) = {0}</code></td>
  126. <td>the format of BSD <code>md5</code> command</td>
  127. </tr>
  128. </table>
  129. <em>Since Ant 1.7.0</em></td>
  130. <td>No; default is <q>CHECKSUM</q></td>
  131. </tr>
  132. <tr>
  133. <td>totalproperty</td>
  134. <td>If specified, this attribute specifies the name of the property that will hold a checksum of
  135. all the checksums and file paths. The individual checksums and the relative paths to the files
  136. within the resource collections in which they are defined will be used to compute this
  137. checksum. (The file separators in the paths will be converted to <q>/</q> before computation
  138. to ensure platform portability). <em>since Ant 1.6</em></td>
  139. <td>No</td>
  140. </tr>
  141. <tr>
  142. <td>forceoverwrite</td>
  143. <td>Overwrite existing files even if the destination files are newer.</td>
  144. <td>No; defaults to <q>no</q></td>
  145. </tr>
  146. <tr>
  147. <td>verifyproperty</td>
  148. <td>Specifies the name of the property to be set with <q>true</q> or <q>false</q> depending upon
  149. whether the generated checksum matches the existing checksum. When this is set, the generated
  150. checksum is not written to a file or property, but rather, the content of the file or property
  151. is used to check against the generated checksum.</td>
  152. <td>No</td>
  153. </tr>
  154. <tr>
  155. <td>readbuffersize</td>
  156. <td>The size of the buffer (in bytes) to use when reading a file.</td>
  157. <td>No; defaults to <q>8192</q>&mdash;you may get a better performance on big files if you
  158. increase this value</td>
  159. </tr>
  160. </table>
  161. <h3>Parameters specified as nested elements</h3>
  162. <h4>resource collections</h4>
  163. <p><a href="../Types/resources.html#collection">Resource collections</a> are used to select files
  164. for which checksums should be generated.</p>
  165. <h3>Examples</h3>
  166. <p><strong>Example 1</strong></p>
  167. <p>Generate a MD5 checksum for <samp>foo.bar</samp> and store the checksum in the destination
  168. file <samp>foo.bar.MD5</samp>. <samp>foo.bar.MD5</samp> is overwritten only if <samp>foo.bar</samp>
  169. is newer than itself.</p>
  170. <pre>&lt;checksum file=&quot;foo.bar&quot;/&gt;</pre>
  171. <p><strong>Example 2</strong></p>
  172. <p>Generate a MD5 checksum for <samp>foo.bar</samp> and store the checksum
  173. in <samp>foo.bar.MD5</samp>. If <samp>foo.bar.MD5</samp> already exists, it is overwritten.</p>
  174. <pre>&lt;checksum file=&quot;foo.bar&quot; forceOverwrite=&quot;yes&quot;/&gt;</pre>
  175. <p><strong>Example 3</strong></p>
  176. <p>Generate a MD5 checksum for <samp>foo.bar</samp> and store it in the project
  177. property <code>foobarMD5</code>.</p>
  178. <pre>&lt;checksum file=&quot;foo.bar&quot; property=&quot;foobarMD5&quot;/&gt;</pre>
  179. <p><strong>Example 4</strong></p>
  180. <p>Generate a MD5 checksum for <samp>foo.bar</samp>, compare it against <samp>foo.bar.MD5</samp> and
  181. set <code>isMD5ok</code> to either <q>true</q> or <q>false</q>, depending upon the result.</p>
  182. <pre>&lt;checksum file=&quot;foo.bar&quot; verifyProperty=&quot;isMD5ok&quot;/&gt;</pre>
  183. <p><strong>Example 5</strong></p>
  184. <p>Generate a SHA-512 checksum for <samp>foo.bar</samp> and store the checksum in the destination
  185. file <samp>foo.bar.asc</samp>. <samp>foo.bar.asc</samp> is overwritten only if <samp>foo.bar</samp>
  186. is newer than itself.</p>
  187. <pre>&lt;checksum file=&quot;foo.bar&quot; algorithm=&quot;SHA-512&quot; fileext=&quot;asc&quot;/&gt;</pre>
  188. <p><strong>Example 6</strong></p>
  189. <p>Generate a MD5 checksum for <samp>foo.bar</samp>, compare it against the value of the
  190. property <code>md5</code>, and set <code>isEqual</code> to either <q>true</q> or <q>false</q>,
  191. depending upon the result.</p>
  192. <pre>&lt;checksum file=&quot;foo.bar&quot; property=&quot;${md5}&quot; verifyProperty=&quot;isEqual&quot;/&gt;</pre>
  193. <p><strong>Example 7</strong></p>
  194. <p>Just like Example 1, but generates a <samp>.MD5</samp> file for every file that begins with the
  195. name <samp>foo</samp>.</p>
  196. <pre>
  197. &lt;checksum&gt;
  198. &lt;fileset dir=&quot;.&quot;&gt;
  199. &lt;include name=&quot;foo*&quot;/&gt;
  200. &lt;/fileset&gt;
  201. &lt;/checksum&gt;</pre>
  202. <p><strong>Example 8</strong></p>
  203. <p>Just like Example 4, but only sets <code>isChecksumEqual</code> to <q>true</q>, if the checksum
  204. matches&mdash;it will never be set to <q>false</q>. This example demonstrates use with
  205. the <code>condition</code> task.</p>
  206. <pre>
  207. &lt;condition property=&quot;isChecksumEqual&quot;&gt;
  208. &lt;checksum&gt;
  209. &lt;fileset dir=&quot;.&quot;&gt;
  210. &lt;include name=&quot;foo.bar&quot;/&gt;
  211. &lt;/fileset&gt;
  212. &lt;/checksum&gt;
  213. &lt;/condition&gt;</pre>
  214. <h3>Note</h3>
  215. <p>When working with more than one file, if condition and/or <var>verifyproperty</var> is used, the
  216. result will be true only if the checksums matched correctly for all files being considered.</p>
  217. </body>
  218. </html>