Skip to content
This repository was archived by the owner on Apr 10, 2025. It is now read-only.

Commit cb41f24

Browse files
authored
Refactor GeneXusCryptography Module (#80)
* Refactor GeneXusCryptography Module issue#96965 * Hexadecimal output to upper case * Hash results to uppercase
1 parent 8764424 commit cb41f24

File tree

21 files changed

+809
-794
lines changed

21 files changed

+809
-794
lines changed

GeneXusCryptography/src/main/java/com/genexus/cryptography/asymmetric/AsymmetricSigner.java

Lines changed: 43 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -32,25 +32,15 @@ public AsymmetricSigner() {
3232

3333
@Override
3434
public String doSign(PrivateKeyManager key, String hashAlgorithm, String plainText) {
35-
/******** INPUT VERIFICATION - BEGIN ********/
36-
if(key == null)
37-
{
38-
error.setError("AE001", "Private key cannot be null");
39-
return "";
40-
}
41-
if(hashAlgorithm == null || hashAlgorithm.length() == 0 || SecurityUtils.compareStrings("", hashAlgorithm))
42-
{
43-
error.setError("AE002", "HashAlgorithm cannot be empty value; use HashAlgorithm domain");
44-
return "";
45-
}
46-
if(plainText == null || plainText.length() == 0 || SecurityUtils.compareStrings("", plainText))
47-
{
48-
error.setError("AE003", "The plainText value to sign cannot be empty");
49-
return "";
50-
}
51-
/******** INPUT VERIFICATION - END ********/
52-
35+
this.error.cleanError();
5336

37+
/*******INPUT VERIFICATION - BEGIN*******/
38+
SecurityUtils.validateObjectInput("key", key, this.error);
39+
SecurityUtils.validateStringInput("hashAlgorithm", hashAlgorithm, this.error);
40+
SecurityUtils.validateStringInput("plainText", plainText, this.error);
41+
if(this.hasError()) { return "";};
42+
/*******INPUT VERIFICATION - END*******/
43+
5444
EncodingUtil eu = new EncodingUtil();
5545
byte[] inputText = eu.getBytes(plainText);
5646
if (eu.hasError()) {
@@ -63,30 +53,21 @@ public String doSign(PrivateKeyManager key, String hashAlgorithm, String plainTe
6353
result = sign(key, hashAlgorithm, inputStream);
6454
}catch(Exception e)
6555
{
66-
error.setError("AE004", e.getMessage());
56+
error.setError("AS001", e.getMessage());
6757
}
6858
return result;
6959
}
7060

7161
@Override
7262
public String doSignFile(PrivateKeyManager key, String hashAlgorithm, String path) {
73-
/******** INPUT VERIFICATION - BEGIN ********/
74-
if(key == null)
75-
{
76-
error.setError("AE005", "Private key cannot be null");
77-
return "";
78-
}
79-
if(hashAlgorithm == null || hashAlgorithm.length() == 0 || SecurityUtils.compareStrings("", hashAlgorithm))
80-
{
81-
error.setError("AE006", "HashAlgorithm cannot be empty value; use HashAlgorithm domain");
82-
return "";
83-
}
84-
if(path == null || path.length() == 0 || SecurityUtils.compareStrings("", path))
85-
{
86-
error.setError("AE007", "The path value of the file to sign cannot be empty");
87-
return "";
88-
}
89-
/******** INPUT VERIFICATION - END ********/
63+
this.error.cleanError();
64+
65+
/*******INPUT VERIFICATION - BEGIN*******/
66+
SecurityUtils.validateObjectInput("key", key, this.error);
67+
SecurityUtils.validateStringInput("hashAlgorithm", hashAlgorithm, this.error);
68+
SecurityUtils.validateStringInput("path", path, this.error);
69+
if(this.hasError()) { return "";}
70+
/*******INPUT VERIFICATION - END*******/
9071

9172
String result = "";
9273
try(InputStream input = SecurityUtils.getFileStream(path, this.error))
@@ -98,32 +79,22 @@ public String doSignFile(PrivateKeyManager key, String hashAlgorithm, String pat
9879
result = sign(key, hashAlgorithm, input);
9980
}catch(Exception e)
10081
{
101-
error.setError("AE008", e.getMessage());
82+
error.setError("AS002", e.getMessage());
10283
}
10384
return result;
10485
}
10586

10687
@Override
10788
public boolean doVerify(CertificateX509 cert, String plainText, String signature) {
108-
/******** INPUT VERIFICATION - BEGIN ********/
109-
if(cert == null)
110-
{
111-
error.setError("AE009", "Certificate cannot be null");
112-
return false;
113-
}
114-
if(plainText == null || plainText.length() == 0 || SecurityUtils.compareStrings("", plainText))
115-
{
116-
error.setError("AE010", "The plainText value to verify cannot be empty");
117-
return false;
118-
}
119-
if(signature == null || signature.length() == 0 || SecurityUtils.compareStrings("", signature))
120-
{
121-
error.setError("AE011", "The signature value to verify cannot be empty");
122-
return false;
123-
}
124-
/******** INPUT VERIFICATION - END ********/
125-
89+
this.error.cleanError();
12690

91+
/*******INPUT VERIFICATION - BEGIN*******/
92+
SecurityUtils.validateObjectInput("cert", cert, this.error);
93+
SecurityUtils.validateStringInput("plainText", plainText, this.error);
94+
SecurityUtils.validateStringInput("signature", signature, this.error);
95+
if(this.hasError()) { return false;}
96+
/*******INPUT VERIFICATION - END*******/
97+
12798
EncodingUtil eu = new EncodingUtil();
12899
byte[] inputText = eu.getBytes(plainText);
129100
if (eu.hasError()) {
@@ -136,30 +107,21 @@ public boolean doVerify(CertificateX509 cert, String plainText, String signature
136107
result = verify(cert, inputStream, signature);
137108
}catch(Exception e)
138109
{
139-
error.setError("AE012", e.getMessage() );
110+
error.setError("AS003", e.getMessage() );
140111
}
141112
return result;
142113
}
143114

144115
@Override
145116
public boolean doVerifyFile(CertificateX509 cert, String path, String signature) {
146-
/******** INPUT VERIFICATION - BEGIN ********/
147-
if(cert == null)
148-
{
149-
error.setError("AE013", "Certificate cannot be null");
150-
return false;
151-
}
152-
if(path == null || path.length() == 0 || SecurityUtils.compareStrings("", path))
153-
{
154-
error.setError("AE014", "The path value of the faile to verify cannot be empty");
155-
return false;
156-
}
157-
if(signature == null || signature.length() == 0 || SecurityUtils.compareStrings("", signature))
158-
{
159-
error.setError("AE015", "The signature value to verify cannot be empty");
160-
return false;
161-
}
162-
/******** INPUT VERIFICATION - END ********/
117+
this.error.cleanError();
118+
119+
/*******INPUT VERIFICATION - BEGIN*******/
120+
SecurityUtils.validateObjectInput("cert", cert, this.error);
121+
SecurityUtils.validateStringInput("path", path, this.error);
122+
SecurityUtils.validateStringInput("signature", signature, this.error);
123+
if(this.hasError()) { return false;}
124+
/*******INPUT VERIFICATION - END*******/
163125

164126
boolean result = false;
165127
try(InputStream input = SecurityUtils.getFileStream(path, this.error))
@@ -170,13 +132,13 @@ public boolean doVerifyFile(CertificateX509 cert, String path, String signature)
170132
result = verify(cert, input, signature);
171133
}catch(Exception e)
172134
{
173-
error.setError("AE016", e.getMessage());
135+
error.setError("AS004", e.getMessage());
174136
}
175137
return result;
176138
}
177139

178140
/******** EXTERNAL OBJECT PUBLIC METHODS - END ********/
179-
141+
180142
private String sign(PrivateKey key, String hashAlgorithm, InputStream input) {
181143
PrivateKeyManager keyMan = (PrivateKeyManager) key;
182144
if (keyMan.hasError()) {
@@ -195,14 +157,14 @@ private String sign(PrivateKey key, String hashAlgorithm, InputStream input) {
195157
try {
196158
outputBytes = signer.generateSignature();
197159
} catch (Exception e) {
198-
error.setError("AE01", e.getMessage());
160+
error.setError("AS005", e.getMessage());
199161
return "";
200162
}
201163
String result = "";
202164
try {
203165
result = Base64.toBase64String(outputBytes);
204166
} catch (Exception e) {
205-
error.setError("AE018", e.getMessage());
167+
error.setError("AS006", e.getMessage());
206168
return "";
207169
}
208170
return result;
@@ -232,19 +194,14 @@ private boolean verify(Certificate certificate, InputStream input, String signat
232194
try {
233195
signatureBytes = Base64.decode(signature);
234196
} catch (Exception e) {
235-
error.setError("AE019", e.getMessage());
236-
return false;
237-
}
238-
239-
if (signatureBytes == null || signatureBytes.length == 0) {
240-
this.error.setError("AE020", "Error reading signature");
197+
error.setError("AS007", e.getMessage());
241198
return false;
242199
}
243200
boolean result = false;
244201
try {
245202
result = signer.verifySignature(signatureBytes);
246203
} catch (Exception e) {
247-
error.setError("AE021", e.getMessage());
204+
error.setError("AS008", e.getMessage());
248205
return false;
249206
}
250207
return result;
@@ -256,7 +213,7 @@ private void setUpSigner(Signer signer, InputStream input, AsymmetricKeyParamete
256213
try {
257214
signer.init(toSign, asymmetricKeyParameter);
258215
} catch (Exception e) {
259-
error.setError("AE022", e.getMessage());
216+
error.setError("AS009", e.getMessage());
260217
return;
261218
}
262219
byte[] buffer = new byte[8192];
@@ -266,7 +223,7 @@ private void setUpSigner(Signer signer, InputStream input, AsymmetricKeyParamete
266223
signer.update(buffer, 0, n);
267224
}
268225
} catch (Exception e) {
269-
error.setError("AE023", e.getMessage());
226+
error.setError("AS010", e.getMessage());
270227
return;
271228
}
272229
}

GeneXusCryptography/src/main/java/com/genexus/cryptography/checksum/ChecksumCreator.java

Lines changed: 32 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
package com.genexus.cryptography.checksum;
22

3+
import java.io.ByteArrayInputStream;
4+
import java.io.InputStream;
5+
6+
import org.bouncycastle.util.encoders.Hex;
7+
38
import com.genexus.cryptography.checksum.utils.CRCParameters;
49
import com.genexus.cryptography.checksum.utils.ChecksumAlgorithm;
510
import com.genexus.cryptography.checksum.utils.ChecksumInputType;
@@ -17,6 +22,15 @@ public ChecksumCreator() {
1722
/********EXTERNAL OBJECT PUBLIC METHODS - BEGIN ********/
1823

1924
public String generateChecksum(String input, String inputType, String checksumAlgorithm) {
25+
this.error.cleanError();
26+
27+
/*******INPUT VERIFICATION - BEGIN*******/
28+
SecurityUtils.validateStringInput("input", input, this.error);
29+
SecurityUtils.validateStringInput("inputType", inputType, this.error);
30+
SecurityUtils.validateStringInput("checksumAlgorithm", checksumAlgorithm, this.error);
31+
if(this.hasError()) { return "";};
32+
/*******INPUT VERIFICATION - END*******/
33+
2034
ChecksumInputType chksumInputType = ChecksumInputType.getChecksumInputType(inputType, this.error);
2135
byte[] inputBytes = ChecksumInputType.getBytes(chksumInputType, input, this.error);
2236
if (this.hasError()) {
@@ -32,6 +46,16 @@ public String generateChecksum(String input, String inputType, String checksumAl
3246

3347
public boolean verifyChecksum(String input, String inputType, String checksumAlgorithm, String digest)
3448
{
49+
this.error.cleanError();
50+
51+
/*******INPUT VERIFICATION - BEGIN*******/
52+
SecurityUtils.validateStringInput("input", input, this.error);
53+
SecurityUtils.validateStringInput("inputType", inputType, this.error);
54+
SecurityUtils.validateStringInput("checksumAlgorithm", checksumAlgorithm, this.error);
55+
SecurityUtils.validateStringInput("digest", digest, this.error);
56+
if(this.hasError()) { return false;};
57+
/*******INPUT VERIFICATION - END*******/
58+
3559
String result = generateChecksum(input, inputType, checksumAlgorithm);
3660
if(SecurityUtils.compareStrings(result, "") || this.hasError())
3761
{
@@ -71,38 +95,24 @@ private String calculateHash(byte[] input, ChecksumAlgorithm checksumAlgorithm)
7195
return "";
7296
}
7397
Hashing hash = new Hashing();
74-
byte[] digest = hash.calculateHash(alg, input);
98+
byte[] digest = null;
99+
try (InputStream inputStream = new ByteArrayInputStream(input)) {
100+
digest = hash.calculateHash(alg, inputStream);
101+
} catch (Exception e) {
102+
error.setError("CH001", e.getMessage());
103+
return "";
104+
}
75105
if (hash.hasError()) {
76106
this.error = hash.getError();
77107
return "";
78108
}
79-
return toHexaString(digest);
109+
return Hex.toHexString(digest);
80110
}
81111

82112
private HashAlgorithm getHashAlgorithm(ChecksumAlgorithm checksumAlgorithm) {
83113
return HashAlgorithm.getHashAlgorithm(ChecksumAlgorithm.valueOf(checksumAlgorithm, this.error), this.error);
84114
}
85115

86-
private String toHexaString(byte[] digest) {
87-
88-
if (this.error.existsError()) {
89-
return "";
90-
}
91-
92-
StringBuilder sb = new StringBuilder();
93-
for (byte b : digest) {
94-
sb.append(String.format("%02X ", b));
95-
}
96-
String result = sb.toString().replaceAll("\\s", "");
97-
if (result == null || result.length() == 0) {
98-
this.error.setError("HS001", "Error encoding hexa");
99-
return "";
100-
}
101-
102-
return result.trim().toUpperCase();
103-
104-
}
105-
106116
private long calculateCRC(byte[] input, CRCParameters parms) {
107117

108118
long curValue = parms.getInit();

GeneXusCryptography/src/main/java/com/genexus/cryptography/checksum/utils/ChecksumAlgorithm.java

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,15 @@ public enum ChecksumAlgorithm {
99
CRC16_DECT_X, CRC16_DNP, CRC16_EN_13757, CRC16_GENIBUS, CRC16_MAXIM, CRC16_MCRF4XX, CRC16_RIELLO, CRC16_T10_DIF,
1010
CRC16_TELEDISK, CRC16_TMS_37157, CRC16_USB, CRC_A, CRC16_KERMIT, CRC16_MODBUS, CRC16_X_25, CRC16_XMODEM, CRC32,
1111
CRC32_BZIP2, CRC32C, CRC32D, CRC32_MPEG_2, CRC32_POSIX, CRC32Q, CRC32_JAMCRC, CRC32_XFER, MD5, SHA1, SHA256,
12-
SHA512,;
12+
SHA512,NONE;
1313

1414
public static ChecksumAlgorithm getChecksumAlgorithm(String checksumAlgorithm, Error error) {
15+
if(error == null) return ChecksumAlgorithm.NONE;
16+
if (checksumAlgorithm == null)
17+
{
18+
error.setError("CHA04", "Unrecognized checksum algorithm");
19+
return ChecksumAlgorithm.NONE;
20+
}
1521
switch (checksumAlgorithm.toUpperCase().trim()) {
1622
case "CRC8":
1723
return ChecksumAlgorithm.CRC8;
@@ -106,12 +112,13 @@ public static ChecksumAlgorithm getChecksumAlgorithm(String checksumAlgorithm, E
106112
case "SHA512":
107113
return ChecksumAlgorithm.SHA512;
108114
default:
109-
error.setError("CA001", "Unrecognized checksum algorithm");
115+
error.setError("CHA01", "Unrecognized checksum algorithm");
110116
return null;
111117
}
112118
}
113119

114120
public static String valueOf(ChecksumAlgorithm checksumAlgorithm, Error error) {
121+
if (error == null) return null;
115122
switch (checksumAlgorithm) {
116123
case CRC8:
117124
return "CRC8";
@@ -206,7 +213,7 @@ public static String valueOf(ChecksumAlgorithm checksumAlgorithm, Error error) {
206213
case SHA512:
207214
return "SHA512";
208215
default:
209-
error.setError("CA002", "Unrecognized checksum algorithm");
216+
error.setError("CHA02", "Unrecognized checksum algorithm");
210217
return null;
211218
}
212219
}
@@ -227,6 +234,7 @@ public static boolean isHash(ChecksumAlgorithm checksumAlgorithm)
227234

228235
public static CRCParameters getParameters(ChecksumAlgorithm checksumAlgorithm, Error error)
229236
{
237+
if (error == null) return new CRCParameters(0, 0x00, 0x00, false, false, 0x00);
230238
switch (checksumAlgorithm) {
231239
case CRC8:
232240
return new CRCParameters(8, 0x07, 0x00, false, false, 0x00);
@@ -313,7 +321,7 @@ public static CRCParameters getParameters(ChecksumAlgorithm checksumAlgorithm, E
313321
case CRC32_XFER:
314322
return new CRCParameters(32, 0x000000AF, 0x00000000, false, false, 0x0000000);
315323
default:
316-
error.setError("CA004", "Unrecognized checksum algorithm");
324+
error.setError("CHA03", "Unrecognized checksum algorithm");
317325
return null;
318326
}
319327
}

0 commit comments

Comments
 (0)