Remove unnecessary call to MessageDigest.reset

This commit is contained in:
Jason Tedor 2015-10-04 21:02:50 -04:00
parent e5a10e9520
commit d3cef85352
2 changed files with 6 additions and 9 deletions

View File

@ -41,23 +41,20 @@ public class MessageDigests {
} }
public static MessageDigest md5() { public static MessageDigest md5() {
return cloneAndReset(MD5_DIGEST); return clone(MD5_DIGEST);
} }
public static MessageDigest sha1() { public static MessageDigest sha1() {
return cloneAndReset(SHA_1_DIGEST); return clone(SHA_1_DIGEST);
} }
public static MessageDigest sha256() { public static MessageDigest sha256() {
return cloneAndReset(SHA_256_DIGEST); return clone(SHA_256_DIGEST);
} }
private static MessageDigest cloneAndReset(MessageDigest messageDigest) { private static MessageDigest clone(MessageDigest messageDigest) {
try { try {
MessageDigest clone = (MessageDigest) messageDigest.clone(); return (MessageDigest) messageDigest.clone();
clone.reset();
return clone;
} catch (CloneNotSupportedException e) { } catch (CloneNotSupportedException e) {
throw new ElasticsearchException("Unexpected exception cloning MessageDigest instance", e); throw new ElasticsearchException("Unexpected exception cloning MessageDigest instance", e);
} }

View File

@ -70,7 +70,7 @@ public class MessageDigestsTests extends ESTestCase {
BigInteger expected = BigInteger.probablePrime(256, random()); BigInteger expected = BigInteger.probablePrime(256, random());
byte[] bytes = expected.toByteArray(); byte[] bytes = expected.toByteArray();
String hex = MessageDigests.toHexString(bytes); String hex = MessageDigests.toHexString(bytes);
String zeros = new String(new char[bytes.length * 2]).replace("\0", "0"); String zeros = new String(new char[2 * bytes.length]).replace("\0", "0");
String expectedAsString = expected.toString(16); String expectedAsString = expected.toString(16);
String expectedHex = zeros.substring(expectedAsString.length()) + expectedAsString; String expectedHex = zeros.substring(expectedAsString.length()) + expectedAsString;
assertEquals(expectedHex, hex); assertEquals(expectedHex, hex);