Skip to content

Commit 8939061

Browse files
committed
Simplify - always require key when creating the instance
git-svn-id: https://svn.apache.org/repos/asf/commons/proper/codec/trunk@1745052 13f79535-47bb-0310-9956-ffa450edef68
1 parent 2286eac commit 8939061

1 file changed

Lines changed: 22 additions & 55 deletions

File tree

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

Lines changed: 22 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -42,19 +42,14 @@
4242
* <p>
4343
* Sample usage:
4444
* <pre>
45+
* import static HmacAlgorithms.*;
4546
* byte[] key = {1,2,3,4}; // don't use this!
4647
* String valueToDigest = "The quick brown fox jumps over the lazy dog";
47-
* byte[] hmac = HmacUtils.use(HmacAlgorithms.HMAC_SHA_224).key(key).update(valueToDigest).doFinal();
48+
* byte[] hmac = HmacUtils.use(HMAC_SHA_224, key).update(valueToDigest).doFinal();
4849
* // Mac re-use
49-
* HmacUtils hm1 = HmacUtils.use(HmacAlgorithms.HMAC_SHA_1).key(key);
50+
* HmacUtils hm1 = HmacUtils.use("HmacAlgoName", key); // use a valid name here!
5051
* String hexPom = hm1.update(new File("pom.xml")).doFinalHex();
5152
* String hexNot = hm1.update(new File("NOTICE.txt")).doFinalHex();
52-
* // Mac key update
53-
* String algo = "HmacNew";
54-
* HmacUtils hm2 = HmacUtils.use(algo).key(key);
55-
* byte[] key2 = {1,2,3,4,5}; // don't use this either!
56-
* String hexPom2 = hm2.update(new File("pom.xml")).doFinalHex();
57-
* String hexNot2 = hm2.key(key2).update(new File("NOTICE.txt")).doFinalHex();
5853
* </pre>
5954
* @since 1.10
6055
* @version $Id$
@@ -854,7 +849,12 @@ public static Mac updateHmac(final Mac mac, final String valueToDigest) {
854849
return mac;
855850
}
856851

857-
// public to maintain binary compatibility
852+
/**
853+
* Preserves binary compatibity only.
854+
* As for previous versions does not provide useful behaviour
855+
* @deprecated since 1.11; only useful to preserve binary compatibility
856+
*/
857+
@Deprecated
858858
public HmacUtils() {
859859
this(null);
860860
}
@@ -867,67 +867,36 @@ private HmacUtils(final Mac mac) {
867867
this.mac = mac;
868868
}
869869

870-
871-
/**
872-
* Creates an instance using the provided {@link Mac}
873-
* If necessary, the
874-
* key must be provided using the {@link #key(byte[])} method
875-
* before it can be used further.
876-
*
877-
* @param mac the {@link Mac} to be used.
878-
* @return the instance
879-
* @since 1.11
880-
*/
881-
public static HmacUtils use(final Mac mac) {
882-
return new HmacUtils(mac);
870+
private HmacUtils(final String algorithm, final byte[] key) {
871+
this(getInitializedMac(algorithm, key));
883872
}
884873

874+
885875
/**
886876
* Creates an instance using the provided algorithm type.
887-
* The key must be provided using the {@link #key(byte[])} method
888-
* before it can be used further.
889877
*
890-
* @param algorithm to be used.
878+
* @param algorithm to be used
879+
* @param key the key to be used
891880
* @return the instance
881+
* @throws IllegalArgumentException
882+
* when a {@link NoSuchAlgorithmException} is caught or key is null or key is invalid.
892883
* @since 1.11
893884
*/
894-
public static HmacUtils use(final String algorithm) {
895-
try {
896-
return new HmacUtils(Mac.getInstance(algorithm));
897-
} catch (NoSuchAlgorithmException e) {
898-
throw new IllegalArgumentException(e);
899-
}
885+
public static HmacUtils use(final String algorithm, final byte[] key) {
886+
return new HmacUtils(algorithm, key);
900887
}
901888

902889
/**
903890
* Creates an instance using the provided algorithm type.
904-
* The key must be provided using the {@link #key(byte[])} method
905-
* before it can be used further.
906891
*
907892
* @param algorithm to be used.
908893
* @return the instance
894+
* @throws IllegalArgumentException
895+
* when a {@link NoSuchAlgorithmException} is caught or key is null or key is invalid.
909896
* @since 1.11
910897
*/
911-
public static HmacUtils use(final HmacAlgorithms algorithm) {
912-
return use(algorithm.getName());
913-
}
914-
915-
/**
916-
* Updates the stored {@link Mac} with the new key.
917-
* This resets the Mac ready for re-use.
918-
*
919-
* @param key the new key
920-
* @return this instance
921-
* @since 1.11
922-
*/
923-
public HmacUtils key(byte[] key) {
924-
final SecretKeySpec keySpec = new SecretKeySpec(key, mac.getAlgorithm());
925-
try {
926-
mac.init(keySpec);
927-
} catch (InvalidKeyException e) {
928-
throw new IllegalArgumentException(e);
929-
}
930-
return this;
898+
public static HmacUtils use(final HmacAlgorithms algorithm, final byte[] key) {
899+
return use(algorithm.getName(), key);
931900
}
932901

933902
/**
@@ -1027,7 +996,6 @@ public HmacUtils update(final File valueToDigest) throws IOException {
1027996
/**
1028997
* Finishes the MAC operation and returns the result.
1029998
* The Mac can be re-used to produce further results from the same key.
1030-
* Or the key can be reset and the Mac reused.
1031999
*
10321000
* @return the result as a byte array
10331001
* @since 1.11
@@ -1039,7 +1007,6 @@ public byte[] doFinal() {
10391007
/**
10401008
* Finishes the MAC operation and returns the result.
10411009
* The Mac can be re-used to produce further results from the same key.
1042-
* Or the key can be reset and the Mac reused.
10431010
*
10441011
* @return the result as a Hex String
10451012
* @since 1.11

0 commit comments

Comments
 (0)