找回密码
 立即注册
搜索
查看: 7152|回复: 0

[技术探讨] 求大神帮我转写一下这个两个函数的代码java转成python

[复制链接]

6

主题

105

回帖

1988

积分

论坛技术达人

积分
1988
金钱
1867
HASS币
40
发表于 2020-7-22 09:33:26 | 显示全部楼层 |阅读模式
public String getSign(String appId, String appKey, String timestamp, String body,String url){
    URL urlObj = new URL(url);
    url=urlObj.getPath();
    appKey = appKey.trim();
    appKey = appKey.replaceAll("\"", "");
    if (body != null) {
        body = body.trim();
    }
    if (!body.equals("")) {
        body = body.replaceAll(" ", "");
        body = body.replaceAll("\t", "");
        body = body.replaceAll("\r", "");
        body = body.replaceAll("\n", "");
    }
    //log.info("body:"+body);
    StringBuffer sb = new StringBuffer();
    sb.append(url).append(body).append(appId).append(appKey).append(timestamp);
    MessageDigest md = null;
    byte[] bytes = null;
    try {
        md = MessageDigest.getInstance("SHA-256");
        bytes = md.digest(sb.toString().getBytes("utf-8"));
    } catch (Exception e) {
        e.printStackTrace();
    }
    return BinaryToHexString(bytes);
}

public String BinaryToHexString(byte[] bytes) {
    StringBuilder hex = new StringBuilder();
    String hexStr = "0123456789abcdef";
    for (int i = 0; i < bytes.length; i++) {
        hex.append(String.valueOf(hexStr.charAt((bytes & 0xF0) >> 4)));
        hex.append(String.valueOf(hexStr.charAt(bytes & 0x0F)));
    }
    return hex.toString();
}
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Archiver|手机版|小黑屋|Hassbian ( 晋ICP备17001384号-1 )

GMT+8, 2025-6-27 20:11 , Processed in 0.087270 second(s), 23 queries .

Powered by Discuz! X3.5

© 2001-2025 Discuz! Team.

快速回复 返回顶部 返回列表