Skip to content

Commit ae31e56

Browse files
committed
Use local code rather than depending on the enum methods
git-svn-id: https://svn.apache.org/repos/asf/commons/proper/codec/trunk@1745098 13f79535-47bb-0310-9956-ffa450edef68
1 parent 52cfbfd commit ae31e56

1 file changed

Lines changed: 58 additions & 30 deletions

File tree

src/main/java/org/apache/commons/codec/digest/HmacUtils.java

Lines changed: 58 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,7 @@ public static Mac getInitializedMac(final String algorithm, final byte[] key) {
228228
*/
229229
@Deprecated
230230
public static byte[] hmacMd5(final byte[] key, final byte[] valueToDigest) {
231-
return HmacAlgorithms.HMAC_MD5.hmac(key, valueToDigest);
231+
return HmacUtils.use(HmacAlgorithms.HMAC_MD5, key).hmac(valueToDigest);
232232
}
233233

234234
/**
@@ -250,7 +250,7 @@ public static byte[] hmacMd5(final byte[] key, final byte[] valueToDigest) {
250250
*/
251251
@Deprecated
252252
public static byte[] hmacMd5(final byte[] key, final InputStream valueToDigest) throws IOException {
253-
return HmacAlgorithms.HMAC_MD5.hmac(key, valueToDigest);
253+
return HmacUtils.use(HmacAlgorithms.HMAC_MD5, key).hmac(valueToDigest);
254254
}
255255

256256
/**
@@ -267,7 +267,7 @@ public static byte[] hmacMd5(final byte[] key, final InputStream valueToDigest)
267267
*/
268268
@Deprecated
269269
public static byte[] hmacMd5(final String key, final String valueToDigest) {
270-
return HmacAlgorithms.HMAC_MD5.hmac(key, valueToDigest);
270+
return HmacUtils.use(HmacAlgorithms.HMAC_MD5, key).hmac(valueToDigest);
271271
}
272272

273273
/**
@@ -284,7 +284,7 @@ public static byte[] hmacMd5(final String key, final String valueToDigest) {
284284
*/
285285
@Deprecated
286286
public static String hmacMd5Hex(final byte[] key, final byte[] valueToDigest) {
287-
return HmacAlgorithms.HMAC_MD5.hmacHex(key, valueToDigest);
287+
return HmacUtils.use(HmacAlgorithms.HMAC_MD5, key).hmacHex(valueToDigest);
288288
}
289289

290290
/**
@@ -306,7 +306,7 @@ public static String hmacMd5Hex(final byte[] key, final byte[] valueToDigest) {
306306
*/
307307
@Deprecated
308308
public static String hmacMd5Hex(final byte[] key, final InputStream valueToDigest) throws IOException {
309-
return HmacAlgorithms.HMAC_MD5.hmacHex(key, valueToDigest);
309+
return HmacUtils.use(HmacAlgorithms.HMAC_MD5, key).hmacHex(valueToDigest);
310310
}
311311

312312
/**
@@ -323,7 +323,7 @@ public static String hmacMd5Hex(final byte[] key, final InputStream valueToDiges
323323
*/
324324
@Deprecated
325325
public static String hmacMd5Hex(final String key, final String valueToDigest) {
326-
return HmacAlgorithms.HMAC_MD5.hmacHex(key, valueToDigest);
326+
return HmacUtils.use(HmacAlgorithms.HMAC_MD5, key).hmacHex(valueToDigest);
327327
}
328328

329329
// hmacSha1
@@ -342,7 +342,7 @@ public static String hmacMd5Hex(final String key, final String valueToDigest) {
342342
*/
343343
@Deprecated
344344
public static byte[] hmacSha1(final byte[] key, final byte[] valueToDigest) {
345-
return HmacAlgorithms.HMAC_SHA_1.hmac(key, valueToDigest);
345+
return HmacUtils.use(HmacAlgorithms.HMAC_SHA_1, key).hmac(valueToDigest);
346346
}
347347

348348
/**
@@ -364,7 +364,7 @@ public static byte[] hmacSha1(final byte[] key, final byte[] valueToDigest) {
364364
*/
365365
@Deprecated
366366
public static byte[] hmacSha1(final byte[] key, final InputStream valueToDigest) throws IOException {
367-
return HmacAlgorithms.HMAC_SHA_1.hmac(key, valueToDigest);
367+
return HmacUtils.use(HmacAlgorithms.HMAC_SHA_1, key).hmac(valueToDigest);
368368
}
369369

370370
/**
@@ -381,7 +381,7 @@ public static byte[] hmacSha1(final byte[] key, final InputStream valueToDigest)
381381
*/
382382
@Deprecated
383383
public static byte[] hmacSha1(final String key, final String valueToDigest) {
384-
return HmacAlgorithms.HMAC_SHA_1.hmac(key, valueToDigest);
384+
return HmacUtils.use(HmacAlgorithms.HMAC_SHA_1, key).hmac(valueToDigest);
385385
}
386386

387387
/**
@@ -398,7 +398,7 @@ public static byte[] hmacSha1(final String key, final String valueToDigest) {
398398
*/
399399
@Deprecated
400400
public static String hmacSha1Hex(final byte[] key, final byte[] valueToDigest) {
401-
return HmacAlgorithms.HMAC_SHA_1.hmacHex(key, valueToDigest);
401+
return HmacUtils.use(HmacAlgorithms.HMAC_SHA_1, key).hmacHex(valueToDigest);
402402
}
403403

404404
/**
@@ -420,7 +420,7 @@ public static String hmacSha1Hex(final byte[] key, final byte[] valueToDigest) {
420420
*/
421421
@Deprecated
422422
public static String hmacSha1Hex(final byte[] key, final InputStream valueToDigest) throws IOException {
423-
return HmacAlgorithms.HMAC_SHA_1.hmacHex(key, valueToDigest);
423+
return HmacUtils.use(HmacAlgorithms.HMAC_SHA_1, key).hmacHex(valueToDigest);
424424
}
425425

426426
/**
@@ -437,7 +437,7 @@ public static String hmacSha1Hex(final byte[] key, final InputStream valueToDige
437437
*/
438438
@Deprecated
439439
public static String hmacSha1Hex(final String key, final String valueToDigest) {
440-
return HmacAlgorithms.HMAC_SHA_1.hmacHex(key, valueToDigest);
440+
return HmacUtils.use(HmacAlgorithms.HMAC_SHA_1, key).hmacHex(valueToDigest);
441441
}
442442

443443
// hmacSha256
@@ -456,7 +456,7 @@ public static String hmacSha1Hex(final String key, final String valueToDigest) {
456456
*/
457457
@Deprecated
458458
public static byte[] hmacSha256(final byte[] key, final byte[] valueToDigest) {
459-
return HmacAlgorithms.HMAC_SHA_256.hmac(key, valueToDigest);
459+
return HmacUtils.use(HmacAlgorithms.HMAC_SHA_256, key).hmac(valueToDigest);
460460
}
461461

462462
/**
@@ -478,7 +478,7 @@ public static byte[] hmacSha256(final byte[] key, final byte[] valueToDigest) {
478478
*/
479479
@Deprecated
480480
public static byte[] hmacSha256(final byte[] key, final InputStream valueToDigest) throws IOException {
481-
return HmacAlgorithms.HMAC_SHA_256.hmac(key, valueToDigest);
481+
return HmacUtils.use(HmacAlgorithms.HMAC_SHA_256, key).hmac(valueToDigest);
482482
}
483483

484484
/**
@@ -495,7 +495,7 @@ public static byte[] hmacSha256(final byte[] key, final InputStream valueToDiges
495495
*/
496496
@Deprecated
497497
public static byte[] hmacSha256(final String key, final String valueToDigest) {
498-
return HmacAlgorithms.HMAC_SHA_256.hmac(key, valueToDigest);
498+
return HmacUtils.use(HmacAlgorithms.HMAC_SHA_256, key).hmac(valueToDigest);
499499
}
500500

501501
/**
@@ -512,7 +512,7 @@ public static byte[] hmacSha256(final String key, final String valueToDigest) {
512512
*/
513513
@Deprecated
514514
public static String hmacSha256Hex(final byte[] key, final byte[] valueToDigest) {
515-
return HmacAlgorithms.HMAC_SHA_256.hmacHex(key, valueToDigest);
515+
return HmacUtils.use(HmacAlgorithms.HMAC_SHA_256, key).hmacHex(valueToDigest);
516516
}
517517

518518
/**
@@ -534,7 +534,7 @@ public static String hmacSha256Hex(final byte[] key, final byte[] valueToDigest)
534534
*/
535535
@Deprecated
536536
public static String hmacSha256Hex(final byte[] key, final InputStream valueToDigest) throws IOException {
537-
return HmacAlgorithms.HMAC_SHA_256.hmacHex(key, valueToDigest);
537+
return HmacUtils.use(HmacAlgorithms.HMAC_SHA_256, key).hmacHex(valueToDigest);
538538
}
539539

540540
/**
@@ -551,7 +551,7 @@ public static String hmacSha256Hex(final byte[] key, final InputStream valueToDi
551551
*/
552552
@Deprecated
553553
public static String hmacSha256Hex(final String key, final String valueToDigest) {
554-
return HmacAlgorithms.HMAC_SHA_256.hmacHex(key, valueToDigest);
554+
return HmacUtils.use(HmacAlgorithms.HMAC_SHA_256, key).hmacHex(valueToDigest);
555555
}
556556

557557
// hmacSha384
@@ -570,7 +570,7 @@ public static String hmacSha256Hex(final String key, final String valueToDigest)
570570
*/
571571
@Deprecated
572572
public static byte[] hmacSha384(final byte[] key, final byte[] valueToDigest) {
573-
return HmacAlgorithms.HMAC_SHA_384.hmac(key, valueToDigest);
573+
return HmacUtils.use(HmacAlgorithms.HMAC_SHA_384, key).hmac(valueToDigest);
574574
}
575575

576576
/**
@@ -592,7 +592,7 @@ public static byte[] hmacSha384(final byte[] key, final byte[] valueToDigest) {
592592
*/
593593
@Deprecated
594594
public static byte[] hmacSha384(final byte[] key, final InputStream valueToDigest) throws IOException {
595-
return HmacAlgorithms.HMAC_SHA_384.hmac(key, valueToDigest);
595+
return HmacUtils.use(HmacAlgorithms.HMAC_SHA_384, key).hmac(valueToDigest);
596596
}
597597

598598
/**
@@ -609,7 +609,7 @@ public static byte[] hmacSha384(final byte[] key, final InputStream valueToDiges
609609
*/
610610
@Deprecated
611611
public static byte[] hmacSha384(final String key, final String valueToDigest) {
612-
return HmacAlgorithms.HMAC_SHA_384.hmac(key, valueToDigest);
612+
return HmacUtils.use(HmacAlgorithms.HMAC_SHA_384, key).hmac(valueToDigest);
613613
}
614614

615615
/**
@@ -626,7 +626,7 @@ public static byte[] hmacSha384(final String key, final String valueToDigest) {
626626
*/
627627
@Deprecated
628628
public static String hmacSha384Hex(final byte[] key, final byte[] valueToDigest) {
629-
return HmacAlgorithms.HMAC_SHA_384.hmacHex(key, valueToDigest);
629+
return HmacUtils.use(HmacAlgorithms.HMAC_SHA_384, key).hmacHex(valueToDigest);
630630
}
631631

632632
/**
@@ -648,7 +648,7 @@ public static String hmacSha384Hex(final byte[] key, final byte[] valueToDigest)
648648
*/
649649
@Deprecated
650650
public static String hmacSha384Hex(final byte[] key, final InputStream valueToDigest) throws IOException {
651-
return HmacAlgorithms.HMAC_SHA_384.hmacHex(key, valueToDigest);
651+
return HmacUtils.use(HmacAlgorithms.HMAC_SHA_384, key).hmacHex(valueToDigest);
652652
}
653653

654654
/**
@@ -665,7 +665,7 @@ public static String hmacSha384Hex(final byte[] key, final InputStream valueToDi
665665
*/
666666
@Deprecated
667667
public static String hmacSha384Hex(final String key, final String valueToDigest) {
668-
return HmacAlgorithms.HMAC_SHA_384.hmacHex(key, valueToDigest);
668+
return HmacUtils.use(HmacAlgorithms.HMAC_SHA_384, key).hmacHex(valueToDigest);
669669
}
670670

671671
// hmacSha512
@@ -684,7 +684,7 @@ public static String hmacSha384Hex(final String key, final String valueToDigest)
684684
*/
685685
@Deprecated
686686
public static byte[] hmacSha512(final byte[] key, final byte[] valueToDigest) {
687-
return HmacAlgorithms.HMAC_SHA_512.hmac(key, valueToDigest);
687+
return HmacUtils.use(HmacAlgorithms.HMAC_SHA_512, key).hmac(valueToDigest);
688688
}
689689

690690
/**
@@ -706,7 +706,7 @@ public static byte[] hmacSha512(final byte[] key, final byte[] valueToDigest) {
706706
*/
707707
@Deprecated
708708
public static byte[] hmacSha512(final byte[] key, final InputStream valueToDigest) throws IOException {
709-
return HmacAlgorithms.HMAC_SHA_512.hmac(key, valueToDigest);
709+
return HmacUtils.use(HmacAlgorithms.HMAC_SHA_512, key).hmac(valueToDigest);
710710
}
711711

712712
/**
@@ -723,7 +723,7 @@ public static byte[] hmacSha512(final byte[] key, final InputStream valueToDiges
723723
*/
724724
@Deprecated
725725
public static byte[] hmacSha512(final String key, final String valueToDigest) {
726-
return HmacAlgorithms.HMAC_SHA_512.hmac(key, valueToDigest);
726+
return HmacUtils.use(HmacAlgorithms.HMAC_SHA_512, key).hmac(valueToDigest);
727727
}
728728

729729
/**
@@ -740,7 +740,7 @@ public static byte[] hmacSha512(final String key, final String valueToDigest) {
740740
*/
741741
@Deprecated
742742
public static String hmacSha512Hex(final byte[] key, final byte[] valueToDigest) {
743-
return HmacAlgorithms.HMAC_SHA_512.hmacHex(key, valueToDigest);
743+
return HmacUtils.use(HmacAlgorithms.HMAC_SHA_512, key).hmacHex(valueToDigest);
744744
}
745745

746746
/**
@@ -762,7 +762,7 @@ public static String hmacSha512Hex(final byte[] key, final byte[] valueToDigest)
762762
*/
763763
@Deprecated
764764
public static String hmacSha512Hex(final byte[] key, final InputStream valueToDigest) throws IOException {
765-
return HmacAlgorithms.HMAC_SHA_512.hmacHex(key, valueToDigest);
765+
return HmacUtils.use(HmacAlgorithms.HMAC_SHA_512, key).hmacHex(valueToDigest);
766766
}
767767

768768
/**
@@ -779,7 +779,7 @@ public static String hmacSha512Hex(final byte[] key, final InputStream valueToDi
779779
*/
780780
@Deprecated
781781
public static String hmacSha512Hex(final String key, final String valueToDigest) {
782-
return HmacAlgorithms.HMAC_SHA_512.hmacHex(key, valueToDigest);
782+
return HmacUtils.use(HmacAlgorithms.HMAC_SHA_512, key).hmacHex(valueToDigest);
783783
}
784784

785785
// update
@@ -884,6 +884,34 @@ public static HmacUtils use(final String algorithm, final byte[] key) {
884884
return new HmacUtils(algorithm, key);
885885
}
886886

887+
/**
888+
* Creates an instance using the provided algorithm type.
889+
*
890+
* @param algorithm to be used
891+
* @param key the key to be used
892+
* @return the instance
893+
* @throws IllegalArgumentException
894+
* when a {@link NoSuchAlgorithmException} is caught or key is null or key is invalid.
895+
* @since 1.11
896+
*/
897+
public static HmacUtils use(final String algorithm, final String key) {
898+
return new HmacUtils(algorithm, StringUtils.getBytesUtf8(key));
899+
}
900+
901+
/**
902+
* Creates an instance using the provided algorithm type.
903+
*
904+
* @param algorithm to be used
905+
* @param key the key to be used
906+
* @return the instance
907+
* @throws IllegalArgumentException
908+
* when a {@link NoSuchAlgorithmException} is caught or key is null or key is invalid.
909+
* @since 1.11
910+
*/
911+
public static HmacUtils use(final HmacAlgorithms algorithm, final String key) {
912+
return new HmacUtils(algorithm.getName(), StringUtils.getBytesUtf8(key));
913+
}
914+
887915
/**
888916
* Creates an instance using the provided algorithm type.
889917
*

0 commit comments

Comments
 (0)