1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package io.github.tonywasher.joceanus.gordianknot.api.encrypt;
18
19 import io.github.tonywasher.joceanus.gordianknot.api.base.GordianLength;
20 import io.github.tonywasher.joceanus.gordianknot.api.digest.GordianDigestSpec;
21 import io.github.tonywasher.joceanus.gordianknot.api.digest.GordianDigestSpecBuilder;
22
23 import java.util.ArrayList;
24 import java.util.List;
25 import java.util.Objects;
26
27
28
29
30 public class GordianSM2EncryptionSpec {
31
32
33
34 private final GordianSM2EncryptionType theType;
35
36
37
38
39 private final GordianDigestSpec theDigest;
40
41
42
43
44 private final boolean isValid;
45
46
47
48
49 private String theName;
50
51
52
53
54
55
56
57 GordianSM2EncryptionSpec(final GordianSM2EncryptionType pType,
58 final GordianDigestSpec pDigest) {
59
60 theType = pType;
61 theDigest = pDigest;
62 isValid = checkValidity();
63 }
64
65
66
67
68
69
70
71 public static GordianSM2EncryptionSpec c1c2c3(final GordianDigestSpec pSpec) {
72 return new GordianSM2EncryptionSpec(GordianSM2EncryptionType.C1C2C3, pSpec);
73 }
74
75
76
77
78
79
80
81 public static GordianSM2EncryptionSpec c1c3c2(final GordianDigestSpec pSpec) {
82 return new GordianSM2EncryptionSpec(GordianSM2EncryptionType.C1C3C2, pSpec);
83 }
84
85
86
87
88
89
90 public GordianSM2EncryptionType getEncryptionType() {
91 return theType;
92 }
93
94
95
96
97
98
99 public GordianDigestSpec getDigestSpec() {
100 return theDigest;
101 }
102
103
104
105
106
107
108 public boolean isValid() {
109 return isValid;
110 }
111
112
113
114
115
116
117 private boolean checkValidity() {
118 return theType != null
119 && theDigest != null
120 && isDigestSupported();
121 }
122
123
124
125
126
127
128 private boolean isDigestSupported() {
129 switch (theDigest.getDigestType()) {
130 case SHA2:
131 return !theDigest.isSha2Hybrid();
132 case WHIRLPOOL:
133 case SM3:
134 return true;
135 case BLAKE2:
136 return theDigest.getDigestLength().equals(theDigest.getDigestState().getLength());
137 default:
138 return false;
139 }
140 }
141
142 @Override
143 public String toString() {
144
145 if (theName == null) {
146
147 if (isValid) {
148
149 theName = theType.toString() + GordianEncryptorSpec.SEP + theDigest.toString();
150 } else {
151
152 theName = "InvalidEncryptorSpec: " + theType + ":" + theDigest;
153 }
154 }
155
156
157 return theName;
158 }
159
160 @Override
161 public boolean equals(final Object pThat) {
162
163 if (this == pThat) {
164 return true;
165 }
166 if (pThat == null) {
167 return false;
168 }
169
170
171 if (pThat.getClass() != this.getClass()) {
172 return false;
173 }
174
175
176 final GordianSM2EncryptionSpec myThat = (GordianSM2EncryptionSpec) pThat;
177
178
179 return theType == myThat.getEncryptionType()
180 && Objects.equals(theDigest, myThat.getDigestSpec());
181 }
182
183 @Override
184 public int hashCode() {
185 return Objects.hash(theType, theDigest);
186 }
187
188
189
190
191
192
193 public static List<GordianSM2EncryptionSpec> listPossibleSpecs() {
194
195 final List<GordianSM2EncryptionSpec> mySpecs = new ArrayList<>();
196
197
198 for (GordianSM2EncryptionType myType : GordianSM2EncryptionType.values()) {
199 mySpecs.add(new GordianSM2EncryptionSpec(myType, GordianDigestSpecBuilder.sm3()));
200 mySpecs.add(new GordianSM2EncryptionSpec(myType, GordianDigestSpecBuilder.sha2(GordianLength.LEN_224)));
201 mySpecs.add(new GordianSM2EncryptionSpec(myType, GordianDigestSpecBuilder.sha2(GordianLength.LEN_256)));
202 mySpecs.add(new GordianSM2EncryptionSpec(myType, GordianDigestSpecBuilder.sha2(GordianLength.LEN_384)));
203 mySpecs.add(new GordianSM2EncryptionSpec(myType, GordianDigestSpecBuilder.sha2(GordianLength.LEN_512)));
204 mySpecs.add(new GordianSM2EncryptionSpec(myType, GordianDigestSpecBuilder.blake2s(GordianLength.LEN_256)));
205 mySpecs.add(new GordianSM2EncryptionSpec(myType, GordianDigestSpecBuilder.blake2b(GordianLength.LEN_512)));
206 mySpecs.add(new GordianSM2EncryptionSpec(myType, GordianDigestSpecBuilder.whirlpool()));
207 }
208
209
210 return mySpecs;
211 }
212
213
214
215
216 public enum GordianSM2EncryptionType {
217
218
219
220 C1C2C3,
221
222
223
224
225 C1C3C2;
226 }
227 }