Smail_
2016-11-23 09:45:47
python hmacsha256加密算法
前记:
python:2.7.12
这里有篇文章记录了其他语言的写法:
http://blog.csdn.net/js_sky/article/details/49024959
然而,这个作者不知道是不是抄的。
python的hmacsha256加密算法用他的写法是错误的!
正确写法:
signature = hmac.new("key", message, digestmod=hashlib.sha256).digest();
另外送上一个经常和hmacsha256结合一起使用的东西(python字符串转16进制串):
python: def toHex(str): lst = [] for ch in str: hv = hex(ord(ch)).replace('0x', '') if len(hv) == 1: hv = '0' + hv lst.append(hv) return reduce(lambda x, y: x + y, lst);
java: public static String String2Hex(final byte[] array) { final StringBuilder sb = new StringBuilder(); for (int i = 0; i < array.length; ++i) { final String hexString = Integer.toHexString(array[i] & 0xFF); if (hexString.length() == 1) { sb.append('0'); } sb.append(hexString.toUpperCase()); } return sb.toString(); }
signature = toHex(signature);
评论

xinxiangxuer LV1
2021年9月10日
ahojcn
2020年6月22日
暂无贡献等级
dc1234567 LV1
2019年7月15日
Zdm1272595 LV3
2019年5月27日
w461192673 LV1
2019年3月8日
314892681
2019年1月4日
暂无贡献等级
L08042Y LV2
2018年6月19日
豆宝宝囡
2018年1月19日
暂无贡献等级
dfaghrs LV19
2017年12月26日
KennedyL
2017年11月29日
暂无贡献等级