From 6103d96e8334e382b2b1c13b32c17e948a23bc60 Mon Sep 17 00:00:00 2001 From: Jason Tedor Date: Sun, 4 Oct 2015 16:25:15 -0400 Subject: [PATCH] Use abstraction from core Original commit: elastic/x-pack-elasticsearch@01b66264e0248277f0eb07194c0b4665833c4cb6 --- .../renderer/cluster/ClusterInfoRenderer.java | 36 ++----------------- 1 file changed, 3 insertions(+), 33 deletions(-) diff --git a/marvel/src/main/java/org/elasticsearch/marvel/agent/renderer/cluster/ClusterInfoRenderer.java b/marvel/src/main/java/org/elasticsearch/marvel/agent/renderer/cluster/ClusterInfoRenderer.java index d723f7b3093..be559aea3a5 100644 --- a/marvel/src/main/java/org/elasticsearch/marvel/agent/renderer/cluster/ClusterInfoRenderer.java +++ b/marvel/src/main/java/org/elasticsearch/marvel/agent/renderer/cluster/ClusterInfoRenderer.java @@ -7,6 +7,7 @@ package org.elasticsearch.marvel.agent.renderer.cluster; import org.elasticsearch.ElasticsearchException; import org.elasticsearch.action.admin.cluster.stats.ClusterStatsResponse; +import org.elasticsearch.common.hash.MessageDigests; import org.elasticsearch.common.xcontent.ToXContent; import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentBuilderString; @@ -16,34 +17,12 @@ import org.elasticsearch.marvel.agent.renderer.AbstractRenderer; import java.io.IOException; import java.io.UnsupportedEncodingException; +import java.nio.charset.StandardCharsets; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; import java.util.List; public class ClusterInfoRenderer extends AbstractRenderer { - - static final class MessageDigestSHA256Provider { - private static final MessageDigest digest; - - static { - try { - digest = MessageDigest.getInstance("SHA-256"); - } catch (NoSuchAlgorithmException e) { - throw new ElasticsearchException("Unexpected exception creating MessageDigest instance", e); - } - } - - private static MessageDigest sha256() { - try { - MessageDigest sha = (MessageDigest) digest.clone(); - sha.reset(); - return sha; - } catch (CloneNotSupportedException e) { - throw new ElasticsearchException("Unexpected exception creating MessageDigest instance", e); - } - } - } - public ClusterInfoRenderer() { super(null, false); } @@ -88,16 +67,7 @@ public class ClusterInfoRenderer extends AbstractRenderer public static String hash(String licenseStatus, String licenseUid, String licenseType, String licenseExpiryDate, String clusterUUID) { String toHash = licenseStatus + licenseUid + licenseType + licenseExpiryDate + clusterUUID; - try { - byte[] sha256Bytes = MessageDigestSHA256Provider.sha256().digest(toHash.getBytes("UTF-8")); - StringBuffer hash = new StringBuffer(); - for (int i = 0; i < sha256Bytes.length; i++) { - hash.append(Integer.toHexString(0xff & sha256Bytes[i])); - } - return hash.toString(); - } catch (UnsupportedEncodingException e) { - throw new ElasticsearchException("Unexpected error calculating SHA-256 MessageDigest", e); - } + return MessageDigests.toHexString(MessageDigests.sha256().digest(toHash.getBytes(StandardCharsets.UTF_8))); } static final class Fields {