Fix bug #1417809: Remove default ascii encoding so md5hash can be used to hash other data.

bzr-revno: 2497
Fixes: https://launchpad.net/bugs/1417809
This commit is contained in:
Ken Roberts 2015-02-10 21:38:53 +02:00 committed by Raoul Snyman
commit 88cb02bb3d
4 changed files with 41 additions and 16 deletions

View File

@ -190,31 +190,32 @@ def verify_ip_address(addr):
return True if verify_ipv4(addr) else verify_ipv6(addr) return True if verify_ipv4(addr) else verify_ipv6(addr)
def md5_hash(salt, data): def md5_hash(salt, data=None):
""" """
Returns the hashed output of md5sum on salt,data Returns the hashed output of md5sum on salt,data
using Python3 hashlib using Python3 hashlib
:param salt: Initial salt :param salt: Initial salt
:param data: Data to hash :param data: OPTIONAL Data to hash
:returns: str :returns: str
""" """
log.debug('md5_hash(salt="%s")' % salt) log.debug('md5_hash(salt="%s")' % salt)
hash_obj = hashlib.new('md5') hash_obj = hashlib.new('md5')
hash_obj.update(salt.encode('ascii')) hash_obj.update(salt)
hash_obj.update(data.encode('ascii')) if data:
hash_obj.update(data)
hash_value = hash_obj.hexdigest() hash_value = hash_obj.hexdigest()
log.debug('md5_hash() returning "%s"' % hash_value) log.debug('md5_hash() returning "%s"' % hash_value)
return hash_value return hash_value
def qmd5_hash(salt, data): def qmd5_hash(salt, data=None):
""" """
Returns the hashed output of MD5Sum on salt, data Returns the hashed output of MD5Sum on salt, data
using PyQt4.QCryptographicHash. using PyQt4.QCryptographicHash.
:param salt: Initial salt :param salt: Initial salt
:param data: Data to hash :param data: OPTIONAL Data to hash
:returns: str :returns: str
""" """
log.debug('qmd5_hash(salt="%s"' % salt) log.debug('qmd5_hash(salt="%s"' % salt)
@ -223,7 +224,7 @@ def qmd5_hash(salt, data):
hash_obj.addData(data) hash_obj.addData(data)
hash_value = hash_obj.result().toHex() hash_value = hash_obj.result().toHex()
log.debug('qmd5_hash() returning "%s"' % hash_value) log.debug('qmd5_hash() returning "%s"' % hash_value)
return decode(hash_value.data(), 'ascii') return hash_value.data()
def clean_button_text(button_text): def clean_button_text(button_text):

View File

@ -343,7 +343,7 @@ class PJLink1(QTcpSocket):
# Authenticated login with salt # Authenticated login with salt
log.debug('(%s) Setting hash with salt="%s"' % (self.ip, data_check[2])) log.debug('(%s) Setting hash with salt="%s"' % (self.ip, data_check[2]))
log.debug('(%s) pin="%s"' % (self.ip, self.pin)) log.debug('(%s) pin="%s"' % (self.ip, self.pin))
salt = qmd5_hash(salt=data_check[2], data=self.pin) salt = qmd5_hash(salt=data_check[2].endcode('ascii'), data=self.pin.encode('ascii'))
else: else:
salt = None salt = None
# We're connected at this point, so go ahead and do regular I/O # We're connected at this point, so go ahead and do regular I/O

View File

@ -134,7 +134,7 @@ class PresentationDocument(object):
""" """
# TODO: If statment can be removed when the upgrade path from 2.0.x to 2.2.x is no longer needed # TODO: If statment can be removed when the upgrade path from 2.0.x to 2.2.x is no longer needed
if Settings().value('presentations/thumbnail_scheme') == 'md5': if Settings().value('presentations/thumbnail_scheme') == 'md5':
folder = md5_hash('', self.file_path) folder = md5_hash(self.file_path)
else: else:
folder = self.get_file_name() folder = self.get_file_name()
return os.path.join(self.controller.thumbnail_folder, folder) return os.path.join(self.controller.thumbnail_folder, folder)
@ -145,7 +145,7 @@ class PresentationDocument(object):
""" """
# TODO: If statment can be removed when the upgrade path from 2.0.x to 2.2.x is no longer needed # TODO: If statment can be removed when the upgrade path from 2.0.x to 2.2.x is no longer needed
if Settings().value('presentations/thumbnail_scheme') == 'md5': if Settings().value('presentations/thumbnail_scheme') == 'md5':
folder = md5_hash('', self.file_path) folder = md5_hash(self.file_path)
else: else:
folder = folder = self.get_file_name() folder = folder = self.get_file_name()
return os.path.join(self.controller.temp_folder, folder) return os.path.join(self.controller.temp_folder, folder)

View File

@ -23,6 +23,8 @@
Package to test the openlp.core.ui.projector.networkutils package. Package to test the openlp.core.ui.projector.networkutils package.
""" """
import os
from unittest import TestCase from unittest import TestCase
from openlp.core.common import verify_ip_address, md5_hash, qmd5_hash from openlp.core.common import verify_ip_address, md5_hash, qmd5_hash
@ -30,6 +32,8 @@ from openlp.core.common import verify_ip_address, md5_hash, qmd5_hash
salt = '498e4a67' salt = '498e4a67'
pin = 'JBMIAProjectorLink' pin = 'JBMIAProjectorLink'
test_hash = '5d8409bc1c3fa39749434aa3a5c38682' test_hash = '5d8409bc1c3fa39749434aa3a5c38682'
test_non_ascii_string = '이것은 한국어 시험 문자열'
test_non_ascii_hash = 'fc00c7912976f6e9c19099b514ced201'
ip4_loopback = '127.0.0.1' ip4_loopback = '127.0.0.1'
ip4_local = '192.168.1.1' ip4_local = '192.168.1.1'
@ -120,7 +124,7 @@ class testProjectorUtilities(TestCase):
Test MD5 hash from salt+data pass (python) Test MD5 hash from salt+data pass (python)
""" """
# WHEN: Given a known salt+data # WHEN: Given a known salt+data
hash_ = md5_hash(salt=salt, data=pin) hash_ = md5_hash(salt=salt.encode('ascii'), data=pin.encode('ascii'))
# THEN: Validate return has is same # THEN: Validate return has is same
self.assertEquals(hash_, test_hash, 'MD5 should have returned a good hash') self.assertEquals(hash_, test_hash, 'MD5 should have returned a good hash')
@ -130,7 +134,7 @@ class testProjectorUtilities(TestCase):
Test MD5 hash from salt+data fail (python) Test MD5 hash from salt+data fail (python)
""" """
# WHEN: Given a different salt+hash # WHEN: Given a different salt+hash
hash_ = md5_hash(salt=pin, data=salt) hash_ = md5_hash(salt=pin.encode('ascii'), data=salt.encode('ascii'))
# THEN: return data is different # THEN: return data is different
self.assertNotEquals(hash_, test_hash, 'MD5 should have returned a bad hash') self.assertNotEquals(hash_, test_hash, 'MD5 should have returned a bad hash')
@ -140,17 +144,37 @@ class testProjectorUtilities(TestCase):
Test MD5 hash from salt+data pass (Qt) Test MD5 hash from salt+data pass (Qt)
""" """
# WHEN: Given a known salt+data # WHEN: Given a known salt+data
hash_ = qmd5_hash(salt=salt, data=pin) hash_ = qmd5_hash(salt=salt.encode('ascii'), data=pin.encode('ascii'))
# THEN: Validate return has is same # THEN: Validate return has is same
self.assertEquals(hash_, test_hash, 'Qt-MD5 should have returned a good hash') self.assertEquals(hash_.decode('ascii'), test_hash, 'Qt-MD5 should have returned a good hash')
def test_qmd5_hash_bad(self): def test_qmd5_hash_bad(self):
""" """
Test MD5 hash from salt+hash fail (Qt) Test MD5 hash from salt+hash fail (Qt)
""" """
# WHEN: Given a different salt+hash # WHEN: Given a different salt+hash
hash_ = qmd5_hash(salt=pin, data=salt) hash_ = qmd5_hash(salt=pin.encode('ascii'), data=salt.encode('ascii'))
# THEN: return data is different # THEN: return data is different
self.assertNotEquals(hash_, test_hash, 'Qt-MD5 should have returned a bad hash') self.assertNotEquals(hash_.decode('ascii'), test_hash, 'Qt-MD5 should have returned a bad hash')
def test_md5_non_ascii_string(self):
"""
Test MD5 hash with non-ascii string - bug 1417809
"""
# WHEN: Non-ascii string is hashed
hash_ = md5_hash(salt=test_non_ascii_string.encode('utf-8'), data=None)
# THEN: Valid MD5 hash should be returned
self.assertEqual(hash_, test_non_ascii_hash, 'MD5 should have returned a valid hash')
def test_qmd5_non_ascii_string(self):
"""
Test MD5 hash with non-ascii string - bug 1417809
"""
# WHEN: Non-ascii string is hashed
hash_ = md5_hash(salt=test_non_ascii_string.encode('utf-8'), data=None)
# THEN: Valid MD5 hash should be returned
self.assertEqual(hash_, test_non_ascii_hash, 'Qt-MD5 should have returned a valid hash')