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.api.digest.spec;
19  
20  import io.github.tonywasher.joceanus.gordianknot.api.base.GordianLength;
21  import io.github.tonywasher.joceanus.gordianknot.api.digest.spec.GordianDigestSubSpec.GordianDigestState;
22  
23  /**
24   * Digest Specification Builder.
25   */
26  public interface GordianDigestSpecBuilder {
27      /**
28       * Define DigestType.
29       *
30       * @param pType the type
31       * @return the Builder
32       */
33      GordianDigestSpecBuilder withType(GordianDigestType pType);
34  
35      /**
36       * Define DigestState.
37       *
38       * @param pState the state
39       * @return the Builder
40       */
41      GordianDigestSpecBuilder withState(GordianDigestState pState);
42  
43      /**
44       * Define DigestLength.
45       *
46       * @param pLength the length
47       * @return the Builder
48       */
49      GordianDigestSpecBuilder withLength(GordianLength pLength);
50  
51      /**
52       * Use Xof implementation.
53       *
54       * @return the Builder
55       */
56      GordianDigestSpecBuilder asXof();
57  
58      /**
59       * Build digestSpec.
60       *
61       * @return the DigestSpec
62       */
63      GordianDigestSpec build();
64  
65  
66      /**
67       * Create generic digest.
68       *
69       * @param pType the digestType
70       * @return the digestSpec
71       */
72      default GordianDigestSpec digest(final GordianDigestType pType) {
73          return withType(pType).build();
74      }
75  
76      /**
77       * Create generic digest.
78       *
79       * @param pType   the digestType
80       * @param pLength the length
81       * @return the digestSpec
82       */
83      default GordianDigestSpec digest(final GordianDigestType pType,
84                                       final GordianLength pLength) {
85          return withType(pType).withLength(pLength).build();
86      }
87  
88      /**
89       * Create generic digest.
90       *
91       * @param pType    the digestType
92       * @param pState   the state
93       * @param pLength  the length
94       * @param pXofMode asXof
95       * @return the digestSpec
96       */
97      default GordianDigestSpec digest(final GordianDigestType pType,
98                                       final GordianDigestState pState,
99                                       final GordianLength pLength,
100                                      final boolean pXofMode) {
101         withType(pType).withState(pState).withLength(pLength);
102         if (pXofMode) {
103             asXof();
104         }
105         return build();
106     }
107 
108     /**
109      * Create Md2.
110      *
111      * @return the digestSpec
112      */
113     default GordianDigestSpec md2() {
114         return withType(GordianDigestType.MD2).build();
115     }
116 
117     /**
118      * Create Md4.
119      *
120      * @return the digestSpec
121      */
122     default GordianDigestSpec md4() {
123         return withType(GordianDigestType.MD4).build();
124     }
125 
126     /**
127      * Create Md5.
128      *
129      * @return the digestSpec
130      */
131     default GordianDigestSpec md5() {
132         return withType(GordianDigestType.MD5).build();
133     }
134 
135     /**
136      * Create Sha1.
137      *
138      * @return the digestSpec
139      */
140     default GordianDigestSpec sha1() {
141         return withType(GordianDigestType.SHA1).build();
142     }
143 
144     /**
145      * Create sm3.
146      *
147      * @return the digestSpec
148      */
149     default GordianDigestSpec sm3() {
150         return withType(GordianDigestType.SM3).build();
151     }
152 
153     /**
154      * Create Whirlpool.
155      *
156      * @return the digestSpec
157      */
158     default GordianDigestSpec whirlpool() {
159         return withType(GordianDigestType.WHIRLPOOL).build();
160     }
161 
162     /**
163      * Create Tiger.
164      *
165      * @return the digestSpec
166      */
167     default GordianDigestSpec tiger() {
168         return withType(GordianDigestType.TIGER).build();
169     }
170 
171     /**
172      * Create sha2.
173      *
174      * @param pLength the length
175      * @return the digestSpec
176      */
177     default GordianDigestSpec sha2(final GordianLength pLength) {
178         return withType(GordianDigestType.SHA2).withLength(pLength).build();
179     }
180 
181     /**
182      * Create sha2.
183      *
184      * @param pState  the state
185      * @param pLength the length
186      * @return the digestSpec
187      */
188     default GordianDigestSpec sha2(final GordianDigestState pState,
189                                    final GordianLength pLength) {
190         return withType(GordianDigestType.SHA2).withState(pState).withLength(pLength).build();
191     }
192 
193     /**
194      * Create sha3.
195      *
196      * @param pLength the length
197      * @return the DigestSpec
198      */
199     default GordianDigestSpec sha3(final GordianLength pLength) {
200         return withType(GordianDigestType.SHA3).withLength(pLength).build();
201     }
202 
203     /**
204      * Create blake2s.
205      *
206      * @param pLength the length
207      * @return the digestSpec
208      */
209     default GordianDigestSpec blake2s(final GordianLength pLength) {
210         return withType(GordianDigestType.BLAKE2).withState(GordianDigestState.STATE256).withLength(pLength).build();
211     }
212 
213     /**
214      * Create blake2b.
215      *
216      * @param pLength the length
217      * @return the digestSpec
218      */
219     default GordianDigestSpec blake2b(final GordianLength pLength) {
220         return withType(GordianDigestType.BLAKE2).withState(GordianDigestState.STATE512).withLength(pLength).build();
221     }
222 
223     /**
224      * Create blake2.
225      *
226      * @param pState  the state
227      * @param pLength the length
228      * @return the digestSpec
229      */
230     default GordianDigestSpec blake2(final GordianDigestState pState,
231                                      final GordianLength pLength) {
232         return withType(GordianDigestType.BLAKE2).withState(pState).withLength(pLength).build();
233     }
234 
235     /**
236      * Create blake2Xs.
237      *
238      * @return the digestSpec
239      */
240     default GordianDigestSpec blake2Xs() {
241         return blake2X(GordianDigestState.STATE256);
242     }
243 
244     /**
245      * Create blake2Xb.
246      *
247      * @return the DigestSpec
248      */
249     default GordianDigestSpec blake2Xb() {
250         return blake2X(GordianDigestState.STATE512);
251     }
252 
253     /**
254      * Create blake2X.
255      *
256      * @param pState the state
257      * @return the digestSpec
258      */
259     default GordianDigestSpec blake2X(final GordianDigestState pState) {
260         return withType(GordianDigestType.BLAKE2).withState(pState).asXof().build();
261     }
262 
263     /**
264      * Create blake3.
265      *
266      * @param pLength the length
267      * @return the digestSpec
268      */
269     default GordianDigestSpec blake3(final GordianLength pLength) {
270         return withType(GordianDigestType.BLAKE3).withLength(pLength).build();
271     }
272 
273     /**
274      * Create gost.
275      *
276      * @return the digestSpec
277      */
278     default GordianDigestSpec gost() {
279         return withType(GordianDigestType.GOST).build();
280     }
281 
282     /**
283      * Create streebog.
284      *
285      * @param pLength the length
286      * @return the digestSpec
287      */
288     default GordianDigestSpec streebog(final GordianLength pLength) {
289         return withType(GordianDigestType.STREEBOG).withLength(pLength).build();
290     }
291 
292     /**
293      * Create skein.
294      *
295      * @param pLength the length
296      * @return the digestSpec
297      */
298     default GordianDigestSpec skein(final GordianLength pLength) {
299         return withType(GordianDigestType.SKEIN).withLength(pLength).build();
300     }
301 
302     /**
303      * Create skein.
304      *
305      * @param pState  the state
306      * @param pLength the length
307      * @return the digestSpec
308      */
309     default GordianDigestSpec skein(final GordianDigestState pState,
310                                     final GordianLength pLength) {
311         return withType(GordianDigestType.SKEIN).withState(pState).withLength(pLength).build();
312     }
313 
314     /**
315      * Create skeinX.
316      *
317      * @param pState the state
318      * @return the digestSpec
319      */
320     default GordianDigestSpec skeinX(final GordianDigestState pState) {
321         return withType(GordianDigestType.SKEIN).withState(pState).asXof().build();
322     }
323 
324     /**
325      * Create ripemd.
326      *
327      * @param pLength the length
328      * @return the digestSpec
329      */
330     default GordianDigestSpec ripemd(final GordianLength pLength) {
331         return withType(GordianDigestType.RIPEMD).withLength(pLength).build();
332     }
333 
334     /**
335      * Create shake128.
336      *
337      * @return the digestSpec
338      */
339     default GordianDigestSpec shake128() {
340         return withType(GordianDigestType.SHAKE).withState(GordianDigestState.STATE128).withLength(GordianLength.LEN_256).build();
341     }
342 
343     /**
344      * Create shake256.
345      *
346      * @return the digestSpec
347      */
348     default GordianDigestSpec shake256() {
349         return withType(GordianDigestType.SHAKE).withState(GordianDigestState.STATE256).withLength(GordianLength.LEN_512).build();
350     }
351 
352     /**
353      * Create kupyna.
354      *
355      * @param pLength the length
356      * @return the digestSpec
357      */
358     default GordianDigestSpec kupyna(final GordianLength pLength) {
359         return withType(GordianDigestType.KUPYNA).withLength(pLength).build();
360     }
361 
362     /**
363      * Create jh.
364      *
365      * @param pLength the length
366      * @return the digestSpec
367      */
368     default GordianDigestSpec jh(final GordianLength pLength) {
369         return withType(GordianDigestType.JH).withLength(pLength).build();
370     }
371 
372     /**
373      * Create groestl.
374      *
375      * @param pLength the length
376      * @return the digestSpec
377      */
378     default GordianDigestSpec groestl(final GordianLength pLength) {
379         return withType(GordianDigestType.GROESTL).withLength(pLength).build();
380     }
381 
382     /**
383      * Create cubeHash.
384      *
385      * @param pLength the length
386      * @return the digestSpec
387      */
388     default GordianDigestSpec cubeHash(final GordianLength pLength) {
389         return withType(GordianDigestType.CUBEHASH).withLength(pLength).build();
390     }
391 
392     /**
393      * Create kangaroo.
394      *
395      * @param pLength the length
396      * @return the digestSpec
397      */
398     default GordianDigestSpec kangaroo(final GordianLength pLength) {
399         return withType(GordianDigestType.KANGAROO).withState(GordianDigestState.STATE128).withLength(pLength).build();
400     }
401 
402     /**
403      * Create marsupimal.
404      *
405      * @param pLength the length
406      * @return the digestSpec
407      */
408     default GordianDigestSpec marsupimal(final GordianLength pLength) {
409         return withType(GordianDigestType.KANGAROO).withState(GordianDigestState.STATE256).withLength(pLength).build();
410     }
411 
412     /**
413      * Create haraka256.
414      *
415      * @return the digestSpec
416      */
417     default GordianDigestSpec haraka256() {
418         return withType(GordianDigestType.HARAKA).withState(GordianDigestState.STATE256).withLength(GordianLength.LEN_256).build();
419     }
420 
421     /**
422      * Create haraka512.
423      *
424      * @return the digestSpec
425      */
426     default GordianDigestSpec haraka512() {
427         return withType(GordianDigestType.HARAKA).withState(GordianDigestState.STATE512).withLength(GordianLength.LEN_256).build();
428     }
429 
430     /**
431      * Create ascon.
432      *
433      * @return the digestSpec
434      */
435     default GordianDigestSpec ascon() {
436         return withType(GordianDigestType.ASCON).build();
437     }
438 
439     /**
440      * Create asconDigestSpec.
441      *
442      * @return the DigestSpec
443      */
444     default GordianDigestSpec asconX() {
445         return withType(GordianDigestType.ASCON).asXof().build();
446     }
447 
448     /**
449      * Create ISAP.
450      *
451      * @return the digestSpec
452      */
453     default GordianDigestSpec isap() {
454         return withType(GordianDigestType.ISAP).build();
455     }
456 
457     /**
458      * Create photonBeetle.
459      *
460      * @return the digestSpec
461      */
462     default GordianDigestSpec photonBeetle() {
463         return withType(GordianDigestType.PHOTONBEETLE).build();
464     }
465 
466     /**
467      * Create romulus.
468      *
469      * @return the digestSpec
470      */
471     default GordianDigestSpec romulus() {
472         return withType(GordianDigestType.ROMULUS).build();
473     }
474 
475     /**
476      * Create sparkle.
477      *
478      * @param pLength the digest length
479      * @return the digestSpec
480      */
481     default GordianDigestSpec sparkle(final GordianLength pLength) {
482         return withType(GordianDigestType.SPARKLE).withLength(pLength).build();
483     }
484 
485     /**
486      * Create xoodyak.
487      *
488      * @return the digestSpec
489      */
490     default GordianDigestSpec xoodyak() {
491         return withType(GordianDigestType.XOODYAK).build();
492     }
493 }