diff --git a/client/src/main/java/com/alibaba/nacos/client/config/impl/CacheData.java b/client/src/main/java/com/alibaba/nacos/client/config/impl/CacheData.java index 471ccb2ef..da2af3499 100644 --- a/client/src/main/java/com/alibaba/nacos/client/config/impl/CacheData.java +++ b/client/src/main/java/com/alibaba/nacos/client/config/impl/CacheData.java @@ -23,7 +23,7 @@ import com.alibaba.nacos.api.exception.NacosException; import com.alibaba.nacos.client.config.filter.impl.ConfigFilterChainManager; import com.alibaba.nacos.client.config.filter.impl.ConfigResponse; import com.alibaba.nacos.client.config.listener.impl.AbstractConfigChangeListener; -import com.alibaba.nacos.client.config.utils.MD5; +import com.alibaba.nacos.common.utils.MD5Utils; import com.alibaba.nacos.client.utils.LogUtils; import com.alibaba.nacos.client.utils.TenantUtil; import org.slf4j.Logger; @@ -242,7 +242,7 @@ public class CacheData { } static public String getMd5String(String config) { - return (null == config) ? Constants.NULL : MD5.getInstance().getMD5String(config); + return (null == config) ? Constants.NULL : MD5Utils.md5Hex(config, Constants.ENCODE); } private String loadCacheContentFromDiskLocal(String name, String dataId, String group, String tenant) { diff --git a/client/src/main/java/com/alibaba/nacos/client/config/impl/ClientWorker.java b/client/src/main/java/com/alibaba/nacos/client/config/impl/ClientWorker.java index f17ddccb3..10c12d7fc 100644 --- a/client/src/main/java/com/alibaba/nacos/client/config/impl/ClientWorker.java +++ b/client/src/main/java/com/alibaba/nacos/client/config/impl/ClientWorker.java @@ -25,7 +25,7 @@ import com.alibaba.nacos.client.config.filter.impl.ConfigFilterChainManager; import com.alibaba.nacos.client.config.http.HttpAgent; import com.alibaba.nacos.client.config.impl.HttpSimpleClient.HttpResult; import com.alibaba.nacos.client.config.utils.ContentUtils; -import com.alibaba.nacos.client.config.utils.MD5; +import com.alibaba.nacos.common.utils.MD5Utils; import com.alibaba.nacos.client.monitor.MetricsMonitor; import com.alibaba.nacos.client.utils.LogUtils; import com.alibaba.nacos.client.utils.ParamUtil; @@ -279,7 +279,7 @@ public class ClientWorker { // 没有 -> 有 if (!cacheData.isUseLocalConfigInfo() && path.exists()) { String content = LocalConfigInfoProcessor.getFailover(agent.getName(), dataId, group, tenant); - String md5 = MD5.getInstance().getMD5String(content); + String md5 = MD5Utils.md5Hex(content, Constants.ENCODE); cacheData.setUseLocalConfigInfo(true); cacheData.setLocalConfigInfoVersion(path.lastModified()); cacheData.setContent(content); @@ -301,7 +301,7 @@ public class ClientWorker { if (cacheData.isUseLocalConfigInfo() && path.exists() && cacheData.getLocalConfigInfoVersion() != path.lastModified()) { String content = LocalConfigInfoProcessor.getFailover(agent.getName(), dataId, group, tenant); - String md5 = MD5.getInstance().getMD5String(content); + String md5 = MD5Utils.md5Hex(content, Constants.ENCODE); cacheData.setUseLocalConfigInfo(true); cacheData.setLocalConfigInfoVersion(path.lastModified()); cacheData.setContent(content); diff --git a/client/src/main/java/com/alibaba/nacos/client/config/impl/HttpSimpleClient.java b/client/src/main/java/com/alibaba/nacos/client/config/impl/HttpSimpleClient.java index cf7398fae..faa77b41c 100644 --- a/client/src/main/java/com/alibaba/nacos/client/config/impl/HttpSimpleClient.java +++ b/client/src/main/java/com/alibaba/nacos/client/config/impl/HttpSimpleClient.java @@ -17,7 +17,7 @@ package com.alibaba.nacos.client.config.impl; import com.alibaba.nacos.api.common.Constants; import com.alibaba.nacos.api.exception.NacosException; -import com.alibaba.nacos.client.config.utils.MD5; +import com.alibaba.nacos.common.utils.MD5Utils; import com.alibaba.nacos.client.utils.ParamUtil; import com.alibaba.nacos.common.constant.HttpHeaderConsts; import com.alibaba.nacos.common.utils.IoUtils; @@ -42,8 +42,8 @@ public class HttpSimpleClient { String encoding, long readTimeoutMs, boolean isSSL) throws IOException { String encodedContent = encodingParams(paramValues, encoding); url += (null == encodedContent) ? "" : ("?" + encodedContent); - if (Limiter.isLimit(MD5.getInstance().getMD5String( - new StringBuilder(url).append(encodedContent).toString()))) { + if (Limiter.isLimit(MD5Utils.md5Hex( + new StringBuilder(url).append(encodedContent).toString(), Constants.ENCODE))) { return new HttpResult(NacosException.CLIENT_OVER_THRESHOLD, "More than client-side current limit threshold"); } @@ -98,8 +98,8 @@ public class HttpSimpleClient { String encoding, long readTimeoutMs, boolean isSSL) throws IOException { String encodedContent = encodingParams(paramValues, encoding); encodedContent = (null == encodedContent) ? "" : encodedContent; - if (Limiter.isLimit(MD5.getInstance().getMD5String( - new StringBuilder(url).append(encodedContent).toString()))) { + if (Limiter.isLimit(MD5Utils.md5Hex( + new StringBuilder(url).append(encodedContent).toString(), Constants.ENCODE))) { return new HttpResult(NacosException.CLIENT_OVER_THRESHOLD, "More than client-side current limit threshold"); } @@ -150,8 +150,8 @@ public class HttpSimpleClient { String encoding, long readTimeoutMs, boolean isSSL) throws IOException { String encodedContent = encodingParams(paramValues, encoding); url += (null == encodedContent) ? "" : ("?" + encodedContent); - if (Limiter.isLimit(MD5.getInstance().getMD5String( - new StringBuilder(url).append(encodedContent).toString()))) { + if (Limiter.isLimit(MD5Utils.md5Hex( + new StringBuilder(url).append(encodedContent).toString(), Constants.ENCODE))) { return new HttpResult(NacosException.CLIENT_OVER_THRESHOLD, "More than client-side current limit threshold"); } @@ -197,7 +197,7 @@ public class HttpSimpleClient { conn.addRequestProperty("Content-Type", "application/x-www-form-urlencoded;charset=" + encoding); String ts = String.valueOf(System.currentTimeMillis()); - String token = MD5.getInstance().getMD5String(ts + ParamUtil.getAppKey()); + String token = MD5Utils.md5Hex(ts + ParamUtil.getAppKey(), Constants.ENCODE); conn.addRequestProperty(Constants.CLIENT_APPNAME_HEADER, ParamUtil.getAppName()); conn.addRequestProperty(Constants.CLIENT_REQUEST_TS_HEADER, ts); diff --git a/client/src/main/java/com/alibaba/nacos/client/config/utils/MD5.java b/client/src/main/java/com/alibaba/nacos/client/config/utils/MD5.java deleted file mode 100644 index 1bba03994..000000000 --- a/client/src/main/java/com/alibaba/nacos/client/config/utils/MD5.java +++ /dev/null @@ -1,132 +0,0 @@ -/* - * Copyright 1999-2018 Alibaba Group Holding Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package com.alibaba.nacos.client.config.utils; - -import com.alibaba.nacos.api.common.Constants; -import com.google.common.collect.Maps; - -import java.io.UnsupportedEncodingException; -import java.security.MessageDigest; -import java.util.Map; -import java.util.concurrent.locks.ReentrantLock; - -/** - * MD5 util - * - * @author Nacos - */ -@SuppressWarnings("PMD.ClassNamingShouldBeCamelRule") -public class MD5 { - private static int DIGITS_SIZE = 16; - private static char[] digits = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'}; - - private static Map rDigits = Maps.newHashMapWithExpectedSize(16); - - static { - for (int i = 0; i < digits.length; ++i) { - rDigits.put(digits[i], i); - } - } - - private static MD5 me = new MD5(); - private MessageDigest mHasher; - private ReentrantLock opLock = new ReentrantLock(); - - private MD5() { - try { - mHasher = MessageDigest.getInstance("md5"); - } catch (Exception e) { - throw new RuntimeException(e); - } - - } - - public static MD5 getInstance() { - return me; - } - - public String getMD5String(String content) { - return bytes2string(hash(content)); - } - - public String getMD5String(byte[] content) { - return bytes2string(hash(content)); - } - - public byte[] getMD5Bytes(byte[] content) { - return hash(content); - } - - /** - * 对字符串进行md5 - * - * @param str - * @return md5 byte[16] - */ - public byte[] hash(String str) { - opLock.lock(); - try { - byte[] bt = mHasher.digest(str.getBytes(Constants.ENCODE)); - if (null == bt || bt.length != DIGITS_SIZE) { - throw new IllegalArgumentException("md5 need"); - } - return bt; - } catch (UnsupportedEncodingException e) { - throw new RuntimeException("unsupported utf-8 encoding", e); - } finally { - opLock.unlock(); - } - } - - /** - * 对二进制数据进行md5 - * - * @param data - * @return md5 byte[16] - */ - public byte[] hash(byte[] data) { - opLock.lock(); - try { - byte[] bt = mHasher.digest(data); - if (null == bt || bt.length != DIGITS_SIZE) { - throw new IllegalArgumentException("md5 need"); - } - return bt; - } finally { - opLock.unlock(); - } - } - - /** - * 将一个字节数组转化为可见的字符串 - * - * @param bt - * @return - */ - public String bytes2string(byte[] bt) { - int l = bt.length; - - char[] out = new char[l << 1]; - - for (int i = 0, j = 0; i < l; i++) { - out[j++] = digits[(0xF0 & bt[i]) >>> 4]; - out[j++] = digits[0x0F & bt[i]]; - } - - return new String(out); - } - -} diff --git a/client/src/test/java/com/alibaba/nacos/client/config/utils/MD5Test.java b/client/src/test/java/com/alibaba/nacos/client/config/utils/MD5Test.java deleted file mode 100644 index 21ca3b98e..000000000 --- a/client/src/test/java/com/alibaba/nacos/client/config/utils/MD5Test.java +++ /dev/null @@ -1,71 +0,0 @@ -/* - * Copyright 1999-2019 Alibaba Group Holding Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package com.alibaba.nacos.client.config.utils; - -import org.junit.Assert; -import org.junit.Test; - -public class MD5Test { - - @Test - public void testGetMD5String() { - Assert.assertEquals("d41d8cd98f00b204e9800998ecf8427e", - MD5.getInstance().getMD5String("")); - Assert.assertEquals("acbd18db4cc2f85cedef654fccc4a4d8", - MD5.getInstance().getMD5String("foo")); - - Assert.assertEquals("d41d8cd98f00b204e9800998ecf8427e", - MD5.getInstance().getMD5String(new byte[0])); - Assert.assertEquals("5289df737df57326fcdd22597afb1fac", - MD5.getInstance().getMD5String(new byte[]{1, 2, 3})); - } - - @Test - public void testGetMD5Bytes() { - byte[] bytes1 = new byte[]{-44, 29, -116, -39, -113, 0, -78, - 4, -23, -128, 9, -104, -20, -8, 66, 126}; - byte[] bytes2 = new byte[]{82, -119, -33, 115, 125, -11, 115, - 38, -4, -35, 34, 89, 122, -5, 31, -84}; - - Assert.assertArrayEquals(bytes1, - MD5.getInstance().getMD5Bytes(new byte[0])); - Assert.assertArrayEquals(bytes2, - MD5.getInstance().getMD5Bytes(new byte[]{1, 2, 3})); - } - - @Test - public void testHash() { - byte[] bytes1 = new byte[]{-44, 29, -116, -39, -113, 0, -78, - 4, -23, -128, 9, -104, -20, -8, 66, 126}; - byte[] bytes2 = new byte[]{-84, -67, 24, -37, 76, -62, -8, 92, - -19, -17, 101, 79, -52, -60, -92, -40}; - byte[] bytes3 = new byte[]{82, -119, -33, 115, 125, -11, 115, - 38, -4, -35, 34, 89, 122, -5, 31, -84}; - - Assert.assertArrayEquals(bytes1, MD5.getInstance().hash("")); - Assert.assertArrayEquals(bytes2, MD5.getInstance().hash("foo")); - Assert.assertArrayEquals(bytes1, MD5.getInstance().hash(new byte[0])); - Assert.assertArrayEquals(bytes3, - MD5.getInstance().hash(new byte[]{1, 2, 3})); - } - - @Test - public void testBytes2string() { - Assert.assertEquals("", MD5.getInstance().bytes2string(new byte[0])); - Assert.assertEquals("010203", - MD5.getInstance().bytes2string(new byte[]{1, 2, 3})); - } -} diff --git a/common/src/main/java/com/alibaba/nacos/common/utils/Md5Utils.java b/common/src/main/java/com/alibaba/nacos/common/utils/MD5Utils.java similarity index 63% rename from common/src/main/java/com/alibaba/nacos/common/utils/Md5Utils.java rename to common/src/main/java/com/alibaba/nacos/common/utils/MD5Utils.java index 1dad4e1ba..abe3e3c6a 100644 --- a/common/src/main/java/com/alibaba/nacos/common/utils/Md5Utils.java +++ b/common/src/main/java/com/alibaba/nacos/common/utils/MD5Utils.java @@ -15,16 +15,18 @@ */ package com.alibaba.nacos.common.utils; -import java.math.BigInteger; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; /** - * MD5 generator + * MD5 util * - * @author nacos + *@author nacos */ -public class Md5Utils { +@SuppressWarnings("PMD.ClassNamingShouldBeCamelRule") +public class MD5Utils { + + private static final char[] DIGITS_LOWER = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'}; private static ThreadLocal MESSAGE_DIGEST_LOCAL = new ThreadLocal() { @Override @@ -37,13 +39,11 @@ public class Md5Utils { } }; - private static final int HEX_VALUE_COUNT = 16; - - public static String getMD5(byte[] bytes) throws NoSuchAlgorithmException { + public static String md5Hex(byte[] bytes) throws NoSuchAlgorithmException { try { MessageDigest messageDigest = MESSAGE_DIGEST_LOCAL.get(); if (messageDigest != null) { - return new BigInteger(1, messageDigest.digest(bytes)).toString(HEX_VALUE_COUNT); + return encodeHexString(messageDigest.digest(bytes)); } throw new NoSuchAlgorithmException("MessageDigest get MD5 instance error"); } finally { @@ -51,11 +51,28 @@ public class Md5Utils { } } - public static String getMD5(String value, String encode) { + public static String md5Hex(String value,String encode) { try { - return getMD5(value.getBytes(encode)); + return md5Hex(value.getBytes(encode)); } catch (Exception e) { throw new RuntimeException(e); } } + + /** + * 将一个字节数组转化为可见的字符串 + */ + public static String encodeHexString(byte[] bytes) { + int l = bytes.length; + + char[] out = new char[l << 1]; + + for (int i = 0, j = 0; i < l; i++) { + out[j++] = DIGITS_LOWER[(0xF0 & bytes[i]) >>> 4]; + out[j++] = DIGITS_LOWER[0x0F & bytes[i]]; + } + + return new String(out); + } + } diff --git a/common/src/test/java/com/alibaba/nacos/common/utils/MD5UtilsTest.java b/common/src/test/java/com/alibaba/nacos/common/utils/MD5UtilsTest.java new file mode 100644 index 000000000..ffe76c1fb --- /dev/null +++ b/common/src/test/java/com/alibaba/nacos/common/utils/MD5UtilsTest.java @@ -0,0 +1,48 @@ +/* + * Copyright 1999-2019 Alibaba Group Holding Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.alibaba.nacos.common.utils; + +import com.alibaba.nacos.api.common.Constants; +import java.security.NoSuchAlgorithmException; +import org.junit.Assert; +import org.junit.Test; + +public class MD5UtilsTest { + + @Test + public void testMd5Hex() throws NoSuchAlgorithmException { + Assert.assertEquals("d41d8cd98f00b204e9800998ecf8427e", + MD5Utils.md5Hex("", Constants.ENCODE)); + Assert.assertEquals("acbd18db4cc2f85cedef654fccc4a4d8", + MD5Utils.md5Hex("foo",Constants.ENCODE)); + Assert.assertEquals("02f463eb799797e2a978fb1a2ae2991e", + MD5Utils.md5Hex("38c5ee9532f037a20b93d0f804cf111fca4003e451d09a692d9dea8032308d9c64eda9047fcd5e850284a49b1a0cfb2ecd45",Constants.ENCODE)); + + Assert.assertEquals("d41d8cd98f00b204e9800998ecf8427e", + MD5Utils.md5Hex(new byte[0])); + Assert.assertEquals("5289df737df57326fcdd22597afb1fac", + MD5Utils.md5Hex(new byte[]{1, 2, 3})); + } + + + + @Test + public void testEncodeHexString() { + Assert.assertEquals("", MD5Utils.encodeHexString(new byte[0])); + Assert.assertEquals("010203", + MD5Utils.encodeHexString(new byte[]{1, 2, 3})); + } +} diff --git a/config/src/main/java/com/alibaba/nacos/config/server/aspect/RequestLogAspect.java b/config/src/main/java/com/alibaba/nacos/config/server/aspect/RequestLogAspect.java index ee6092c61..432a32478 100755 --- a/config/src/main/java/com/alibaba/nacos/config/server/aspect/RequestLogAspect.java +++ b/config/src/main/java/com/alibaba/nacos/config/server/aspect/RequestLogAspect.java @@ -15,7 +15,7 @@ */ package com.alibaba.nacos.config.server.aspect; -import com.alibaba.nacos.common.utils.Md5Utils; +import com.alibaba.nacos.common.utils.MD5Utils; import com.alibaba.nacos.config.server.constant.Constants; import com.alibaba.nacos.config.server.monitor.MetricsMonitor; import com.alibaba.nacos.config.server.service.ConfigService; @@ -67,7 +67,7 @@ public class RequestLogAspect { public Object interfacePublishSingle(ProceedingJoinPoint pjp, HttpServletRequest request, HttpServletResponse response, String dataId, String group, String tenant, String content) throws Throwable { - final String md5 = content == null ? null : Md5Utils.getMD5(content, Constants.ENCODE); + final String md5 = content == null ? null : MD5Utils.md5Hex(content, Constants.ENCODE); MetricsMonitor.getPublishMonitor().incrementAndGet(); return logClientRequest("publish", pjp, request, response, dataId, group, tenant, md5); } diff --git a/config/src/main/java/com/alibaba/nacos/config/server/model/ConfigInfoBase.java b/config/src/main/java/com/alibaba/nacos/config/server/model/ConfigInfoBase.java index a6cf69449..d7dc640ca 100644 --- a/config/src/main/java/com/alibaba/nacos/config/server/model/ConfigInfoBase.java +++ b/config/src/main/java/com/alibaba/nacos/config/server/model/ConfigInfoBase.java @@ -15,7 +15,7 @@ */ package com.alibaba.nacos.config.server.model; -import com.alibaba.nacos.common.utils.Md5Utils; +import com.alibaba.nacos.common.utils.MD5Utils; import com.alibaba.nacos.config.server.constant.Constants; import java.io.PrintWriter; @@ -47,7 +47,7 @@ public class ConfigInfoBase implements Serializable, Comparable this.group = group; this.content = content; if (this.content != null) { - this.md5 = Md5Utils.getMD5(this.content, Constants.ENCODE); + this.md5 = MD5Utils.md5Hex(this.content, Constants.ENCODE); } } diff --git a/config/src/main/java/com/alibaba/nacos/config/server/service/ConfigService.java b/config/src/main/java/com/alibaba/nacos/config/server/service/ConfigService.java index 8c9fa1154..6274c0b75 100644 --- a/config/src/main/java/com/alibaba/nacos/config/server/service/ConfigService.java +++ b/config/src/main/java/com/alibaba/nacos/config/server/service/ConfigService.java @@ -15,7 +15,7 @@ */ package com.alibaba.nacos.config.server.service; -import com.alibaba.nacos.common.utils.Md5Utils; +import com.alibaba.nacos.common.utils.MD5Utils; import com.alibaba.nacos.config.server.constant.Constants; import com.alibaba.nacos.config.server.model.CacheItem; import com.alibaba.nacos.config.server.model.ConfigInfoBase; @@ -70,7 +70,7 @@ public class ConfigService { } try { - final String md5 = Md5Utils.getMD5(content, Constants.ENCODE); + final String md5 = MD5Utils.md5Hex(content, Constants.ENCODE); if (md5.equals(ConfigService.getContentMd5(groupKey))) { dumpLog.warn( @@ -116,7 +116,7 @@ public class ConfigService { } try { - final String md5 = Md5Utils.getMD5(content, Constants.ENCODE); + final String md5 = MD5Utils.md5Hex(content, Constants.ENCODE); if (md5.equals(ConfigService.getContentBetaMd5(groupKey))) { dumpLog.warn( "[dump-beta-ignore] ignore to save cache file. groupKey={}, md5={}, lastModifiedOld={}, " @@ -155,7 +155,7 @@ public class ConfigService { } try { - final String md5 = Md5Utils.getMD5(content, Constants.ENCODE); + final String md5 = MD5Utils.md5Hex(content, Constants.ENCODE); if (md5.equals(ConfigService.getContentTagMd5(groupKey, tag))) { dumpLog.warn( "[dump-tag-ignore] ignore to save cache file. groupKey={}, md5={}, lastModifiedOld={}, " @@ -192,7 +192,7 @@ public class ConfigService { } try { - final String md5 = Md5Utils.getMD5(content, Constants.ENCODE); + final String md5 = MD5Utils.md5Hex(content, Constants.ENCODE); if (!STANDALONE_MODE || PropertyUtil.isStandaloneUseMysql()) { String loacalMd5 = DiskUtil.getLocalConfigMd5(dataId, group, tenant); if (md5.equals(loacalMd5)) { diff --git a/config/src/main/java/com/alibaba/nacos/config/server/service/DiskUtil.java b/config/src/main/java/com/alibaba/nacos/config/server/service/DiskUtil.java index 5d0450f0e..549c0e9bd 100644 --- a/config/src/main/java/com/alibaba/nacos/config/server/service/DiskUtil.java +++ b/config/src/main/java/com/alibaba/nacos/config/server/service/DiskUtil.java @@ -16,7 +16,7 @@ package com.alibaba.nacos.config.server.service; import com.alibaba.nacos.common.utils.IoUtils; -import com.alibaba.nacos.common.utils.Md5Utils; +import com.alibaba.nacos.common.utils.MD5Utils; import com.alibaba.nacos.config.server.constant.Constants; import com.alibaba.nacos.config.server.utils.LogUtil; import org.apache.commons.io.FileUtils; @@ -172,7 +172,7 @@ public class DiskUtil { static public String getLocalConfigMd5(String dataId, String group, String tenant) throws IOException { - return Md5Utils.getMD5(getConfig(dataId, group, tenant), Constants.ENCODE); + return MD5Utils.md5Hex(getConfig(dataId, group, tenant), Constants.ENCODE); } static public File heartBeatFile() { diff --git a/config/src/main/java/com/alibaba/nacos/config/server/service/PersistService.java b/config/src/main/java/com/alibaba/nacos/config/server/service/PersistService.java index 82635f2c6..187009bef 100755 --- a/config/src/main/java/com/alibaba/nacos/config/server/service/PersistService.java +++ b/config/src/main/java/com/alibaba/nacos/config/server/service/PersistService.java @@ -15,8 +15,8 @@ */ package com.alibaba.nacos.config.server.service; -import com.alibaba.nacos.common.utils.Md5Utils; import com.alibaba.nacos.config.server.constant.Constants; +import com.alibaba.nacos.common.utils.MD5Utils; import com.alibaba.nacos.config.server.enums.FileTypeEnum; import com.alibaba.nacos.api.exception.NacosException; import com.alibaba.nacos.config.server.model.*; @@ -538,7 +538,7 @@ public class PersistService { String appNameTmp = StringUtils.isBlank(configInfo.getAppName()) ? StringUtils.EMPTY : configInfo.getAppName(); String tenantTmp = StringUtils.isBlank(configInfo.getTenant()) ? StringUtils.EMPTY : configInfo.getTenant(); try { - String md5 = Md5Utils.getMD5(configInfo.getContent(), Constants.ENCODE); + String md5 = MD5Utils.md5Hex(configInfo.getContent(), Constants.ENCODE); jt.update( "INSERT INTO config_info_beta(data_id,group_id,tenant_id,app_name,content,md5,beta_ips,src_ip," + "src_user,gmt_create,gmt_modified) VALUES(?,?,?,?,?,?,?,?,?,?,?)", @@ -564,7 +564,7 @@ public class PersistService { String tenantTmp = StringUtils.isBlank(configInfo.getTenant()) ? StringUtils.EMPTY : configInfo.getTenant(); String tagTmp = StringUtils.isBlank(tag) ? StringUtils.EMPTY : tag.trim(); try { - String md5 = Md5Utils.getMD5(configInfo.getContent(), Constants.ENCODE); + String md5 = MD5Utils.md5Hex(configInfo.getContent(), Constants.ENCODE); jt.update( "INSERT INTO config_info_tag(data_id,group_id,tenant_id,tag_id,app_name,content,md5,src_ip,src_user," + "gmt_create,gmt_modified) VALUES(?,?,?,?,?,?,?,?,?,?,?)", @@ -628,7 +628,7 @@ public class PersistService { String appNameTmp = StringUtils.isBlank(configInfo.getAppName()) ? StringUtils.EMPTY : configInfo.getAppName(); String tenantTmp = StringUtils.isBlank(configInfo.getTenant()) ? StringUtils.EMPTY : configInfo.getTenant(); try { - String md5 = Md5Utils.getMD5(configInfo.getContent(), Constants.ENCODE); + String md5 = MD5Utils.md5Hex(configInfo.getContent(), Constants.ENCODE); jt.update( "UPDATE config_info_beta SET content=?, md5 = ?, src_ip=?,src_user=?,gmt_modified=?,app_name=? WHERE " + "data_id=? AND group_id=? AND tenant_id=?", @@ -654,7 +654,7 @@ public class PersistService { String tenantTmp = StringUtils.isBlank(configInfo.getTenant()) ? StringUtils.EMPTY : configInfo.getTenant(); String tagTmp = StringUtils.isBlank(tag) ? StringUtils.EMPTY : tag.trim(); try { - String md5 = Md5Utils.getMD5(configInfo.getContent(), Constants.ENCODE); + String md5 = MD5Utils.md5Hex(configInfo.getContent(), Constants.ENCODE); jt.update( "UPDATE config_info_tag SET content=?, md5 = ?, src_ip=?,src_user=?,gmt_modified=?,app_name=? WHERE " + "data_id=? AND group_id=? AND tenant_id=? AND tag_id=?", @@ -2695,7 +2695,7 @@ public class PersistService { final String type = configAdvanceInfo == null ? null : (String) configAdvanceInfo.get("type"); final String schema = configAdvanceInfo == null ? null : (String) configAdvanceInfo.get("schema"); - final String md5Tmp = Md5Utils.getMD5(configInfo.getContent(), Constants.ENCODE); + final String md5Tmp = MD5Utils.md5Hex(configInfo.getContent(), Constants.ENCODE); KeyHolder keyHolder = new GeneratedKeyHolder(); @@ -2904,7 +2904,7 @@ public class PersistService { final Timestamp time, Map configAdvanceInfo) { String appNameTmp = StringUtils.isBlank(configInfo.getAppName()) ? StringUtils.EMPTY : configInfo.getAppName(); String tenantTmp = StringUtils.isBlank(configInfo.getTenant()) ? StringUtils.EMPTY : configInfo.getTenant(); - final String md5Tmp = Md5Utils.getMD5(configInfo.getContent(), Constants.ENCODE); + final String md5Tmp = MD5Utils.md5Hex(configInfo.getContent(), Constants.ENCODE); String desc = configAdvanceInfo == null ? null : (String) configAdvanceInfo.get("desc"); String use = configAdvanceInfo == null ? null : (String) configAdvanceInfo.get("use"); String effect = configAdvanceInfo == null ? null : (String) configAdvanceInfo.get("effect"); @@ -3061,7 +3061,7 @@ public class PersistService { final Timestamp time, String ops) { String appNameTmp = StringUtils.isBlank(configInfo.getAppName()) ? StringUtils.EMPTY : configInfo.getAppName(); String tenantTmp = StringUtils.isBlank(configInfo.getTenant()) ? StringUtils.EMPTY : configInfo.getTenant(); - final String md5Tmp = Md5Utils.getMD5(configInfo.getContent(), Constants.ENCODE); + final String md5Tmp = MD5Utils.md5Hex(configInfo.getContent(), Constants.ENCODE); try { jt.update( "INSERT INTO his_config_info (id,data_id,group_id,tenant_id,app_name,content,md5,src_ip,src_user,gmt_modified,op_type) VALUES(?,?,?,?,?,?,?,?,?,?,?)", @@ -3355,7 +3355,7 @@ public class PersistService { String md5InDb = cf.getMd5(); final String content = cf.getContent(); final String tenant = cf.getTenant(); - final String md5 = Md5Utils.getMD5(content, Constants.ENCODE); + final String md5 = MD5Utils.md5Hex(content, Constants.ENCODE); if (StringUtils.isBlank(md5InDb)) { try { updateMd5(cf.getDataId(), cf.getGroup(), tenant, md5, new Timestamp(cf.getLastModified())); diff --git a/config/src/main/java/com/alibaba/nacos/config/server/service/dump/DumpService.java b/config/src/main/java/com/alibaba/nacos/config/server/service/dump/DumpService.java index 0a59ceadb..ff91eac7f 100755 --- a/config/src/main/java/com/alibaba/nacos/config/server/service/dump/DumpService.java +++ b/config/src/main/java/com/alibaba/nacos/config/server/service/dump/DumpService.java @@ -16,7 +16,7 @@ package com.alibaba.nacos.config.server.service.dump; import com.alibaba.nacos.common.utils.IoUtils; -import com.alibaba.nacos.common.utils.Md5Utils; +import com.alibaba.nacos.common.utils.MD5Utils; import com.alibaba.nacos.config.server.constant.Constants; import com.alibaba.nacos.config.server.manager.TaskManager; import com.alibaba.nacos.config.server.model.ConfigInfo; @@ -67,7 +67,7 @@ public class DumpService { PersistService persistService; @PostConstruct - public void init() { + public void init() { LogUtil.defaultLog.warn("DumpService start"); DumpProcessor processor = new DumpProcessor(this); DumpAllProcessor dumpAllProcessor = new DumpAllProcessor(this); @@ -347,7 +347,7 @@ public class DumpService { ConfigInfo cf = MergeTaskProcessor.merge(dataId, group, tenant, datumList); String aggrContent = cf.getContent(); String localContentMD5 = ConfigService.getContentMd5(GroupKey.getKey(dataId, group)); - String aggrConetentMD5 = Md5Utils.getMD5(aggrContent, Constants.ENCODE); + String aggrConetentMD5 = MD5Utils.md5Hex(aggrContent, Constants.ENCODE); if (!StringUtils.equals(localContentMD5, aggrConetentMD5)) { persistService.insertOrUpdate(null, null, cf, time, null, false); diff --git a/config/src/main/java/com/alibaba/nacos/config/server/service/dump/DumpTask.java b/config/src/main/java/com/alibaba/nacos/config/server/service/dump/DumpTask.java index e3479f58b..dc2d48cf5 100755 --- a/config/src/main/java/com/alibaba/nacos/config/server/service/dump/DumpTask.java +++ b/config/src/main/java/com/alibaba/nacos/config/server/service/dump/DumpTask.java @@ -15,7 +15,7 @@ */ package com.alibaba.nacos.config.server.service.dump; -import com.alibaba.nacos.common.utils.Md5Utils; +import com.alibaba.nacos.common.utils.MD5Utils; import com.alibaba.nacos.config.server.constant.Constants; import com.alibaba.nacos.config.server.manager.AbstractTask; import com.alibaba.nacos.config.server.manager.TaskProcessor; @@ -265,7 +265,7 @@ class DumpAllProcessor implements TaskProcessor { cf.getLastModified(), cf.getType()); final String content = cf.getContent(); - final String md5 = Md5Utils.getMD5(content, Constants.ENCODE); + final String md5 = MD5Utils.md5Hex(content, Constants.ENCODE); LogUtil.dumpLog.info("[dump-all-ok] {}, {}, length={}, md5={}", GroupKey2.getKey(cf.getDataId(), cf.getGroup()), cf.getLastModified(), content.length(), md5); } @@ -410,7 +410,7 @@ class DumpChangeProcessor implements TaskProcessor { boolean result = ConfigService.dumpChange(cf.getDataId(), cf.getGroup(), cf.getTenant(), cf.getContent(), cf.getLastModified()); final String content = cf.getContent(); - final String md5 = Md5Utils.getMD5(content, Constants.ENCODE); + final String md5 = MD5Utils.md5Hex(content, Constants.ENCODE); LogUtil.defaultLog.info( "[dump-change-ok] {}, {}, length={}, md5={}", new Object[] { diff --git a/config/src/main/java/com/alibaba/nacos/config/server/service/trace/ConfigTraceService.java b/config/src/main/java/com/alibaba/nacos/config/server/service/trace/ConfigTraceService.java index 1775cf893..bc6af42a8 100644 --- a/config/src/main/java/com/alibaba/nacos/config/server/service/trace/ConfigTraceService.java +++ b/config/src/main/java/com/alibaba/nacos/config/server/service/trace/ConfigTraceService.java @@ -15,7 +15,7 @@ */ package com.alibaba.nacos.config.server.service.trace; -import com.alibaba.nacos.common.utils.Md5Utils; +import com.alibaba.nacos.common.utils.MD5Utils; import com.alibaba.nacos.config.server.constant.Constants; import com.alibaba.nacos.config.server.monitor.MetricsMonitor; import com.alibaba.nacos.config.server.utils.LogUtil; @@ -62,7 +62,7 @@ public class ConfigTraceService { } //localIp | dataid | group | tenant | requestIpAppName | ts | handleIp | event | type | [delayed = -1] | ext // (md5) - String md5 = content == null ? null : Md5Utils.getMD5(content, Constants.ENCODE); + String md5 = content == null ? null : MD5Utils.md5Hex(content, Constants.ENCODE); LogUtil.traceLog.info("{}|{}|{}|{}|{}|{}|{}|{}|{}|{}|{}", LOCAL_IP, dataId, group, tenant, requestIpAppName, ts, handleIp, "persist", type, -1, md5); diff --git a/naming/src/main/java/com/alibaba/nacos/naming/core/Instances.java b/naming/src/main/java/com/alibaba/nacos/naming/core/Instances.java index 55d16bb15..fc6106ee9 100644 --- a/naming/src/main/java/com/alibaba/nacos/naming/core/Instances.java +++ b/naming/src/main/java/com/alibaba/nacos/naming/core/Instances.java @@ -17,7 +17,8 @@ package com.alibaba.nacos.naming.core; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.annotation.JSONField; -import com.alibaba.nacos.common.utils.Md5Utils; +import com.alibaba.nacos.common.utils.MD5Utils; +import com.alibaba.nacos.api.common.Constants; import com.alibaba.nacos.naming.pojo.Record; import org.apache.commons.lang3.StringUtils; @@ -66,7 +67,7 @@ public class Instances implements Record { sb.append(","); } - return Md5Utils.getMD5(sb.toString(), "UTF-8"); + return MD5Utils.md5Hex(sb.toString(), Constants.ENCODE); } public String convertMap2String(Map map) { diff --git a/naming/src/main/java/com/alibaba/nacos/naming/core/Service.java b/naming/src/main/java/com/alibaba/nacos/naming/core/Service.java index 147fe1300..c4e07c2ef 100644 --- a/naming/src/main/java/com/alibaba/nacos/naming/core/Service.java +++ b/naming/src/main/java/com/alibaba/nacos/naming/core/Service.java @@ -17,7 +17,8 @@ package com.alibaba.nacos.naming.core; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.annotation.JSONField; -import com.alibaba.nacos.common.utils.Md5Utils; +import com.alibaba.nacos.common.utils.MD5Utils; +import com.alibaba.nacos.api.common.Constants; import com.alibaba.nacos.naming.boot.SpringContext; import com.alibaba.nacos.naming.consistency.KeyBuilder; import com.alibaba.nacos.naming.consistency.RecordListener; @@ -472,7 +473,7 @@ public class Service extends com.alibaba.nacos.api.naming.pojo.Service implement ipsString.append(","); } - checksum = Md5Utils.getMD5(ipsString.toString(), "UTF-8"); + checksum = MD5Utils.md5Hex(ipsString.toString(), Constants.ENCODE); } private void updateOrAddCluster(Collection clusters) {