use of org.wso2.carbon.apimgt.core.exception.ETagGenerationException in project carbon-apimgt by wso2.
the class ETagUtils method getHash.
/**
* If some other hashing algorithm is needed use this method.
*
* @param updatedTime, the updated/created time of the resource in UNIX time
* @param algorithm the algorithm used for hashing
* @return String
* @throws NoSuchAlgorithmException if the given algorithm is invalid or not found in {@link MessageDigest}
* @throws ETagGenerationException if hash generation failed.
*/
private static String getHash(String updatedTime, String algorithm) throws ETagGenerationException, NoSuchAlgorithmException {
MessageDigest messageDigest = MessageDigest.getInstance(algorithm);
try {
messageDigest.update(updatedTime.getBytes("UTF-8"));
byte[] digest = messageDigest.digest();
// conversion to hexadecimal
StringBuilder sb = new StringBuilder();
for (byte b : digest) {
sb.append(String.format("%02x", b & 0xff));
}
String generatedHash = sb.toString();
if (log.isDebugEnabled()) {
log.debug("ETag generated in HEX '" + generatedHash + "' for '" + updatedTime + "'");
}
return sb.toString();
} catch (UnsupportedEncodingException e) {
String errorMessage = "Error while converting timestamp to String :" + updatedTime;
log.error(errorMessage, e);
throw new ETagGenerationException(errorMessage, e);
}
}
Aggregations