Skip to content

Commit 123641c

Browse files
author
guido.van.rossum
committed
Change hashlib to return bytes from digest() instead of str8.
git-svn-id: http://svn.python.org/projects/python/branches/py3k-struni@56216 6015fed2-1504-0410-9fe1-9d1591cc4771
1 parent 856e83f commit 123641c

5 files changed

Lines changed: 32 additions & 29 deletions

File tree

Lib/test/test_hashlib.py

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,12 @@
1212

1313

1414
def hexstr(s):
15-
import string
16-
h = string.hexdigits
17-
r = ''
18-
for c in s:
19-
i = ord(c)
20-
r = r + h[(i >> 4) & 0xF] + h[i & 0xF]
15+
assert isinstance(s, bytes), repr(s)
16+
h = b"0123456789abcdef"
17+
r = b''
18+
for i in s:
19+
r.append(h[(i >> 4) & 0xF])
20+
r.append(h[i & 0xF])
2121
return r
2222

2323

@@ -37,7 +37,8 @@ def test_unknown_hash(self):
3737
def test_hexdigest(self):
3838
for name in self.supported_hash_names:
3939
h = hashlib.new(name)
40-
self.assert_(hexstr(h.digest()) == h.hexdigest())
40+
assert isinstance(h.digest(), bytes), name
41+
self.assertEqual(hexstr(h.digest()), h.hexdigest())
4142

4243

4344
def test_large_update(self):
@@ -66,35 +67,37 @@ def check(self, name, data, digest):
6667

6768

6869
def test_case_md5_0(self):
69-
self.check('md5', '', 'd41d8cd98f00b204e9800998ecf8427e')
70+
self.check('md5', b'', b'd41d8cd98f00b204e9800998ecf8427e')
7071

7172
def test_case_md5_1(self):
72-
self.check('md5', 'abc', '900150983cd24fb0d6963f7d28e17f72')
73+
self.check('md5', b'abc', b'900150983cd24fb0d6963f7d28e17f72')
7374

7475
def test_case_md5_2(self):
75-
self.check('md5', 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789',
76-
'd174ab98d277d9f5a5611c2c9f419d9f')
76+
self.check('md5',
77+
b'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789',
78+
b'd174ab98d277d9f5a5611c2c9f419d9f')
7779

7880

7981
# use the three examples from Federal Information Processing Standards
8082
# Publication 180-1, Secure Hash Standard, 1995 April 17
8183
# http://www.itl.nist.gov/div897/pubs/fip180-1.htm
8284

8385
def test_case_sha1_0(self):
84-
self.check('sha1', "",
85-
"da39a3ee5e6b4b0d3255bfef95601890afd80709")
86+
self.check('sha1', b"",
87+
b"da39a3ee5e6b4b0d3255bfef95601890afd80709")
8688

8789
def test_case_sha1_1(self):
88-
self.check('sha1', "abc",
89-
"a9993e364706816aba3e25717850c26c9cd0d89d")
90+
self.check('sha1', b"abc",
91+
b"a9993e364706816aba3e25717850c26c9cd0d89d")
9092

9193
def test_case_sha1_2(self):
92-
self.check('sha1', "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq",
93-
"84983e441c3bd26ebaae4aa1f95129e5e54670f1")
94+
self.check('sha1',
95+
b"abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq",
96+
b"84983e441c3bd26ebaae4aa1f95129e5e54670f1")
9497

9598
def test_case_sha1_3(self):
96-
self.check('sha1', "a" * 1000000,
97-
"34aa973cd4c4daa4f61eeb2bdbad27316534016f")
99+
self.check('sha1', b"a" * 1000000,
100+
b"34aa973cd4c4daa4f61eeb2bdbad27316534016f")
98101

99102

100103
# use the examples from Federal Information Processing Standards

Lib/uuid.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -528,7 +528,7 @@ def uuid3(namespace, name):
528528
"""Generate a UUID from the MD5 hash of a namespace UUID and a name."""
529529
from hashlib import md5
530530
hash = md5(namespace.bytes + bytes(name, "utf-8")).digest()
531-
return UUID(bytes=bytes_(hash[:16]), version=3)
531+
return UUID(bytes=hash[:16], version=3)
532532

533533
def uuid4():
534534
"""Generate a random UUID."""
@@ -551,7 +551,7 @@ def uuid5(namespace, name):
551551
"""Generate a UUID from the SHA-1 hash of a namespace UUID and a name."""
552552
from hashlib import sha1
553553
hash = sha1(namespace.bytes + bytes(name, "utf-8")).digest()
554-
return UUID(bytes=bytes_(hash[:16]), version=5)
554+
return UUID(bytes=hash[:16], version=5)
555555

556556
# The following standard UUIDs are for use with uuid3() or uuid5().
557557

Modules/_hashopenssl.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ EVP_digest(EVPobject *self, PyObject *unused)
103103
digest_size = EVP_MD_CTX_size(&temp_ctx);
104104
EVP_DigestFinal(&temp_ctx, digest, NULL);
105105

106-
retval = PyString_FromStringAndSize((const char *)digest, digest_size);
106+
retval = PyBytes_FromStringAndSize((const char *)digest, digest_size);
107107
EVP_MD_CTX_cleanup(&temp_ctx);
108108
return retval;
109109
}
@@ -133,7 +133,7 @@ EVP_hexdigest(EVPobject *self, PyObject *unused)
133133
retval = PyString_FromStringAndSize(NULL, digest_size * 2);
134134
if (!retval)
135135
return NULL;
136-
hex_digest = PyString_AsString(retval);
136+
hex_digest = PyString_AS_STRING(retval);
137137
if (!hex_digest) {
138138
Py_DECREF(retval);
139139
return NULL;

Modules/sha256module.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -432,7 +432,7 @@ SHA256_digest(SHAobject *self, PyObject *unused)
432432

433433
SHAcopy(self, &temp);
434434
sha_final(digest, &temp);
435-
return PyString_FromStringAndSize((const char *)digest, self->digestsize);
435+
return PyBytes_FromStringAndSize((const char *)digest, self->digestsize);
436436
}
437437

438438
PyDoc_STRVAR(SHA256_hexdigest__doc__,
@@ -510,9 +510,9 @@ static PyObject *
510510
SHA256_get_name(PyObject *self, void *closure)
511511
{
512512
if (((SHAobject *)self)->digestsize == 32)
513-
return PyString_FromStringAndSize("SHA256", 6);
513+
return PyUnicode_FromStringAndSize("SHA256", 6);
514514
else
515-
return PyString_FromStringAndSize("SHA224", 6);
515+
return PyUnicode_FromStringAndSize("SHA224", 6);
516516
}
517517

518518
static PyGetSetDef SHA_getseters[] = {

Modules/sha512module.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -498,7 +498,7 @@ SHA512_digest(SHAobject *self, PyObject *unused)
498498

499499
SHAcopy(self, &temp);
500500
sha512_final(digest, &temp);
501-
return PyString_FromStringAndSize((const char *)digest, self->digestsize);
501+
return PyBytes_FromStringAndSize((const char *)digest, self->digestsize);
502502
}
503503

504504
PyDoc_STRVAR(SHA512_hexdigest__doc__,
@@ -576,9 +576,9 @@ static PyObject *
576576
SHA512_get_name(PyObject *self, void *closure)
577577
{
578578
if (((SHAobject *)self)->digestsize == 64)
579-
return PyString_FromStringAndSize("SHA512", 6);
579+
return PyUnicode_FromStringAndSize("SHA512", 6);
580580
else
581-
return PyString_FromStringAndSize("SHA384", 6);
581+
return PyUnicode_FromStringAndSize("SHA384", 6);
582582
}
583583

584584
static PyGetSetDef SHA_getseters[] = {

0 commit comments

Comments
 (0)