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.impl.bc;
18  
19  import io.github.tonywasher.joceanus.gordianknot.api.digest.GordianDigestSpec;
20  import io.github.tonywasher.joceanus.gordianknot.impl.core.digest.GordianCoreDigest;
21  import org.bouncycastle.crypto.Digest;
22  
23  /**
24   * BouncyCastle Digest.
25   */
26  public class BouncyDigest
27          extends GordianCoreDigest {
28      /**
29       * Digest.
30       */
31      private final Digest theDigest;
32  
33      /**
34       * Constructor.
35       *
36       * @param pDigestSpec the digestSpec
37       * @param pDigest     the digest
38       */
39      BouncyDigest(final GordianDigestSpec pDigestSpec,
40                   final Digest pDigest) {
41          super(pDigestSpec);
42          theDigest = pDigest;
43      }
44  
45      /**
46       * Obtain Underlying Digest.
47       *
48       * @return the underlying digest
49       */
50      Digest getDigest() {
51          return theDigest;
52      }
53  
54      @Override
55      public int getDigestSize() {
56          return getDigestSpec() != null
57                  ? super.getDigestSize()
58                  : theDigest.getDigestSize();
59      }
60  
61      @Override
62      public void doUpdate(final byte[] pBytes,
63                           final int pOffset,
64                           final int pLength) {
65          theDigest.update(pBytes, pOffset, pLength);
66      }
67  
68      @Override
69      public void update(final byte pByte) {
70          theDigest.update(pByte);
71      }
72  
73      @Override
74      public void reset() {
75          theDigest.reset();
76      }
77  
78      @Override
79      public byte[] finish() {
80          final byte[] myResult = new byte[getDigestSize()];
81          doFinish(myResult, 0);
82          return myResult;
83      }
84  
85      @Override
86      public int doFinish(final byte[] pBuffer,
87                          final int pOffset) {
88          return theDigest.doFinal(pBuffer, pOffset);
89      }
90  }