1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
|
<tt>
<html><head></head><body>Awesome! Thank you!<br><br>
-- <br><br>
Sent from my Android phone with K-9 Mail. Please excuse my brevity.<br><br><div class="gmail_quote">Thorsten Behrens &lt;sbehrens@gmx.li&gt; wrote:<blockquote class="gmail_quote" style="margin: 0pt 0pt 0pt 0.8ex; border-left: 1px solid rgb(204, 204, 204); padding-left: 1ex;"><br>
<pre style="white-space: pre-wrap; word-wrap:break-word; ">I am going back into the code to take a peek at your suggestion.<br>
<br>
<br>
On 1/29/2011 8:47 PM, Dwayne C. Litzenberger wrote:<br>
&gt; Have a look in the various <a href="http://common.py">common.py</a> files. All of the hex test vectors are<br>
&gt; being fed through either a2b_hex or b2a_hex. I think it should be possible<br>
&gt; to make versions of b2a_hex and a2b_hex that also do bytes-&gt;str and<br>
&gt; str-&gt;bytes conversions, respectively.<br>
&gt;<br>
&gt; The following code works in both Python 2.1 and Python 3.2b2:<br>
&gt;<br>
&gt; from binascii import b2a_hex as _b2a_hex, a2b_hex as _a2b_hex<br>
&gt; from codecs import ascii_decode as _ascii_decode<br>
&gt; def bin2hex(bts):<br>
&gt; """Like b2a_hex, but returns a str instead of bytes in Python 3.x"""<br>
&gt; return _ascii_decode(_b2a_hex(bts))[0]<br>
&gt; def hex2bin(s):<br>
&gt; """Like a2b_hex, but expects a str instead of bytes in Python 3.x"""<br>
&gt; return _a2b_hex(s.encode('ascii'))<br>
<br>
This would actually make things worse. That it works at all is to be considered a bug - there's a TODO I have not followed up on yet, and that TODO is to add type-checking to all functions so that an error is returned if a parameter is not "an object interpretable as a buffer of bytes". That is, if encode() is called with a unicode (str) object, that should raise an error.<br>
<br>
The reason I believe that pycrypto should check type is that the Python <br>
3.x stdlib behaves that way:<br>
<br>
&gt;&gt;&gt; from hashlib import sha1<br>
&gt;&gt;&gt; h = sha1()<br>
&gt;&gt;&gt; h.update("lorem")<br>
Traceback (most recent call last):<br>
File "&lt;stdin&gt;", line 1, in &lt;module&gt;<br>
TypeError: Unicode-objects must be encoded before hashing<br>
&gt;&gt;&gt; h.update(b"lorem")<br>
&gt;&gt;&gt; print (h.hexdigest())<br>
b58e92fff5246645f772bfe7a60272f356c0151a<br>
<br>
For consistency, I have both Crypto.Hash and Crypto.Cipher behaving this way. The changes are in the doc, but in a nutshell:<br>
<br>
Crypto.Hash<br>
<br>
Python 3.x: digest() returns a bytes object<br>
Python 3.x: hexdigest() returns a bytes object<br>
Python 3.x: The passed argument to update() must be an object<br>
interpretable as a buffer of bytes<br>
<br>
Crypto.Cipher<br>
new()<br>
Python 3.x: ```mode`` is a string object; ```key``` and ```IV``` must be objects interpretable as a buffer of bytes.<br>
cipher object<br>
Python 3.x: ```IV``` is a bytes object.<br>
decrypt()<br>
Python 3.x: ```string``` must be an object interpretable as a buffer of bytes.<br>
decrypt() will return a bytes object.<br>
encrypt()<br>
Python 3.x: ```string``` must be an object interpretable as a buffer of bytes.<br>
encrypt() will return a bytes object.<br>
<br>
<br>
If these new conventions will cause an issue, let's discuss that now, before I add the type-checking.<br>
<br>
<br>
All that having been said, I still think it should be possible to have the vectors be Unicode literals and to convert them to a bytes object when reading them in. It just will need to be done in a different part of the code.<br>
<br>
Thorsten<br>
<br>
</pre></blockquote></div></body></html>
</tt>
|