用python 做云短信的时候遇到 checksum问题。

lonelyriver 2017-05-03 01:15:45
CheckSum字段要求:SHA1(AppSecret + Nonce + CurTime),三个参数拼接的字符串,进行SHA1哈希计算,转化成16进制字符(String,小写)


我的代码是:

CheckSum = AppSecret + str(Nonce) + str(CurTime)

for i in range(1000):
sha = hashlib.sha1(CheckSum)
CheckSum = sha.hexdigest()
print CheckSum

反馈的代码是414 checksum参数错误
知道有错,但不知道错在哪里,以及怎样实现。还请各位前辈帮忙修正下。
...全文
262 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
屎克螂 2017-05-05
  • 打赏
  • 举报
回复
CheckSum = AppSecret + str(Nonce) + str(CurTime) 试下用这种方式 "%s%s%s" %(AppSecret,Nonce,CurTime) for i in range(1000): 我不知道你不是嫌hash太快了
老兵与新兵 2017-05-04
  • 打赏
  • 举报
回复
是不是要对checksum进行编码,我用的python3.5.2
lonelyriver 2017-05-03
  • 打赏
  • 举报
回复
import java.security.MessageDigest;

public class CheckSumBuilder {
    // 计算并获取CheckSum
    public static String getCheckSum(String appSecret, String nonce, String curTime) {
        return encode("sha1", appSecret + nonce + curTime);
    }

    // 计算并获取md5值
    public static String getMD5(String requestBody) {
        return encode(“md5”, requestBody);
    }

    private static String encode(String algorithm, String value) {
        if (value == null) {
            return null;
        }
        try {
            MessageDigest messageDigest
                    = MessageDigest.getInstance(algorithm);
            messageDigest.update(value.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();
    }
    private static final char[] HEX_DIGITS = { '0', '1', '2', '3', '4', '5',
            '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
}
这个有平台提供的java实现

37,722

社区成员

发帖
与我相关
我的任务
社区描述
JavaScript,VBScript,AngleScript,ActionScript,Shell,Perl,Ruby,Lua,Tcl,Scala,MaxScript 等脚本语言交流。
社区管理员
  • 脚本语言(Perl/Python)社区
  • IT.BOB
加入社区
  • 近7日
  • 近30日
  • 至今

试试用AI创作助手写篇文章吧