SHA_hexdigest(): A couple of small patches to this function, added

after a brief conversation with TP.  First, the return values of the
PyString_* function calls should be checked for errors.  Second,
bit-manipulations should be used instead of division for spliting the
byte up into its 4 bit digits.
This commit is contained in:
Barry Warsaw 2000-08-15 06:03:35 +00:00
parent 3fdcccb82f
commit 57b808d21a
1 changed files with 10 additions and 2 deletions

View File

@ -422,14 +422,22 @@ SHA_hexdigest(SHAobject *self, PyObject *args)
/* Create a new string */
retval = PyString_FromStringAndSize(NULL, sizeof(digest) * 2);
if (!retval)
return NULL;
hex_digest = PyString_AsString(retval);
if (!hex_digest) {
Py_DECREF(retval);
return NULL;
}
/* Make hex version of the digest */
for(i=j=0; i<sizeof(digest); i++) {
char c;
c = digest[i] / 16; c = (c>9) ? c+'a'-10 : c + '0';
c = (digest[i] >> 4) & 0xf;
c = (c>9) ? c+'a'-10 : c + '0';
hex_digest[j++] = c;
c = digest[i] % 16; c = (c>9) ? c+'a'-10 : c + '0';
c = (digest[i] & 0xf);
c = (c>9) ? c+'a'-10 : c + '0';
hex_digest[j++] = c;
}
return retval;