View Javadoc
1   /*
2    * GordianKnot: Security Suite
3    * Copyright 2012-2026. Tony Washer
4    *
5    * Licensed under the Apache License, Version 2.0 (the "License"); you may not
6    * use this file except in compliance with the License.  You may obtain a copy
7    * of the License at
8    *
9    *   http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
14   * License for the specific language governing permissions and limitations under
15   * the License.
16   */
17  package io.github.tonywasher.joceanus.gordianknot.api.digest;
18  
19  import io.github.tonywasher.joceanus.gordianknot.api.base.GordianConsumer;
20  import io.github.tonywasher.joceanus.gordianknot.api.base.GordianException;
21  import io.github.tonywasher.joceanus.gordianknot.api.digest.spec.GordianDigestSpec;
22  
23  /**
24   * GordianKnot interface for Message Digests.
25   */
26  public interface GordianDigest
27          extends GordianConsumer {
28      /**
29       * Obtain DigestSpec.
30       *
31       * @return the digestSpec
32       */
33      GordianDigestSpec getDigestSpec();
34  
35      /**
36       * Obtain the digest size.
37       *
38       * @return the digest size
39       */
40      int getDigestSize();
41  
42      /**
43       * Calculate the digest.
44       *
45       * @return the digest
46       */
47      byte[] finish();
48  
49      /**
50       * Calculate the Digest, and return it in the buffer provided.
51       *
52       * @param pBuffer the buffer to return the digest in.
53       * @param pOffset the offset in the buffer to store the digest.
54       * @return the number of bytes placed into buffer
55       * @throws GordianException on error
56       */
57      int finish(byte[] pBuffer,
58                 int pOffset) throws GordianException;
59  
60      /**
61       * Update the digest, calculate and reset it.
62       *
63       * @param pBytes the bytes to update with.
64       * @return the digest
65       */
66      default byte[] finish(final byte[] pBytes) {
67          update(pBytes);
68          return finish();
69      }
70  }