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.mac;
18  
19  import io.github.tonywasher.joceanus.gordianknot.api.base.GordianLength;
20  
21  /**
22   * SipHashSpec.
23   */
24  public enum GordianSipHashSpec {
25      /**
26       * SipHash-2-4.
27       */
28      SIPHASH_2_4(GordianLength.LEN_64, 2),
29  
30      /**
31       * SipHash128-2-4.
32       */
33      SIPHASH128_2_4(GordianLength.LEN_64, 4),
34  
35      /**
36       * SipHash-4-8.
37       */
38      SIPHASH_4_8(GordianLength.LEN_128, 2),
39  
40      /**
41       * SipHash128-4-8.
42       */
43      SIPHASH128_4_8(GordianLength.LEN_128, 4);
44  
45      /**
46       * The output length.
47       */
48      private final GordianLength theOutLen;
49  
50      /**
51       * The number of compression rounds.
52       */
53      private final int theCompression;
54  
55      /**
56       * The name of the hash.
57       */
58      private String theName;
59  
60      /**
61       * Constructor.
62       *
63       * @param pOutLen      the output length
64       * @param pCompression the number of compression rounds
65       */
66      GordianSipHashSpec(final GordianLength pOutLen,
67                         final int pCompression) {
68          theOutLen = pOutLen;
69          theCompression = pCompression;
70      }
71  
72      /**
73       * Obtain the output length.
74       *
75       * @return the length
76       */
77      public GordianLength getOutLength() {
78          return theOutLen;
79      }
80  
81      /**
82       * Is this a long output?
83       *
84       * @return true/false
85       */
86      public boolean isLong() {
87          return theOutLen == GordianLength.LEN_128;
88      }
89  
90      /**
91       * Obtain the number of compression rounds.
92       *
93       * @return the number of rounds
94       */
95      public int getCompression() {
96          return theCompression;
97      }
98  
99      /**
100      * Obtain the number of finalization rounds.
101      *
102      * @return the number of rounds
103      */
104     public int getFinalisation() {
105         return theCompression << 1;
106     }
107 
108     @Override
109     public String toString() {
110         /* If we have not yet loaded the name */
111         if (theName == null) {
112             /* Build the name */
113             theName = "SipHash";
114             if (GordianLength.LEN_128 == theOutLen) {
115                 theName += theOutLen;
116             }
117             theName += GordianMacSpec.SEP + getCompression()
118                     + GordianMacSpec.SEP + getFinalisation();
119         }
120 
121         /* return the name */
122         return theName;
123     }
124 }