hmac.new 用法求助

lin52na 2014-04-19 04:42:40
python3.3.3


import hmac
from hashlib import sha1
hmac.new(secret, str_to_sign, sha1).digest())

两个参数 secret、str_to_sign我传入的是字符串。

错误提示如下
File "E:\python\uvvu.py", line 27, in create_nonce
hmac.new(secret, str_to_sign, sha1).digest())
File "D:\python\lib\hmac.py", line 131, in new
return HMAC(key, msg, digestmod)
File "D:\python\lib\hmac.py", line 38, in __init__
raise TypeError("key: expected bytes, but got %r" % type(key).__name__)
TypeError: key: expected bytes, but got 'str'

hmac.new的参数是要bytes吗?但是我试了把str_to_sign,secret转为bytes还是报这个错误,何解?
...全文
233 1 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
angel_su 2014-04-19
  • 打赏
  • 举报
回复
Python 3.4.0 (v3.4.0:04f714765c13, Mar 16 2014, 19:25:23) [MSC v.1600 64 bit (AMD64)] on win32 Type "copyright", "credits" or "license()" for more information. >>> import hmac >>> from hashlib import sha1 >>> hmac.new(b'abcd', b'hello', sha1).digest() b"Q&\x82?\xdb?N\xe3\xf9p\xf8'I)\xa5\x0b\xbd\\\x8d\x0c" >>>

37,743

社区成员

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

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