接主题,java 代码如下:
public String getCheckSum(String requestBody, String appSecret, String time) {
String md5Digest = MessageDigestUtils.md5Digest(requestBody); String checksum = MessageDigestUtils.sha1Digest(appSecret, md5Digest, time); return checksum;
}
public class MessageDigestUtils {
private static final char[] HEX_DIGITS = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'}; private MessageDigestUtils() { } public static String md5Digest(String input) { StringBuilder hexValue = new StringBuilder(); try { MessageDigest md5 = MessageDigest.getInstance("MD5"); byte[] md5Bytes = md5.digest(input.getBytes(StandardCharsets.UTF_8)); for (byte md5Byte : md5Bytes) { int val = ((int) md5Byte) & 0xff; if (val < 16) { hexValue.append("0"); } hexValue.append(Integer.toHexString(val)); } } catch (Exception e) { } return hexValue.toString(); } public static String sha1Digest(String appSecret, String nonce, String time) { String cOntent= appSecret + nonce + time; try { MessageDigest messageDigest = MessageDigest.getInstance("sha1"); messageDigest.update(content.getBytes()); return getFormattedText(messageDigest.digest()); } catch (Exception e) { throw new RuntimeException(e); } } private static String getFormattedText(byte[] bytes) { int len = bytes.length; StringBuilder buf = new StringBuilder(len * 2); for (int j = 0; j < len; j++) { buf.append(HEX_DIGITS[(bytes[j] >> 4) & 0x0f]); buf.append(HEX_DIGITS[bytes[j] & 0x0f]); } return buf.toString(); }
}
1 wiix 2022-11-27 17:51:12 +08:00 md5 和 sha1 总该认得吧? getFormattedText 将原始 md5/sha1 转成 16 进制文本。 python 有自己的实现,用不着这些代码。 |
![]() | 2 yankebupt 2022-11-27 19:38:15 +08:00 java 的库转完是个 byte[],得自己转字符串…… 你 python 随便找个直出字符串的 md5 sha1 库,就懒得转了...... |
![]() | 3 YepTen 2022-11-28 08:40:00 +08:00 你应该学点密码学的知识 |
4 blueorange 2022-11-28 09:14:10 +08:00 hashlib.md5("123".encode()).hexdigest() |
5 blueorange 2022-11-28 09:14:40 +08:00 import ashlib hashlib.sha1("123".encode()).hexdigest() hashlib.md5("123".encode()).hexdigest() |
![]() | 6 bjzhush 2022-11-28 11:20:15 +08:00 ![]() 这不就 md5 的 sha ,你用同样的输入交给这俩函数运行,看看 Python 的 md5 和 sha 的输出,对得上就验证成功了 |
![]() | 7 feitxue 2022-11-28 22:27:02 +08:00 想起了当时客户对接的银行给的示例,是命令行方式加密,当时是翻译成 java 代码来搞。 嗯。怎么说呢,java 的 hutoo 真好用。 |