diff --git a/agents-flex-core/src/main/java/com/agentsflex/core/util/HashUtil.java b/agents-flex-core/src/main/java/com/agentsflex/core/util/HashUtil.java new file mode 100644 index 0000000..5aa1692 --- /dev/null +++ b/agents-flex-core/src/main/java/com/agentsflex/core/util/HashUtil.java @@ -0,0 +1,70 @@ +/* + * Copyright (c) 2023-2025, Agents-Flex (fuhai999@gmail.com). + *
+ * 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.agentsflex.core.util; + +import javax.crypto.Mac; +import javax.crypto.spec.SecretKeySpec; +import java.nio.charset.StandardCharsets; +import java.security.MessageDigest; +import java.util.Base64; + +public class HashUtil { + private static final char[] HEX_DIGITS = "0123456789abcdef".toCharArray(); + private static final char[] CHAR_ARRAY = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ".toCharArray(); + + public static String md5(String srcStr) { + return hash("MD5", srcStr); + } + + + public static String sha256(String srcStr) { + return hash("SHA-256", srcStr); + } + + public static String hmacSHA256ToBase64(String content, String secret) { + try { + Mac hmacSHA256 = Mac.getInstance("HmacSHA256"); + SecretKeySpec secretKey = new SecretKeySpec(secret.getBytes(StandardCharsets.UTF_8), "HmacSHA256"); + hmacSHA256.init(secretKey); + byte[] bytes = hmacSHA256.doFinal(content.getBytes(StandardCharsets.UTF_8)); + return Base64.getEncoder().encodeToString(bytes); + } catch (Exception e) { + throw new RuntimeException(e); + } + } + + public static String hash(String algorithm, String srcStr) { + try { + MessageDigest md = MessageDigest.getInstance(algorithm); + byte[] bytes = md.digest(srcStr.getBytes(StandardCharsets.UTF_8)); + return bytesToHex(bytes); + } catch (Exception e) { + throw new RuntimeException(e); + } + } + + public static String bytesToHex(byte[] bytes) { + StringBuilder ret = new StringBuilder(bytes.length * 2); + for (byte aByte : bytes) { + ret.append(HEX_DIGITS[(aByte >> 4) & 0x0f]); + ret.append(HEX_DIGITS[aByte & 0x0f]); + } + return ret.toString(); + } + + + +}