From bb70dcba98aa034fb130ef7542fc6f31d6fc47d0 Mon Sep 17 00:00:00 2001 From: wangwei Date: Sat, 18 Apr 2020 16:35:17 +0800 Subject: [PATCH 1/3] [ISSUE#2618]modify server md5 generation method --- common/pom.xml | 5 +++++ .../main/java/com/alibaba/nacos/common/utils/Md5Utils.java | 6 ++---- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/common/pom.xml b/common/pom.xml index 9b64da6b3..374c00915 100644 --- a/common/pom.xml +++ b/common/pom.xml @@ -44,6 +44,11 @@ commons-io + + commons-codec + commons-codec + + org.apache.commons commons-lang3 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 index 1dad4e1ba..f68dcd8ff 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,9 +15,9 @@ */ package com.alibaba.nacos.common.utils; -import java.math.BigInteger; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; +import org.apache.commons.codec.binary.Hex; /** * MD5 generator @@ -37,13 +37,11 @@ public class Md5Utils { } }; - private static final int HEX_VALUE_COUNT = 16; - public static String getMD5(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 Hex.encodeHexString(messageDigest.digest(bytes)); } throw new NoSuchAlgorithmException("MessageDigest get MD5 instance error"); } finally { From aeb14910e753a9ebe0e5124b417d1d0443817120 Mon Sep 17 00:00:00 2001 From: wangwei Date: Sun, 19 Apr 2020 18:42:59 +0800 Subject: [PATCH 2/3] unify the MD5 generation on both sides by MD5 utility classes in the common module --- .../nacos/client/config/impl/CacheData.java | 2 +- .../client/config/impl/ClientWorker.java | 2 +- .../client/config/impl/HttpSimpleClient.java | 2 +- .../com/alibaba/nacos/common}/utils/MD5.java | 2 +- .../alibaba/nacos/common/utils/Md5Utils.java | 59 ------------------- .../alibaba/nacos/common}/utils/MD5Test.java | 4 +- .../server/aspect/RequestLogAspect.java | 5 +- .../config/server/model/ConfigInfoBase.java | 5 +- .../config/server/service/ConfigService.java | 10 ++-- .../nacos/config/server/service/DiskUtil.java | 4 +- .../config/server/service/PersistService.java | 19 +++--- .../server/service/dump/DumpService.java | 4 +- .../config/server/service/dump/DumpTask.java | 7 +-- .../service/trace/ConfigTraceService.java | 5 +- .../alibaba/nacos/naming/core/Instances.java | 4 +- .../alibaba/nacos/naming/core/Service.java | 4 +- 16 files changed, 38 insertions(+), 100 deletions(-) rename {client/src/main/java/com/alibaba/nacos/client/config => common/src/main/java/com/alibaba/nacos/common}/utils/MD5.java (98%) delete mode 100644 common/src/main/java/com/alibaba/nacos/common/utils/Md5Utils.java rename {client/src/test/java/com/alibaba/nacos/client/config => common/src/test/java/com/alibaba/nacos/common}/utils/MD5Test.java (91%) 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..404a0bf6b 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.MD5; import com.alibaba.nacos.client.utils.LogUtils; import com.alibaba.nacos.client.utils.TenantUtil; import org.slf4j.Logger; 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..782bc10e4 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.MD5; import com.alibaba.nacos.client.monitor.MetricsMonitor; import com.alibaba.nacos.client.utils.LogUtils; import com.alibaba.nacos.client.utils.ParamUtil; 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..6ac906ad2 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.MD5; import com.alibaba.nacos.client.utils.ParamUtil; import com.alibaba.nacos.common.constant.HttpHeaderConsts; import com.alibaba.nacos.common.utils.IoUtils; diff --git a/client/src/main/java/com/alibaba/nacos/client/config/utils/MD5.java b/common/src/main/java/com/alibaba/nacos/common/utils/MD5.java similarity index 98% rename from client/src/main/java/com/alibaba/nacos/client/config/utils/MD5.java rename to common/src/main/java/com/alibaba/nacos/common/utils/MD5.java index 1bba03994..9e62df402 100644 --- a/client/src/main/java/com/alibaba/nacos/client/config/utils/MD5.java +++ b/common/src/main/java/com/alibaba/nacos/common/utils/MD5.java @@ -13,7 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package com.alibaba.nacos.client.config.utils; +package com.alibaba.nacos.common.utils; import com.alibaba.nacos.api.common.Constants; import com.google.common.collect.Maps; 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 deleted file mode 100644 index f68dcd8ff..000000000 --- a/common/src/main/java/com/alibaba/nacos/common/utils/Md5Utils.java +++ /dev/null @@ -1,59 +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.common.utils; - -import java.security.MessageDigest; -import java.security.NoSuchAlgorithmException; -import org.apache.commons.codec.binary.Hex; - -/** - * MD5 generator - * - * @author nacos - */ -public class Md5Utils { - - private static ThreadLocal MESSAGE_DIGEST_LOCAL = new ThreadLocal() { - @Override - protected MessageDigest initialValue() { - try { - return MessageDigest.getInstance("MD5"); - } catch (NoSuchAlgorithmException e) { - return null; - } - } - }; - - public static String getMD5(byte[] bytes) throws NoSuchAlgorithmException { - try { - MessageDigest messageDigest = MESSAGE_DIGEST_LOCAL.get(); - if (messageDigest != null) { - return Hex.encodeHexString(messageDigest.digest(bytes)); - } - throw new NoSuchAlgorithmException("MessageDigest get MD5 instance error"); - } finally { - MESSAGE_DIGEST_LOCAL.remove(); - } - } - - public static String getMD5(String value, String encode) { - try { - return getMD5(value.getBytes(encode)); - } catch (Exception e) { - throw new RuntimeException(e); - } - } -} diff --git a/client/src/test/java/com/alibaba/nacos/client/config/utils/MD5Test.java b/common/src/test/java/com/alibaba/nacos/common/utils/MD5Test.java similarity index 91% rename from client/src/test/java/com/alibaba/nacos/client/config/utils/MD5Test.java rename to common/src/test/java/com/alibaba/nacos/common/utils/MD5Test.java index 21ca3b98e..3bb6bc805 100644 --- a/client/src/test/java/com/alibaba/nacos/client/config/utils/MD5Test.java +++ b/common/src/test/java/com/alibaba/nacos/common/utils/MD5Test.java @@ -13,7 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package com.alibaba.nacos.client.config.utils; +package com.alibaba.nacos.common.utils; import org.junit.Assert; import org.junit.Test; @@ -26,6 +26,8 @@ public class MD5Test { MD5.getInstance().getMD5String("")); Assert.assertEquals("acbd18db4cc2f85cedef654fccc4a4d8", MD5.getInstance().getMD5String("foo")); + Assert.assertEquals("02f463eb799797e2a978fb1a2ae2991e", + MD5.getInstance().getMD5String("38c5ee9532f037a20b93d0f804cf111fca4003e451d09a692d9dea8032308d9c64eda9047fcd5e850284a49b1a0cfb2ecd45")); Assert.assertEquals("d41d8cd98f00b204e9800998ecf8427e", MD5.getInstance().getMD5String(new byte[0])); 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..c1673ae46 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,8 +15,7 @@ */ package com.alibaba.nacos.config.server.aspect; -import com.alibaba.nacos.common.utils.Md5Utils; -import com.alibaba.nacos.config.server.constant.Constants; +import com.alibaba.nacos.common.utils.MD5; import com.alibaba.nacos.config.server.monitor.MetricsMonitor; import com.alibaba.nacos.config.server.service.ConfigService; import com.alibaba.nacos.config.server.utils.GroupKey2; @@ -67,7 +66,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 : MD5.getInstance().getMD5String(content); 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..2fc836e77 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,8 +15,7 @@ */ package com.alibaba.nacos.config.server.model; -import com.alibaba.nacos.common.utils.Md5Utils; -import com.alibaba.nacos.config.server.constant.Constants; +import com.alibaba.nacos.common.utils.MD5; import java.io.PrintWriter; import java.io.Serializable; @@ -47,7 +46,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 = MD5.getInstance().getMD5String(this.content); } } 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..72cf90f3d 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.MD5; 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 = MD5.getInstance().getMD5String(content); 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 = MD5.getInstance().getMD5String(content); 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 = MD5.getInstance().getMD5String(content); 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 = MD5.getInstance().getMD5String(content); 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..bdfabf3b0 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.MD5; 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 MD5.getInstance().getMD5String(getConfig(dataId, group, tenant)); } 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..bf3e15752 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,7 @@ */ 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.MD5; import com.alibaba.nacos.config.server.enums.FileTypeEnum; import com.alibaba.nacos.api.exception.NacosException; import com.alibaba.nacos.config.server.model.*; @@ -538,7 +537,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 = MD5.getInstance().getMD5String(configInfo.getContent()); 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 +563,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 = MD5.getInstance().getMD5String(configInfo.getContent()); 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 +627,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 = MD5.getInstance().getMD5String(configInfo.getContent()); 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 +653,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 = MD5.getInstance().getMD5String(configInfo.getContent()); 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 +2694,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 = MD5.getInstance().getMD5String(configInfo.getContent()); KeyHolder keyHolder = new GeneratedKeyHolder(); @@ -2904,7 +2903,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 = MD5.getInstance().getMD5String(configInfo.getContent()); 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 +3060,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 = MD5.getInstance().getMD5String(configInfo.getContent()); 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 +3354,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 = MD5.getInstance().getMD5String(content); 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..44904dcf8 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.MD5; import com.alibaba.nacos.config.server.constant.Constants; import com.alibaba.nacos.config.server.manager.TaskManager; import com.alibaba.nacos.config.server.model.ConfigInfo; @@ -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 = MD5.getInstance().getMD5String(aggrContent); 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..8295ecf64 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,8 +15,7 @@ */ package com.alibaba.nacos.config.server.service.dump; -import com.alibaba.nacos.common.utils.Md5Utils; -import com.alibaba.nacos.config.server.constant.Constants; +import com.alibaba.nacos.common.utils.MD5; import com.alibaba.nacos.config.server.manager.AbstractTask; import com.alibaba.nacos.config.server.manager.TaskProcessor; import com.alibaba.nacos.config.server.model.ConfigInfo; @@ -265,7 +264,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 = MD5.getInstance().getMD5String(content); LogUtil.dumpLog.info("[dump-all-ok] {}, {}, length={}, md5={}", GroupKey2.getKey(cf.getDataId(), cf.getGroup()), cf.getLastModified(), content.length(), md5); } @@ -410,7 +409,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 = MD5.getInstance().getMD5String(content); 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..c3e121559 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,8 +15,7 @@ */ package com.alibaba.nacos.config.server.service.trace; -import com.alibaba.nacos.common.utils.Md5Utils; -import com.alibaba.nacos.config.server.constant.Constants; +import com.alibaba.nacos.common.utils.MD5; import com.alibaba.nacos.config.server.monitor.MetricsMonitor; import com.alibaba.nacos.config.server.utils.LogUtil; import org.apache.commons.lang3.StringUtils; @@ -62,7 +61,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 : MD5.getInstance().getMD5String(content); 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..0a84b02e4 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,7 @@ 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.MD5; import com.alibaba.nacos.naming.pojo.Record; import org.apache.commons.lang3.StringUtils; @@ -66,7 +66,7 @@ public class Instances implements Record { sb.append(","); } - return Md5Utils.getMD5(sb.toString(), "UTF-8"); + return MD5.getInstance().getMD5String(sb.toString()); } 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..97ac2483b 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,7 @@ 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.MD5; import com.alibaba.nacos.naming.boot.SpringContext; import com.alibaba.nacos.naming.consistency.KeyBuilder; import com.alibaba.nacos.naming.consistency.RecordListener; @@ -472,7 +472,7 @@ public class Service extends com.alibaba.nacos.api.naming.pojo.Service implement ipsString.append(","); } - checksum = Md5Utils.getMD5(ipsString.toString(), "UTF-8"); + checksum = MD5.getInstance().getMD5String(ipsString.toString()); } private void updateOrAddCluster(Collection clusters) { From dcdbaf50eab21eefd603e2ac2507b4a3c2d21fdb Mon Sep 17 00:00:00 2001 From: wangwei Date: Mon, 20 Apr 2020 13:48:31 +0800 Subject: [PATCH 3/3] use MD5Utils replace MD5 --- .../nacos/client/config/impl/CacheData.java | 4 +- .../client/config/impl/ClientWorker.java | 6 +- .../client/config/impl/HttpSimpleClient.java | 16 +-- common/pom.xml | 5 - .../com/alibaba/nacos/common/utils/MD5.java | 132 ------------------ .../alibaba/nacos/common/utils/MD5Utils.java | 78 +++++++++++ .../alibaba/nacos/common/utils/MD5Test.java | 73 ---------- .../nacos/common/utils/MD5UtilsTest.java | 48 +++++++ .../server/aspect/RequestLogAspect.java | 5 +- .../config/server/model/ConfigInfoBase.java | 5 +- .../config/server/service/ConfigService.java | 10 +- .../nacos/config/server/service/DiskUtil.java | 4 +- .../config/server/service/PersistService.java | 19 +-- .../server/service/dump/DumpService.java | 6 +- .../config/server/service/dump/DumpTask.java | 7 +- .../service/trace/ConfigTraceService.java | 5 +- .../alibaba/nacos/naming/core/Instances.java | 5 +- .../alibaba/nacos/naming/core/Service.java | 5 +- 18 files changed, 178 insertions(+), 255 deletions(-) delete mode 100644 common/src/main/java/com/alibaba/nacos/common/utils/MD5.java create mode 100644 common/src/main/java/com/alibaba/nacos/common/utils/MD5Utils.java delete mode 100644 common/src/test/java/com/alibaba/nacos/common/utils/MD5Test.java create mode 100644 common/src/test/java/com/alibaba/nacos/common/utils/MD5UtilsTest.java 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 404a0bf6b..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.common.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 782bc10e4..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.common.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 6ac906ad2..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.common.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/common/pom.xml b/common/pom.xml index 374c00915..9b64da6b3 100644 --- a/common/pom.xml +++ b/common/pom.xml @@ -44,11 +44,6 @@ commons-io - - commons-codec - commons-codec - - org.apache.commons commons-lang3 diff --git a/common/src/main/java/com/alibaba/nacos/common/utils/MD5.java b/common/src/main/java/com/alibaba/nacos/common/utils/MD5.java deleted file mode 100644 index 9e62df402..000000000 --- a/common/src/main/java/com/alibaba/nacos/common/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.common.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/common/src/main/java/com/alibaba/nacos/common/utils/MD5Utils.java b/common/src/main/java/com/alibaba/nacos/common/utils/MD5Utils.java new file mode 100644 index 000000000..abe3e3c6a --- /dev/null +++ b/common/src/main/java/com/alibaba/nacos/common/utils/MD5Utils.java @@ -0,0 +1,78 @@ +/* + * 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.common.utils; + +import java.security.MessageDigest; +import java.security.NoSuchAlgorithmException; + +/** + * MD5 util + * + *@author nacos + */ +@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 + protected MessageDigest initialValue() { + try { + return MessageDigest.getInstance("MD5"); + } catch (NoSuchAlgorithmException e) { + return null; + } + } + }; + + public static String md5Hex(byte[] bytes) throws NoSuchAlgorithmException { + try { + MessageDigest messageDigest = MESSAGE_DIGEST_LOCAL.get(); + if (messageDigest != null) { + return encodeHexString(messageDigest.digest(bytes)); + } + throw new NoSuchAlgorithmException("MessageDigest get MD5 instance error"); + } finally { + MESSAGE_DIGEST_LOCAL.remove(); + } + } + + public static String md5Hex(String value,String encode) { + try { + 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/MD5Test.java b/common/src/test/java/com/alibaba/nacos/common/utils/MD5Test.java deleted file mode 100644 index 3bb6bc805..000000000 --- a/common/src/test/java/com/alibaba/nacos/common/utils/MD5Test.java +++ /dev/null @@ -1,73 +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.common.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("02f463eb799797e2a978fb1a2ae2991e", - MD5.getInstance().getMD5String("38c5ee9532f037a20b93d0f804cf111fca4003e451d09a692d9dea8032308d9c64eda9047fcd5e850284a49b1a0cfb2ecd45")); - - 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/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 c1673ae46..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,8 @@ */ package com.alibaba.nacos.config.server.aspect; -import com.alibaba.nacos.common.utils.MD5; +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; import com.alibaba.nacos.config.server.utils.GroupKey2; @@ -66,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 : MD5.getInstance().getMD5String(content); + 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 2fc836e77..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,8 @@ */ package com.alibaba.nacos.config.server.model; -import com.alibaba.nacos.common.utils.MD5; +import com.alibaba.nacos.common.utils.MD5Utils; +import com.alibaba.nacos.config.server.constant.Constants; import java.io.PrintWriter; import java.io.Serializable; @@ -46,7 +47,7 @@ public class ConfigInfoBase implements Serializable, Comparable this.group = group; this.content = content; if (this.content != null) { - this.md5 = MD5.getInstance().getMD5String(this.content); + 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 72cf90f3d..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.MD5; +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 = MD5.getInstance().getMD5String(content); + 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 = MD5.getInstance().getMD5String(content); + 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 = MD5.getInstance().getMD5String(content); + 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 = MD5.getInstance().getMD5String(content); + 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 bdfabf3b0..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.MD5; +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 MD5.getInstance().getMD5String(getConfig(dataId, group, tenant)); + 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 bf3e15752..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,7 +15,8 @@ */ package com.alibaba.nacos.config.server.service; -import com.alibaba.nacos.common.utils.MD5; +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.*; @@ -537,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 = MD5.getInstance().getMD5String(configInfo.getContent()); + 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(?,?,?,?,?,?,?,?,?,?,?)", @@ -563,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 = MD5.getInstance().getMD5String(configInfo.getContent()); + 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(?,?,?,?,?,?,?,?,?,?,?)", @@ -627,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 = MD5.getInstance().getMD5String(configInfo.getContent()); + 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=?", @@ -653,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 = MD5.getInstance().getMD5String(configInfo.getContent()); + 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=?", @@ -2694,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 = MD5.getInstance().getMD5String(configInfo.getContent()); + final String md5Tmp = MD5Utils.md5Hex(configInfo.getContent(), Constants.ENCODE); KeyHolder keyHolder = new GeneratedKeyHolder(); @@ -2903,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 = MD5.getInstance().getMD5String(configInfo.getContent()); + 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"); @@ -3060,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 = MD5.getInstance().getMD5String(configInfo.getContent()); + 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(?,?,?,?,?,?,?,?,?,?,?)", @@ -3354,7 +3355,7 @@ public class PersistService { String md5InDb = cf.getMd5(); final String content = cf.getContent(); final String tenant = cf.getTenant(); - final String md5 = MD5.getInstance().getMD5String(content); + 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 44904dcf8..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.MD5; +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 = MD5.getInstance().getMD5String(aggrContent); + 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 8295ecf64..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,8 @@ */ package com.alibaba.nacos.config.server.service.dump; -import com.alibaba.nacos.common.utils.MD5; +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; import com.alibaba.nacos.config.server.model.ConfigInfo; @@ -264,7 +265,7 @@ class DumpAllProcessor implements TaskProcessor { cf.getLastModified(), cf.getType()); final String content = cf.getContent(); - final String md5 = MD5.getInstance().getMD5String(content); + 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); } @@ -409,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 = MD5.getInstance().getMD5String(content); + 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 c3e121559..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,8 @@ */ package com.alibaba.nacos.config.server.service.trace; -import com.alibaba.nacos.common.utils.MD5; +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; import org.apache.commons.lang3.StringUtils; @@ -61,7 +62,7 @@ public class ConfigTraceService { } //localIp | dataid | group | tenant | requestIpAppName | ts | handleIp | event | type | [delayed = -1] | ext // (md5) - String md5 = content == null ? null : MD5.getInstance().getMD5String(content); + 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 0a84b02e4..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.MD5; +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 MD5.getInstance().getMD5String(sb.toString()); + 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 97ac2483b..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.MD5; +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 = MD5.getInstance().getMD5String(ipsString.toString()); + checksum = MD5Utils.md5Hex(ipsString.toString(), Constants.ENCODE); } private void updateOrAddCluster(Collection clusters) {