@@ -20,14 +20,12 @@ This file is part of the iText (R) project.
2020 You should have received a copy of the GNU Affero General Public License
2121 along with this program. If not, see <https://www.gnu.org/licenses/>.
2222 */
23- package com .itextpdf .signatures ;
23+ package com .itextpdf .kernel . crypto ;
2424
2525import com .itextpdf .bouncycastleconnector .BouncyCastleFactoryCreator ;
2626import 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 ;
3129
3230import java .io .IOException ;
3331import java .io .InputStream ;
@@ -37,6 +35,8 @@ This file is part of the iText (R) project.
3735import java .security .NoSuchProviderException ;
3836import java .util .HashMap ;
3937import java .util .Map ;
38+ import org .slf4j .Logger ;
39+ import org .slf4j .LoggerFactory ;
4040
4141/**
4242 * Class that contains a map with the different message digest algorithms.
@@ -123,10 +123,10 @@ public class DigestAlgorithms {
123123 digestNames .put ("1.2.840.113549.2.5" , "MD5" );
124124 digestNames .put ("1.2.840.113549.2.2" , "MD2" );
125125 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" );
130130 digestNames .put ("1.3.36.3.2.2" , "RIPEMD128" );
131131 digestNames .put ("1.3.36.3.2.1" , "RIPEMD160" );
132132 digestNames .put ("1.3.36.3.2.3" , "RIPEMD256" );
@@ -137,8 +137,6 @@ public class DigestAlgorithms {
137137 digestNames .put ("1.2.840.113549.1.1.11" , "SHA256" );
138138 digestNames .put ("1.2.840.113549.1.1.12" , "SHA384" );
139139 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" );
142140 digestNames .put ("1.2.840.10040.4.3" , "SHA1" );
143141 digestNames .put ("2.16.840.1.101.3.4.3.1" , "SHA224" );
144142 digestNames .put ("2.16.840.1.101.3.4.3.2" , "SHA256" );
@@ -148,11 +146,11 @@ public class DigestAlgorithms {
148146 digestNames .put ("1.3.36.3.3.1.2" , "RIPEMD160" );
149147 digestNames .put ("1.3.36.3.3.1.4" , "RIPEMD256" );
150148 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" );
156154
157155 fixNames .put ("SHA256" , SHA256 );
158156 fixNames .put ("SHA384" , SHA384 );
@@ -164,26 +162,26 @@ public class DigestAlgorithms {
164162 allowedDigests .put ("MD-5" , "1.2.840.113549.2.5" );
165163 allowedDigests .put ("SHA1" , "1.3.14.3.2.26" );
166164 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 );
175173 allowedDigests .put ("RIPEMD128" , "1.3.36.3.2.2" );
176174 allowedDigests .put ("RIPEMD-128" , "1.3.36.3.2.2" );
177175 allowedDigests .put ("RIPEMD160" , "1.3.36.3.2.1" );
178176 allowedDigests .put ("RIPEMD-160" , "1.3.36.3.2.1" );
179177 allowedDigests .put ("RIPEMD256" , "1.3.36.3.2.3" );
180178 allowedDigests .put ("RIPEMD-256" , "1.3.36.3.2.3" );
181179 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 );
187185
188186 bitLengths .put ("MD2" , 128 );
189187 bitLengths .put ("MD-2" , 128 );
@@ -217,7 +215,9 @@ public class DigestAlgorithms {
217215 *
218216 * @param digestOid oid of the digest algorithm
219217 * @param provider the provider you want to use to create the hash
218+ *
220219 * @return MessageDigest object
220+ *
221221 * @throws NoSuchAlgorithmException thrown when a particular cryptographic algorithm is
222222 * requested but is not available in the environment
223223 * @throws NoSuchProviderException thrown when a particular security provider is
@@ -233,15 +233,17 @@ public static MessageDigest getMessageDigestFromOid(String digestOid, String pro
233233 *
234234 * @param hashAlgorithm the algorithm you want to use to create a hash
235235 * @param provider the provider you want to use to create the hash
236+ *
236237 * @return a MessageDigest object
238+ *
237239 * @throws NoSuchAlgorithmException thrown when a particular cryptographic algorithm is
238240 * requested but is not available in the environment
239241 * @throws NoSuchProviderException thrown when a particular security provider is
240242 * requested but is not available in the environment
241243 */
242244 public static MessageDigest getMessageDigest (String hashAlgorithm , String provider )
243245 throws NoSuchAlgorithmException , NoSuchProviderException {
244- return SignUtils .getMessageDigest (hashAlgorithm , provider );
246+ return CryptoUtil .getMessageDigest (hashAlgorithm , provider );
245247 }
246248
247249 /**
@@ -250,7 +252,9 @@ public static MessageDigest getMessageDigest(String hashAlgorithm, String provid
250252 * @param data the message of which you want to create a hash
251253 * @param hashAlgorithm the algorithm used to create the hash
252254 * @param provider the provider used to create the hash
255+ *
253256 * @return the hash
257+ *
254258 * @throws GeneralSecurityException when requested cryptographic algorithm or security provider
255259 * is not available
256260 * @throws IOException signals that an I/O exception has occurred
@@ -262,11 +266,13 @@ public static byte[] digest(InputStream data, String hashAlgorithm, String provi
262266 }
263267
264268 /**
265- * Create a digest based on the inputstream .
269+ * Create a digest based on the input stream .
266270 *
267271 * @param data data to be digested
268272 * @param messageDigest algorithm to be used
273+ *
269274 * @return digest of the data
275+ *
270276 * @throws IOException signals that an I/O exception has occurred
271277 */
272278 public static byte [] digest (InputStream data , MessageDigest messageDigest )
@@ -291,7 +297,7 @@ public static String getDigest(String oid) {
291297 if (ret == null ) {
292298 try {
293299 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 );
295301 return digest ;
296302 } catch (Exception e ) {
297303 return oid ;
@@ -305,6 +311,7 @@ public static String getDigest(String oid) {
305311 * Normalize the digest name.
306312 *
307313 * @param algo the name to be normalized
314+ *
308315 * @return normalized name
309316 */
310317 public static String normalizeDigestName (String algo ) {
@@ -318,21 +325,22 @@ public static String normalizeDigestName(String algo) {
318325 * Returns the id of a digest algorithms that is allowed in PDF,
319326 * or null if it isn't allowed.
320327 *
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
323331 */
324332 public static String getAllowedDigest (String name ) {
325333 if (name == null ) {
326334 throw new IllegalArgumentException (
327- SignExceptionMessageConstant .THE_NAME_OF_THE_DIGEST_ALGORITHM_IS_NULL );
335+ KernelExceptionMessageConstant .THE_NAME_OF_THE_DIGEST_ALGORITHM_IS_NULL );
328336 }
329337 String allowedDigest = allowedDigests .get (name .toUpperCase ());
330338 if (allowedDigest != null ) {
331339 return allowedDigest ;
332340 }
333341 allowedDigest = BOUNCY_CASTLE_FACTORY .getDigestAlgorithmOid (name .toUpperCase ());
334342 if (allowedDigest != null ) {
335- LOGGER .warn (SignLogMessageConstant .ALGORITHM_NOT_FROM_SPEC );
343+ LOGGER .warn (KernelLogMessageConstant .ALGORITHM_NOT_FROM_SPEC );
336344 }
337345 return allowedDigest ;
338346 }
@@ -341,12 +349,13 @@ public static String getAllowedDigest(String name) {
341349 * Retrieve the output length in bits of the given digest algorithm.
342350 *
343351 * @param name the name of the digest algorithm
352+ *
344353 * @return the length of the output of the algorithm in bits
345354 */
346355 public static int getOutputBitLength (String name ) {
347356 if (name == null ) {
348357 throw new IllegalArgumentException (
349- SignExceptionMessageConstant .THE_NAME_OF_THE_DIGEST_ALGORITHM_IS_NULL );
358+ KernelExceptionMessageConstant .THE_NAME_OF_THE_DIGEST_ALGORITHM_IS_NULL );
350359 }
351360 return bitLengths .get (name ).intValue ();
352361 }
0 commit comments