1717package org .apache .commons .codec .digest ;
1818
1919import java .security .MessageDigest ;
20- import java .security .NoSuchAlgorithmException ;
2120import java .util .Arrays ;
2221import java .util .regex .Matcher ;
2322import java .util .regex .Pattern ;
@@ -52,18 +51,12 @@ public class Sha2Crypt {
5251 /** Prefix for optional rounds specification. */
5352 private static final String ROUNDS_PREFIX = "rounds=" ;
5453
55- /** The SHA-256 MessageDigest algorithm. */
56- private static final String SHA256_ALGORITHM = "SHA-256" ;
57-
5854 /** The number of bytes the final hash value will have (SHA-256 variant). */
5955 private static final int SHA256_BLOCKSIZE = 32 ;
6056
6157 /** The prefixes that can be used to identify this crypt() variant (SHA-256). */
6258 static final String SHA256_PREFIX = "$5$" ;
6359
64- /** The SHA-512 MessageDigest algorithm. */
65- private static final String SHA512_ALGORITHM = "SHA-512" ;
66-
6760 /** The number of bytes the final hash value will have (SHA-512 variant). */
6861 private static final int SHA512_BLOCKSIZE = 64 ;
6962
@@ -79,9 +72,10 @@ public class Sha2Crypt {
7972 * <p>
8073 * See {@link Crypt#crypt(String, String)} for details.
8174 *
82- * @throws NoSuchAlgorithmException if no "SHA-256" algorithm implementation is available
75+ * @throws RuntimeException
76+ * when a {@link java.security.NoSuchAlgorithmException} is caught.
8377 */
84- public static String sha256Crypt (byte [] keyBytes ) throws NoSuchAlgorithmException {
78+ public static String sha256Crypt (byte [] keyBytes ) {
8579 return sha256Crypt (keyBytes , null );
8680 }
8781
@@ -91,13 +85,14 @@ public static String sha256Crypt(byte[] keyBytes) throws NoSuchAlgorithmExceptio
9185 * See {@link Crypt#crypt(String, String)} for details.
9286 *
9387 * @throws IllegalArgumentException if the salt does not match the allowed pattern
94- * @throws NoSuchAlgorithmException if no "SHA-256" algorithm implementation is available
88+ * @throws RuntimeException
89+ * when a {@link java.security.NoSuchAlgorithmException} is caught.
9590 */
96- public static String sha256Crypt (byte [] keyBytes , String salt ) throws NoSuchAlgorithmException {
91+ public static String sha256Crypt (byte [] keyBytes , String salt ) {
9792 if (salt == null ) {
9893 salt = SHA256_PREFIX + B64 .getRandomSalt (8 );
9994 }
100- return sha2Crypt (keyBytes , salt , SHA256_PREFIX , SHA256_BLOCKSIZE , SHA256_ALGORITHM );
95+ return sha2Crypt (keyBytes , salt , SHA256_PREFIX , SHA256_BLOCKSIZE , MessageDigestAlgorithms . SHA_256 );
10196 }
10297
10398 /**
@@ -121,10 +116,11 @@ public static String sha256Crypt(byte[] keyBytes, String salt) throws NoSuchAlgo
121116 * {@link MessageDigest} algorithm identifier string
122117 * @return complete hash value including prefix and salt
123118 * @throws IllegalArgumentException if the given salt is {@code null} or does not match the allowed pattern
124- * @throws NoSuchAlgorithmException if no implementation for the given algorithm is available
119+ * @throws RuntimeException
120+ * when a {@link java.security.NoSuchAlgorithmException} is caught.
125121 */
126122 private static String sha2Crypt (byte [] keyBytes , String salt , String saltPrefix , int blocksize , String algorithm )
127- throws NoSuchAlgorithmException {
123+ {
128124
129125 int keyLen = keyBytes .length ;
130126
@@ -150,7 +146,7 @@ private static String sha2Crypt(byte[] keyBytes, String salt, String saltPrefix,
150146
151147 // 1. start digest A
152148 // Prepare for the real work.
153- MessageDigest ctx = MessageDigest . getInstance (algorithm );
149+ MessageDigest ctx = DigestUtils . getDigest (algorithm );
154150
155151 // 2. the password string is added to digest A
156152 /*
@@ -178,7 +174,7 @@ private static String sha2Crypt(byte[] keyBytes, String salt, String saltPrefix,
178174 * Compute alternate sha512 sum with input KEY, SALT, and KEY. The final result will be added to the first
179175 * context.
180176 */
181- MessageDigest altCtx = MessageDigest . getInstance (algorithm );
177+ MessageDigest altCtx = DigestUtils . getDigest (algorithm );
182178
183179 // 5. add the password to digest B
184180 /*
@@ -256,7 +252,7 @@ private static String sha2Crypt(byte[] keyBytes, String salt, String saltPrefix,
256252 /*
257253 * Start computation of P byte sequence.
258254 */
259- altCtx = MessageDigest . getInstance (algorithm );
255+ altCtx = DigestUtils . getDigest (algorithm );
260256
261257 // 14. for every byte in the password (excluding the terminating NUL byte
262258 // in the C representation of the string)
@@ -297,7 +293,7 @@ private static String sha2Crypt(byte[] keyBytes, String salt, String saltPrefix,
297293 /*
298294 * Start computation of S byte sequence.
299295 */
300- altCtx = MessageDigest . getInstance (algorithm );
296+ altCtx = DigestUtils . getDigest (algorithm );
301297
302298 // 18. repeast the following 16+A[0] times, where A[0] represents the first
303299 // byte in digest A interpreted as an 8-bit unsigned value
@@ -351,7 +347,7 @@ private static String sha2Crypt(byte[] keyBytes, String salt, String saltPrefix,
351347 /*
352348 * New context.
353349 */
354- ctx = MessageDigest . getInstance (algorithm );
350+ ctx = DigestUtils . getDigest (algorithm );
355351
356352 // b) for odd round numbers add the byte sequense P to digest C
357353 // c) for even round numbers add digest A/C
@@ -504,9 +500,10 @@ private static String sha2Crypt(byte[] keyBytes, String salt, String saltPrefix,
504500 * <p>
505501 * See {@link Crypt#crypt(String, String)} for details.
506502 *
507- * @throws NoSuchAlgorithmException if no "SHA-512" algorithm implementation is available
503+ * @throws RuntimeException
504+ * when a {@link java.security.NoSuchAlgorithmException} is caught.
508505 */
509- public static String sha512Crypt (byte [] keyBytes ) throws NoSuchAlgorithmException {
506+ public static String sha512Crypt (byte [] keyBytes ) {
510507 return sha512Crypt (keyBytes , null );
511508 }
512509
@@ -516,12 +513,13 @@ public static String sha512Crypt(byte[] keyBytes) throws NoSuchAlgorithmExceptio
516513 * See {@link Crypt#crypt(String, String)} for details.
517514 *
518515 * @throws IllegalArgumentException if the salt does not match the allowed pattern
519- * @throws NoSuchAlgorithmException if no "SHA-512" algorithm implementation is available
516+ * @throws RuntimeException
517+ * when a {@link java.security.NoSuchAlgorithmException} is caught.
520518 */
521- public static String sha512Crypt (byte [] keyBytes , String salt ) throws NoSuchAlgorithmException {
519+ public static String sha512Crypt (byte [] keyBytes , String salt ) {
522520 if (salt == null ) {
523521 salt = SHA512_PREFIX + B64 .getRandomSalt (8 );
524522 }
525- return sha2Crypt (keyBytes , salt , SHA512_PREFIX , SHA512_BLOCKSIZE , SHA512_ALGORITHM );
523+ return sha2Crypt (keyBytes , salt , SHA512_PREFIX , SHA512_BLOCKSIZE , MessageDigestAlgorithms . SHA_512 );
526524 }
527525}
0 commit comments