@@ -20,14 +20,12 @@ This file is part of the iText (R) project.
20
20
You should have received a copy of the GNU Affero General Public License
21
21
along with this program. If not, see <https://www.gnu.org/licenses/>.
22
22
*/
23
- package com .itextpdf .signatures ;
23
+ package com .itextpdf .kernel . crypto ;
24
24
25
25
import com .itextpdf .bouncycastleconnector .BouncyCastleFactoryCreator ;
26
26
import com .itextpdf .commons .bouncycastle .IBouncyCastleFactory ;
27
- import com .itextpdf .signatures .exceptions .SignExceptionMessageConstant ;
28
- import com .itextpdf .signatures .logs .SignLogMessageConstant ;
29
- import org .slf4j .Logger ;
30
- import org .slf4j .LoggerFactory ;
27
+ import com .itextpdf .kernel .exceptions .KernelExceptionMessageConstant ;
28
+ import com .itextpdf .kernel .logs .KernelLogMessageConstant ;
31
29
32
30
import java .io .IOException ;
33
31
import java .io .InputStream ;
@@ -37,6 +35,8 @@ This file is part of the iText (R) project.
37
35
import java .security .NoSuchProviderException ;
38
36
import java .util .HashMap ;
39
37
import java .util .Map ;
38
+ import org .slf4j .Logger ;
39
+ import org .slf4j .LoggerFactory ;
40
40
41
41
/**
42
42
* Class that contains a map with the different message digest algorithms.
@@ -123,10 +123,10 @@ public class DigestAlgorithms {
123
123
digestNames .put ("1.2.840.113549.2.5" , "MD5" );
124
124
digestNames .put ("1.2.840.113549.2.2" , "MD2" );
125
125
digestNames .put ("1.3.14.3.2.26" , "SHA1" );
126
- digestNames .put ("2.16.840.1.101.3.4.2.4" , "SHA224" );
127
- digestNames .put ("2.16.840.1.101.3.4.2.1" , "SHA256" );
128
- digestNames .put ("2.16.840.1.101.3.4.2.2" , "SHA384" );
129
- digestNames .put ("2.16.840.1.101.3.4.2.3" , "SHA512" );
126
+ digestNames .put (OID . SHA_224 , "SHA224" );
127
+ digestNames .put (OID . SHA_256 , "SHA256" );
128
+ digestNames .put (OID . SHA_384 , "SHA384" );
129
+ digestNames .put (OID . SHA_512 , "SHA512" );
130
130
digestNames .put ("1.3.36.3.2.2" , "RIPEMD128" );
131
131
digestNames .put ("1.3.36.3.2.1" , "RIPEMD160" );
132
132
digestNames .put ("1.3.36.3.2.3" , "RIPEMD256" );
@@ -137,8 +137,6 @@ public class DigestAlgorithms {
137
137
digestNames .put ("1.2.840.113549.1.1.11" , "SHA256" );
138
138
digestNames .put ("1.2.840.113549.1.1.12" , "SHA384" );
139
139
digestNames .put ("1.2.840.113549.1.1.13" , "SHA512" );
140
- digestNames .put ("1.2.840.113549.2.5" , "MD5" );
141
- digestNames .put ("1.2.840.113549.2.2" , "MD2" );
142
140
digestNames .put ("1.2.840.10040.4.3" , "SHA1" );
143
141
digestNames .put ("2.16.840.1.101.3.4.3.1" , "SHA224" );
144
142
digestNames .put ("2.16.840.1.101.3.4.3.2" , "SHA256" );
@@ -148,11 +146,11 @@ public class DigestAlgorithms {
148
146
digestNames .put ("1.3.36.3.3.1.2" , "RIPEMD160" );
149
147
digestNames .put ("1.3.36.3.3.1.4" , "RIPEMD256" );
150
148
digestNames .put ("1.2.643.2.2.9" , "GOST3411" );
151
- digestNames .put ("2.16.840.1.101.3.4.2.7" , "SHA3-224" );
152
- digestNames .put ("2.16.840.1.101.3.4.2.8" , "SHA3-256" );
153
- digestNames .put ("2.16.840.1.101.3.4.2.9" , "SHA3-384" );
154
- digestNames .put ("2.16.840.1.101.3.4.2.10" , "SHA3-512" );
155
- digestNames .put ("2.16.840.1.101.3.4.2.12" , "SHAKE256" );
149
+ digestNames .put (OID . SHA3_224 , "SHA3-224" );
150
+ digestNames .put (OID . SHA3_256 , "SHA3-256" );
151
+ digestNames .put (OID . SHA3_384 , "SHA3-384" );
152
+ digestNames .put (OID . SHA3_512 , "SHA3-512" );
153
+ digestNames .put (OID . SHAKE_256 , "SHAKE256" );
156
154
157
155
fixNames .put ("SHA256" , SHA256 );
158
156
fixNames .put ("SHA384" , SHA384 );
@@ -164,26 +162,26 @@ public class DigestAlgorithms {
164
162
allowedDigests .put ("MD-5" , "1.2.840.113549.2.5" );
165
163
allowedDigests .put ("SHA1" , "1.3.14.3.2.26" );
166
164
allowedDigests .put ("SHA-1" , "1.3.14.3.2.26" );
167
- allowedDigests .put ("SHA224" , "2.16.840.1.101.3.4.2.4" );
168
- allowedDigests .put ("SHA-224" , "2.16.840.1.101.3.4.2.4" );
169
- allowedDigests .put ("SHA256" , "2.16.840.1.101.3.4.2.1" );
170
- allowedDigests .put ("SHA-256" , "2.16.840.1.101.3.4.2.1" );
171
- allowedDigests .put ("SHA384" , "2.16.840.1.101.3.4.2.2" );
172
- allowedDigests .put ("SHA-384" , "2.16.840.1.101.3.4.2.2" );
173
- allowedDigests .put ("SHA512" , "2.16.840.1.101.3.4.2.3" );
174
- allowedDigests .put ("SHA-512" , "2.16.840.1.101.3.4.2.3" );
165
+ allowedDigests .put ("SHA224" , OID . SHA_224 );
166
+ allowedDigests .put ("SHA-224" , OID . SHA_224 );
167
+ allowedDigests .put ("SHA256" , OID . SHA_256 );
168
+ allowedDigests .put ("SHA-256" , OID . SHA_256 );
169
+ allowedDigests .put ("SHA384" , OID . SHA_384 );
170
+ allowedDigests .put ("SHA-384" , OID . SHA_384 );
171
+ allowedDigests .put ("SHA512" , OID . SHA_512 );
172
+ allowedDigests .put ("SHA-512" , OID . SHA_512 );
175
173
allowedDigests .put ("RIPEMD128" , "1.3.36.3.2.2" );
176
174
allowedDigests .put ("RIPEMD-128" , "1.3.36.3.2.2" );
177
175
allowedDigests .put ("RIPEMD160" , "1.3.36.3.2.1" );
178
176
allowedDigests .put ("RIPEMD-160" , "1.3.36.3.2.1" );
179
177
allowedDigests .put ("RIPEMD256" , "1.3.36.3.2.3" );
180
178
allowedDigests .put ("RIPEMD-256" , "1.3.36.3.2.3" );
181
179
allowedDigests .put ("GOST3411" , "1.2.643.2.2.9" );
182
- allowedDigests .put ("SHA3-224" , "2.16.840.1.101.3.4.2.7" );
183
- allowedDigests .put ("SHA3-256" , "2.16.840.1.101.3.4.2.8" );
184
- allowedDigests .put ("SHA3-384" , "2.16.840.1.101.3.4.2.9" );
185
- allowedDigests .put ("SHA3-512" , "2.16.840.1.101.3.4.2.10" );
186
- allowedDigests .put ("SHAKE256" , "2.16.840.1.101.3.4.2.12" );
180
+ allowedDigests .put ("SHA3-224" , OID . SHA3_224 );
181
+ allowedDigests .put ("SHA3-256" , OID . SHA3_256 );
182
+ allowedDigests .put ("SHA3-384" , OID . SHA3_384 );
183
+ allowedDigests .put ("SHA3-512" , OID . SHA3_512 );
184
+ allowedDigests .put ("SHAKE256" , OID . SHAKE_256 );
187
185
188
186
bitLengths .put ("MD2" , 128 );
189
187
bitLengths .put ("MD-2" , 128 );
@@ -217,7 +215,9 @@ public class DigestAlgorithms {
217
215
*
218
216
* @param digestOid oid of the digest algorithm
219
217
* @param provider the provider you want to use to create the hash
218
+ *
220
219
* @return MessageDigest object
220
+ *
221
221
* @throws NoSuchAlgorithmException thrown when a particular cryptographic algorithm is
222
222
* requested but is not available in the environment
223
223
* @throws NoSuchProviderException thrown when a particular security provider is
@@ -233,15 +233,17 @@ public static MessageDigest getMessageDigestFromOid(String digestOid, String pro
233
233
*
234
234
* @param hashAlgorithm the algorithm you want to use to create a hash
235
235
* @param provider the provider you want to use to create the hash
236
+ *
236
237
* @return a MessageDigest object
238
+ *
237
239
* @throws NoSuchAlgorithmException thrown when a particular cryptographic algorithm is
238
240
* requested but is not available in the environment
239
241
* @throws NoSuchProviderException thrown when a particular security provider is
240
242
* requested but is not available in the environment
241
243
*/
242
244
public static MessageDigest getMessageDigest (String hashAlgorithm , String provider )
243
245
throws NoSuchAlgorithmException , NoSuchProviderException {
244
- return SignUtils .getMessageDigest (hashAlgorithm , provider );
246
+ return CryptoUtil .getMessageDigest (hashAlgorithm , provider );
245
247
}
246
248
247
249
/**
@@ -250,7 +252,9 @@ public static MessageDigest getMessageDigest(String hashAlgorithm, String provid
250
252
* @param data the message of which you want to create a hash
251
253
* @param hashAlgorithm the algorithm used to create the hash
252
254
* @param provider the provider used to create the hash
255
+ *
253
256
* @return the hash
257
+ *
254
258
* @throws GeneralSecurityException when requested cryptographic algorithm or security provider
255
259
* is not available
256
260
* @throws IOException signals that an I/O exception has occurred
@@ -262,11 +266,13 @@ public static byte[] digest(InputStream data, String hashAlgorithm, String provi
262
266
}
263
267
264
268
/**
265
- * Create a digest based on the inputstream .
269
+ * Create a digest based on the input stream .
266
270
*
267
271
* @param data data to be digested
268
272
* @param messageDigest algorithm to be used
273
+ *
269
274
* @return digest of the data
275
+ *
270
276
* @throws IOException signals that an I/O exception has occurred
271
277
*/
272
278
public static byte [] digest (InputStream data , MessageDigest messageDigest )
@@ -291,7 +297,7 @@ public static String getDigest(String oid) {
291
297
if (ret == null ) {
292
298
try {
293
299
String digest = getMessageDigest (oid , BOUNCY_CASTLE_FACTORY .getProviderName ()).getAlgorithm ();
294
- LOGGER .warn (SignLogMessageConstant .ALGORITHM_NOT_FROM_SPEC );
300
+ LOGGER .warn (KernelLogMessageConstant .ALGORITHM_NOT_FROM_SPEC );
295
301
return digest ;
296
302
} catch (Exception e ) {
297
303
return oid ;
@@ -305,6 +311,7 @@ public static String getDigest(String oid) {
305
311
* Normalize the digest name.
306
312
*
307
313
* @param algo the name to be normalized
314
+ *
308
315
* @return normalized name
309
316
*/
310
317
public static String normalizeDigestName (String algo ) {
@@ -318,21 +325,22 @@ public static String normalizeDigestName(String algo) {
318
325
* Returns the id of a digest algorithms that is allowed in PDF,
319
326
* or null if it isn't allowed.
320
327
*
321
- * @param name The name of the digest algorithm.
322
- * @return An oid.
328
+ * @param name the name of the digest algorithm
329
+ *
330
+ * @return an oid
323
331
*/
324
332
public static String getAllowedDigest (String name ) {
325
333
if (name == null ) {
326
334
throw new IllegalArgumentException (
327
- SignExceptionMessageConstant .THE_NAME_OF_THE_DIGEST_ALGORITHM_IS_NULL );
335
+ KernelExceptionMessageConstant .THE_NAME_OF_THE_DIGEST_ALGORITHM_IS_NULL );
328
336
}
329
337
String allowedDigest = allowedDigests .get (name .toUpperCase ());
330
338
if (allowedDigest != null ) {
331
339
return allowedDigest ;
332
340
}
333
341
allowedDigest = BOUNCY_CASTLE_FACTORY .getDigestAlgorithmOid (name .toUpperCase ());
334
342
if (allowedDigest != null ) {
335
- LOGGER .warn (SignLogMessageConstant .ALGORITHM_NOT_FROM_SPEC );
343
+ LOGGER .warn (KernelLogMessageConstant .ALGORITHM_NOT_FROM_SPEC );
336
344
}
337
345
return allowedDigest ;
338
346
}
@@ -341,12 +349,13 @@ public static String getAllowedDigest(String name) {
341
349
* Retrieve the output length in bits of the given digest algorithm.
342
350
*
343
351
* @param name the name of the digest algorithm
352
+ *
344
353
* @return the length of the output of the algorithm in bits
345
354
*/
346
355
public static int getOutputBitLength (String name ) {
347
356
if (name == null ) {
348
357
throw new IllegalArgumentException (
349
- SignExceptionMessageConstant .THE_NAME_OF_THE_DIGEST_ALGORITHM_IS_NULL );
358
+ KernelExceptionMessageConstant .THE_NAME_OF_THE_DIGEST_ALGORITHM_IS_NULL );
350
359
}
351
360
return bitLengths .get (name ).intValue ();
352
361
}
0 commit comments