View Javadoc
1   /*
2    * GordianKnot: Security Suite
3    * Copyright 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  
18  package io.github.tonywasher.joceanus.gordianknot.impl.bc.sign;
19  
20  import io.github.tonywasher.joceanus.gordianknot.api.base.GordianException;
21  import io.github.tonywasher.joceanus.gordianknot.api.digest.spec.GordianDigestSpec;
22  import io.github.tonywasher.joceanus.gordianknot.api.sign.spec.GordianSignatureSpec;
23  import io.github.tonywasher.joceanus.gordianknot.impl.bc.digest.BouncyDigest;
24  import io.github.tonywasher.joceanus.gordianknot.impl.bc.keypair.BouncyKeyPair;
25  import io.github.tonywasher.joceanus.gordianknot.impl.core.base.GordianBaseFactory;
26  import io.github.tonywasher.joceanus.gordianknot.impl.core.sign.GordianCoreSignature;
27  import io.github.tonywasher.joceanus.gordianknot.impl.core.spec.sign.GordianCoreSignatureSpec;
28  import org.bouncycastle.crypto.Digest;
29  import org.bouncycastle.crypto.digests.NullDigest;
30  
31  /**
32   * Digest signature base.
33   */
34  public abstract class BouncyDigestSignature
35          extends GordianCoreSignature {
36      /**
37       * The Digest.
38       */
39      private BouncyDigest theDigest;
40  
41      /**
42       * Constructor.
43       *
44       * @param pFactory the factory
45       * @param pSpec    the signatureSpec.
46       * @throws GordianException on error
47       */
48      BouncyDigestSignature(final GordianBaseFactory pFactory,
49                            final GordianSignatureSpec pSpec) throws GordianException {
50          super(pFactory, pSpec);
51          theDigest = pSpec.getSignatureSpec() == null
52                  ? new BouncyDigest(null, new NullDigest())
53                  : (BouncyDigest) getDigestFactory().createDigest(((GordianCoreSignatureSpec) pSpec).getDigestSpec());
54      }
55  
56      /**
57       * Constructor.
58       *
59       * @param pFactory the factory
60       * @param pSpec    the signatureSpec.
61       * @param pDigest  the digest
62       */
63      BouncyDigestSignature(final GordianBaseFactory pFactory,
64                            final GordianSignatureSpec pSpec,
65                            final Digest pDigest) {
66          super(pFactory, pSpec);
67          theDigest = new BouncyDigest(((GordianCoreSignatureSpec) pSpec).getDigestSpec(), pDigest);
68      }
69  
70      /**
71       * Set the digest.
72       *
73       * @param pSpec the digestSpec.
74       * @throws GordianException on error
75       */
76      protected void setDigest(final GordianDigestSpec pSpec) throws GordianException {
77          theDigest = pSpec == null
78                  ? new BouncyDigest(null, new NullDigest())
79                  : (BouncyDigest) getDigestFactory().createDigest(pSpec);
80      }
81  
82      @Override
83      public void update(final byte[] pBytes,
84                         final int pOffset,
85                         final int pLength) {
86          theDigest.update(pBytes, pOffset, pLength);
87      }
88  
89      @Override
90      public void update(final byte pByte) {
91          theDigest.update(pByte);
92      }
93  
94      @Override
95      public void reset() {
96          theDigest.reset();
97      }
98  
99      /**
100      * Obtain the calculated digest.
101      *
102      * @return the digest.
103      */
104     protected byte[] getDigest() {
105         return theDigest.finish();
106     }
107 
108     @Override
109     protected BouncyKeyPair getKeyPair() {
110         return (BouncyKeyPair) super.getKeyPair();
111     }
112 }