forked from openlp/openlp
Convert md5sum calls to utf-8 for non-ascii pins
This commit is contained in:
parent
8fcf42cdc3
commit
de3f4046b7
@ -195,7 +195,7 @@ 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=None):
|
def md5_hash(salt=None, 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
|
||||||
@ -205,7 +205,10 @@ def md5_hash(salt, data=None):
|
|||||||
:returns: str
|
:returns: str
|
||||||
"""
|
"""
|
||||||
log.debug('md5_hash(salt="{text}")'.format(text=salt))
|
log.debug('md5_hash(salt="{text}")'.format(text=salt))
|
||||||
|
if not salt and not data:
|
||||||
|
return None
|
||||||
hash_obj = hashlib.new('md5')
|
hash_obj = hashlib.new('md5')
|
||||||
|
if salt:
|
||||||
hash_obj.update(salt)
|
hash_obj.update(salt)
|
||||||
if data:
|
if data:
|
||||||
hash_obj.update(data)
|
hash_obj.update(data)
|
||||||
@ -214,17 +217,24 @@ def md5_hash(salt, data=None):
|
|||||||
return hash_value
|
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
|
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 salt: Initial salt
|
||||||
:param data: OPTIONAL Data to hash
|
:param data: OPTIONAL Data to hash
|
||||||
:returns: str
|
:returns: QByteArray
|
||||||
"""
|
"""
|
||||||
log.debug('qmd5_hash(salt="{text}"'.format(text=salt))
|
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 = QHash(QHash.Md5)
|
||||||
|
if salt:
|
||||||
hash_obj.addData(salt)
|
hash_obj.addData(salt)
|
||||||
if data:
|
if data:
|
||||||
hash_obj.addData(data)
|
hash_obj.addData(data)
|
||||||
|
@ -49,7 +49,7 @@ from codecs import decode
|
|||||||
from PyQt5.QtCore import pyqtSignal, pyqtSlot
|
from PyQt5.QtCore import pyqtSignal, pyqtSlot
|
||||||
from PyQt5.QtNetwork import QAbstractSocket, QTcpSocket
|
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 *
|
from openlp.core.lib.projector.constants import *
|
||||||
|
|
||||||
# Shortcuts
|
# Shortcuts
|
||||||
@ -364,14 +364,15 @@ class PJLink1(QTcpSocket):
|
|||||||
else:
|
else:
|
||||||
log.debug('({ip}) Setting hash with salt="{data}"'.format(ip=self.ip, data=data_check[2]))
|
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))
|
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:
|
else:
|
||||||
salt = None
|
data_hash = 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 setup regular I/O
|
||||||
self.readyRead.connect(self.get_data)
|
self.readyRead.connect(self.get_data)
|
||||||
self.projectorReceivedData.connect(self._send_command)
|
self.projectorReceivedData.connect(self._send_command)
|
||||||
# Initial data we should know about
|
# Initial data we should know about
|
||||||
self.send_command(cmd='CLSS', salt=salt)
|
self.send_command(cmd='CLSS', salt=data_hash)
|
||||||
self.waitForReadyRead()
|
self.waitForReadyRead()
|
||||||
if (not self.no_poll) and (self.state() == self.ConnectedState):
|
if (not self.no_poll) and (self.state() == self.ConnectedState):
|
||||||
log.debug('({ip}) Starting timer'.format(ip=self.ip))
|
log.debug('({ip}) Starting timer'.format(ip=self.ip))
|
||||||
|
@ -124,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.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
|
# 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')
|
||||||
@ -134,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.encode('ascii'), data=salt.encode('ascii'))
|
hash_ = md5_hash(salt=pin.encode('utf-8'), data=salt.encode('utf-8'))
|
||||||
|
|
||||||
# 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')
|
||||||
@ -144,7 +144,7 @@ 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.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
|
# THEN: Validate return has is same
|
||||||
self.assertEquals(hash_, test_hash, 'Qt-MD5 should have returned a good hash')
|
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)
|
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.encode('ascii'), data=salt.encode('ascii'))
|
hash_ = qmd5_hash(salt=pin.encode('utf-8'), data=salt.encode('utf-8'))
|
||||||
|
|
||||||
# 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_, 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
|
Test MD5 hash with non-ascii string - bug 1417809
|
||||||
"""
|
"""
|
||||||
# WHEN: Non-ascii string is hashed
|
# 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
|
# THEN: Valid MD5 hash should be returned
|
||||||
self.assertEqual(hash_, test_non_ascii_hash, 'Qt-MD5 should have returned a valid hash')
|
self.assertEqual(hash_, test_non_ascii_hash, 'Qt-MD5 should have returned a valid hash')
|
||||||
|
Loading…
Reference in New Issue
Block a user