Pull #3093: Use DatatypeConverter#printHexBinary to convert byte array to hex String in PropertyCacheFile (#3093)

This commit is contained in:
Andrei Selkin 2016-04-13 16:39:08 +03:00 committed by Roman Ivanov
parent 42cf6ad0a9
commit b5d2fc84d7
2 changed files with 4 additions and 29 deletions

View File

@ -24,6 +24,7 @@
<!-- The local ones -->
<allow class="java.security.MessageDigest" local-only="true"/>
<allow class="java.security.NoSuchAlgorithmException" local-only="true"/>
<allow class="javax.xml.bind.DatatypeConverter" local-only="true"/>
<allow pkg="com.puppycrawl.tools.checkstyle.grammars" local-only="true"/>
<allow pkg="org.apache.commons.cli" local-only="true"/>

View File

@ -36,6 +36,8 @@ import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Properties;
import javax.xml.bind.DatatypeConverter;
import com.google.common.io.Closeables;
import com.google.common.io.Flushables;
import com.puppycrawl.tools.checkstyle.api.Configuration;
@ -62,18 +64,6 @@ final class PropertyCacheFile {
*/
private static final String CONFIG_HASH_KEY = "configuration*?";
/** Hex digits. */
private static final char[] HEX_CHARS = {
'0', '1', '2', '3', '4', '5', '6', '7',
'8', '9', 'A', 'B', 'C', 'D', 'E', 'F',
};
/** Mask for last byte. */
private static final int MASK_0X0F = 0x0F;
/** Bit shift. */
private static final int SHIFT_4 = 4;
/** The details on files. **/
private final Properties details = new Properties();
@ -221,27 +211,11 @@ final class PropertyCacheFile {
final MessageDigest digest = MessageDigest.getInstance("SHA-1");
digest.update(outputStream.toByteArray());
return hexEncode(digest.digest());
return DatatypeConverter.printHexBinary(digest.digest());
}
catch (final IOException | NoSuchAlgorithmException ex) {
// rethrow as unchecked exception
throw new IllegalStateException("Unable to calculate hashcode.", ex);
}
}
/**
* Hex-encodes a byte array.
* @param byteArray the byte array
* @return hex encoding of {@code byteArray}
*/
private static String hexEncode(byte... byteArray) {
final StringBuilder buf = new StringBuilder(2 * byteArray.length);
for (final byte b : byteArray) {
final int low = b & MASK_0X0F;
final int high = b >> SHIFT_4 & MASK_0X0F;
buf.append(HEX_CHARS[high]);
buf.append(HEX_CHARS[low]);
}
return buf.toString();
}
}