0.安装itsdangerous内部默认使用了HMAC和SHA1来签名,基于 Django 签名模块。它也支持JSON Web 签名 (JWS)。这个库采用BSD协议,由Armin Ronacher编写,而大部分设计与实现的版权归Simon Willison和其他的把这个库变为现实的Django爱好者们。
1 | pip install itsdangerous |
1 2 3 4 5 6 | In [1]: from itsdangerous import Signer In [2]: s = Signer('secret_key') In [3]: s.sign(b'hello') Out[3]: b'hello.v8Y625GovH2FcEWU_j5w1klqs0I' |
1 2 | In [4]: s.unsign('hello.v8Y625GovH2FcEWU_j5w1klqs0I') # 这里也可以传bytes数据 Out[4]: b'hello' |
1 2 3 4 5 6 7 8 | In [6]: from itsdangerous import TimestampSigner In [7]: s = TimestampSigner('secret_key') In [8]: string = s.sign('hello') In [9]: string Out[9]: b'hello.DmZg2g.idiRG3HJdMzUNN-di8LXHKiEW5s' |
1 2 3 4 5 6 7 | In [10]: s.unsign(string) # 没有输入有效期,是可以直接反签名 Out[10]: b'hello' In [11]: s.unsign(string,max_age=5) # 输入有效期 5s SignatureExpired Traceback (most recent call last) ..... SignatureExpired: Signature age 48 > 5 seconds |
1 2 3 4 5 6 7 8 9 | In [13]: from itsdangerous import Serializer In [14]: s = Serializer('secret_key') In [15]: s.dumps({'name':'zhangsan','age':18}) Out[15]: '{"name": "zhangsan", "age": 18}.SQd5BPTqTZQ4vLOEN7PKBYWMsas' In [16]: s.loads('{"name": "zhangsan", "age": 18}.SQd5BPTqTZQ4vLOEN7PKBYWMsas') Out[16]: {'name': 'zhangsan', 'age': 18} |
1 2 3 4 5 6 7 8 9 | In [23]: from itsdangerous import URLSafeSerializer In [25]: s = URLSafeSerializer('secret_key') In [26]: s.dumps([1,2,3]) Out[26]: 'WzEsMiwzXQ.1JvL0-RdbHYdszhKCATOTdQEZl0' In [27]: s.loads('WzEsMiwzXQ.1JvL0-RdbHYdszhKCATOTdQEZl0') Out[27]: [1, 2, 3] |
1 2 3 4 5 6 7 8 9 10 | In [29]: from itsdangerous import JSONWebSignatureSerializer In [30]: s = JSONWebSignatureSerializer('secret_key') In [31]: s.dumps({'name':'mike'}) Out[31]: b'eyJhbGciOiJIUzI1NiJ9.eyJuYW1lIjoibWlrZSJ9.A9eujaivW63mvCSmz_6KVT6sDtLneBe3U62wCoSellY' In [32]: s.loads('eyJhbGciOiJIUzI1NiJ9.eyJuYW1lIjoibWlrZSJ9.A9eujaiv ...: W63mvCSmz_6KVT6sDtLneBe3U62wCoSellY') Out[32]: {'name': 'mike'} |
1 2 3 4 5 6 | In [34]: s.dumps({'name':'mike'},header_fields={'myheader':123}) Out[34]: b'eyJteWhlYWRlciI6MTIzLCJhbGciOiJIUzI1NiJ9.eyJuYW1lIjoibWlrZSJ9.mE-UAcsVsqisJj6EIOF1ocahGdRz5cbZyyySF39OREQ' In [35]: s.loads('eyJteWhlYWRlciI6MTIzLCJhbGciOiJIUzI1NiJ9.eyJuYW1lI ...: joibWlrZSJ9.mE-UAcsVsqisJj6EIOF1ocahGdRz5cbZyyySF39OREQ',return_header=True) Out[35]: ({'name': 'mike'}, {'myheader': 123, 'alg': 'HS256'}) |
1 2 3 4 5 6 7 8 9 10 11 12 13 | In [42]: from itsdangerous import TimedJSONWebSignatureSerializer In [43]: s = TimedJSONWebSignatureSerializer('secret_key',10) # 设置有效期为 10s In [44]: s.dumps({'id':1}) Out[44]: b'eyJhbGciOiJIUzI1NiIsImlhdCI6MTUzNTQzNTg3NSwiZXhwIjoxNTM1NDM1ODg1fQ.eyJpZCI6MX0.vZ5fDRR1-zBJk2nOH7kpdQIxvEv8dru0C3o_wuagIec' In [45]: s.loads('eyJhbGciOiJIUzI1NiIsImlhdCI6MTUzNTQzNTg3NSwiZXhwIj ...: oxNTM1NDM1ODg1fQ.eyJpZCI6MX0.vZ5fDRR1-zBJk2nOH7kpdQIxvEv8dr ...: u0C3o_wuagIec') Traceback (most recent call last) ..... SignatureExpired: Signature expired |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | In [36]: s = URLSafeSerializer('scret_key',salt='verify_salt') # 激活使用的盐 In [37]: s.dumps(10) # 假设用户的 id=10 Out[37]: 'MTA.jz5Oj-RsTztx1o0KQhhjdEL2z9E' In [38]: s_vip = URLSafeSerializer('secret_key',salt='vip_salt') # 升级vip使用的盐 In [39]: s_vip.dumps(10) Out[39]: 'MTA.j_qvVU-kNlTimdlgMty0qcy_618' # 反签名时,使用 salt=vip_salt 的对象去加载 salt='verify_salt' 对象,就会报签名错误 In [40]: s_vip.loads(s.dumps(10)) Traceback (most recent call last) ..... BadSignature: Signature b'jz5Oj-RsTztx1o0KQhhjdEL2z9E' does not match # 使用相同盐的序列化器才能成功反签名 In [41]: s_vip.loads(s_vip.dumps(10)) Out[41]: 10 |
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) | 黑马程序员IT技术论坛 X3.2 |