title: MessageDigest.update() before digest()?
body: <p>I have found the following example code (somewhere on the web, can't remember where):</p>
  
  <pre><code>public static byte[] produce(final byte[] data)
          throws NoSuchAlgorithmException{
      MessageDigest digest = MessageDigest.getInstance(ALGORITHM);
      digest.reset();
      digest.update(data);
      byte[] ret = digest.digest(data);
      for (int i = 0; i &lt; HASH_ITERATIONS; i++) {
          digest.reset();
          ret = digest.digest(ret);
      }
      return ret;
  }
  </code></pre>
  
  <p>What bothers me is that the docs says that digest() already calls update() on its own with the passed input:</p>
  
  <blockquote>
    <p>Performs a final update on the digest using the specified array of
    bytes, then completes the digest computation. That is, this method
    first calls update(input), passing the input array to the update
    method, then calls digest().</p>
  </blockquote>
  
  <p>Additionally, after the call to update(), the MessageDigest object's <code>state</code> becomes <code>1</code>.<br/>
  After a call to digest() (next in the code) it returns to be simply <code>0</code>.<br/>
  Finally, when stepping through while debugging, I can clearly see that the data is being manipulated.</p>
  
  <p>So, is it safe to remove the call to update()?</p>
  
  <p>Note that I wouldn't bother anyone with that question if it weren't a big security issue.</p>