Convert md5sum calls to utf-8 for non-ascii pins

This commit is contained in:
Ken Roberts 2016-07-08 12:19:08 -07:00
parent 8fcf42cdc3
commit de3f4046b7
3 changed files with 27 additions and 16 deletions

View File

@ -195,7 +195,7 @@ def verify_ip_address(addr):
return True if verify_ipv4(addr) else verify_ipv6(addr)
def md5_hash(salt, data=None):
def md5_hash(salt=None, data=None):
"""
Returns the hashed output of md5sum on salt,data
using Python3 hashlib
@ -205,8 +205,11 @@ def md5_hash(salt, data=None):
:returns: str
"""
log.debug('md5_hash(salt="{text}")'.format(text=salt))
if not salt and not data:
return None
hash_obj = hashlib.new('md5')
hash_obj.update(salt)
if salt:
hash_obj.update(salt)
if data:
hash_obj.update(data)
hash_value = hash_obj.hexdigest()
@ -214,18 +217,25 @@ def md5_hash(salt, data=None):
return hash_value
def qmd5_hash(salt, data=None):
def qmd5_hash(salt=None, data=None):
"""
Returns the hashed output of MD5Sum on salt, data
using PyQt5.QCryptographicHash.
using PyQt5.QCryptographicHash. Function returns a
QByteArray instead of a text string.
If you need a string instead, call with
result = str(qmd5_hash(salt=..., data=...), encoding='ascii')
:param salt: Initial salt
:param data: OPTIONAL Data to hash
:returns: str
:returns: QByteArray
"""
log.debug('qmd5_hash(salt="{text}"'.format(text=salt))
if salt is None and data is None:
return None
hash_obj = QHash(QHash.Md5)
hash_obj.addData(salt)
if salt:
hash_obj.addData(salt)
if data:
hash_obj.addData(data)
hash_value = hash_obj.result().toHex()

View File

@ -49,7 +49,7 @@ from codecs import decode
from PyQt5.QtCore import pyqtSignal, pyqtSlot
from PyQt5.QtNetwork import QAbstractSocket, QTcpSocket
from openlp.core.common import translate, md5_hash
from openlp.core.common import translate, qmd5_hash
from openlp.core.lib.projector.constants import *
# Shortcuts
@ -364,14 +364,15 @@ class PJLink1(QTcpSocket):
else:
log.debug('({ip}) Setting hash with salt="{data}"'.format(ip=self.ip, data=data_check[2]))
log.debug('({ip}) pin="{data}"'.format(ip=self.ip, data=self.pin))
salt = md5_hash(salt=data_check[2].encode('ascii'), data=self.pin.encode('ascii'))
data_hash = str(qmd5_hash(salt=data_check[2].encode('utf-8'), data=self.pin.encode('utf-8')),
encoding='ascii')
else:
salt = None
# We're connected at this point, so go ahead and do regular I/O
data_hash = None
# We're connected at this point, so go ahead and setup regular I/O
self.readyRead.connect(self.get_data)
self.projectorReceivedData.connect(self._send_command)
# Initial data we should know about
self.send_command(cmd='CLSS', salt=salt)
self.send_command(cmd='CLSS', salt=data_hash)
self.waitForReadyRead()
if (not self.no_poll) and (self.state() == self.ConnectedState):
log.debug('({ip}) Starting timer'.format(ip=self.ip))

View File

@ -124,7 +124,7 @@ class testProjectorUtilities(TestCase):
Test MD5 hash from salt+data pass (python)
"""
# WHEN: Given a known salt+data
hash_ = md5_hash(salt=salt.encode('ascii'), data=pin.encode('ascii'))
hash_ = md5_hash(salt=salt.encode('utf-8'), data=pin.encode('utf-8'))
# THEN: Validate return has is same
self.assertEquals(hash_, test_hash, 'MD5 should have returned a good hash')
@ -134,7 +134,7 @@ class testProjectorUtilities(TestCase):
Test MD5 hash from salt+data fail (python)
"""
# WHEN: Given a different salt+hash
hash_ = md5_hash(salt=pin.encode('ascii'), data=salt.encode('ascii'))
hash_ = md5_hash(salt=pin.encode('utf-8'), data=salt.encode('utf-8'))
# THEN: return data is different
self.assertNotEquals(hash_, test_hash, 'MD5 should have returned a bad hash')
@ -144,7 +144,7 @@ class testProjectorUtilities(TestCase):
Test MD5 hash from salt+data pass (Qt)
"""
# WHEN: Given a known salt+data
hash_ = qmd5_hash(salt=salt.encode('ascii'), data=pin.encode('ascii'))
hash_ = qmd5_hash(salt=salt.encode('utf-8'), data=pin.encode('utf-8'))
# THEN: Validate return has is same
self.assertEquals(hash_, test_hash, 'Qt-MD5 should have returned a good hash')
@ -154,7 +154,7 @@ class testProjectorUtilities(TestCase):
Test MD5 hash from salt+hash fail (Qt)
"""
# WHEN: Given a different salt+hash
hash_ = qmd5_hash(salt=pin.encode('ascii'), data=salt.encode('ascii'))
hash_ = qmd5_hash(salt=pin.encode('utf-8'), data=salt.encode('utf-8'))
# THEN: return data is different
self.assertNotEquals(hash_, test_hash, 'Qt-MD5 should have returned a bad hash')
@ -174,7 +174,7 @@ class testProjectorUtilities(TestCase):
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)
hash_ = md5_hash(data=test_non_ascii_string.encode('utf-8'))
# THEN: Valid MD5 hash should be returned
self.assertEqual(hash_, test_non_ascii_hash, 'Qt-MD5 should have returned a valid hash')