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.keyset;
18
19 import io.github.tonywasher.joceanus.gordianknot.api.base.GordianLength;
20 import io.github.tonywasher.joceanus.gordianknot.api.key.GordianKeyLengths;
21
22 import java.util.Objects;
23
24 /**
25 * KeySet Specification.
26 */
27 public class GordianKeySetSpec {
28 /**
29 * Minimum Cipher Steps.
30 */
31 public static final Integer MINIMUM_CIPHER_STEPS = 3;
32
33 /**
34 * Maximum Cipher Steps.
35 */
36 public static final Integer MAXIMUM_CIPHER_STEPS = 6;
37
38 /**
39 * Default Cipher Steps.
40 */
41 public static final Integer DEFAULT_CIPHER_STEPS = 4;
42
43 /**
44 * Default KeyLength.
45 */
46 public static final GordianLength DEFAULT_KEYLEN = GordianLength.LEN_256;
47
48 /**
49 * KeyLength.
50 */
51 private final GordianLength theKeyLength;
52
53 /**
54 * Number of CipherSteps.
55 */
56 private final int theCipherSteps;
57
58 /**
59 * Is the Spec valid?.
60 */
61 private final boolean isValid;
62
63 /**
64 * Constructor.
65 */
66 public GordianKeySetSpec() {
67 this(DEFAULT_KEYLEN);
68 }
69
70 /**
71 * Constructor.
72 *
73 * @param pKeyLen the keyLength.
74 */
75 public GordianKeySetSpec(final GordianLength pKeyLen) {
76 this(pKeyLen, DEFAULT_CIPHER_STEPS);
77 }
78
79 /**
80 * Constructor.
81 *
82 * @param pKeyLen the keyLength.
83 * @param pNumSteps the number of cipherSteps
84 */
85 public GordianKeySetSpec(final GordianLength pKeyLen,
86 final int pNumSteps) {
87 theKeyLength = pKeyLen;
88 theCipherSteps = pNumSteps;
89 isValid = validateKeySetSpec();
90 }
91
92 /**
93 * Is the Spec valid?
94 *
95 * @return true/false
96 */
97 public boolean isValid() {
98 return isValid;
99 }
100
101 /**
102 * Obtain the keyLength.
103 *
104 * @return the keyLength
105 */
106 public GordianLength getKeyLength() {
107 return theKeyLength;
108 }
109
110 /**
111 * Obtain the number of Cipher Steps.
112 *
113 * @return the # of cipher steps
114 */
115 public int getCipherSteps() {
116 return theCipherSteps;
117 }
118
119 /**
120 * Validate the Parameters.
121 *
122 * @return valid true/false
123 */
124 private boolean validateKeySetSpec() {
125 /* Check keyLength */
126 if (theKeyLength == null
127 || !GordianKeyLengths.isSupportedLength(theKeyLength)) {
128 return false;
129 }
130
131 /* Check cipher steps is in range */
132 return !(theCipherSteps < MINIMUM_CIPHER_STEPS
133 || theCipherSteps > MAXIMUM_CIPHER_STEPS);
134 }
135
136 @Override
137 public boolean equals(final Object pThat) {
138 /* Handle the trivial cases */
139 if (this == pThat) {
140 return true;
141 }
142 if (pThat == null) {
143 return false;
144 }
145
146 /* Check Length and cipherSteps */
147 return pThat instanceof GordianKeySetSpec myThat
148 && theKeyLength == myThat.getKeyLength()
149 && theCipherSteps == myThat.getCipherSteps();
150 }
151
152 @Override
153 public int hashCode() {
154 return Objects.hash(theKeyLength, theCipherSteps);
155 }
156
157 @Override
158 public String toString() {
159 return "KeySet" + theKeyLength + "-" + theCipherSteps;
160 }
161 }