← Back to team overview

openlp-core team mailing list archive

[Merge] lp:~alisonken1/openlp/projector-cleanups into lp:openlp

 

Ken Roberts has proposed merging lp:~alisonken1/openlp/projector-cleanups into lp:openlp.

Commit message:
Fix md5sum and qmd5sum to use UTF-8

Requested reviews:
  OpenLP Core (openlp-core)

For more details, see:
https://code.launchpad.net/~alisonken1/openlp/projector-cleanups/+merge/299612

- Convert calls to md5_hash and qmd5_hash to use UTF-8 for non-ascii characters
- Fixed test to verify UTF-8 returns valid ASCII hash
- Switch back to qmd5_hash to stay within the Qt framework

--------------------------------
lp:~alisonken1/openlp/projector-cleanups (revision 2679)
[SUCCESS] https://ci.openlp.io/job/Branch-01-Pull/1646/
[SUCCESS] https://ci.openlp.io/job/Branch-02-Functional-Tests/1557/
[SUCCESS] https://ci.openlp.io/job/Branch-03-Interface-Tests/1495/
[SUCCESS] https://ci.openlp.io/job/Branch-04a-Windows_Functional_Tests/1261/
[SUCCESS] https://ci.openlp.io/job/Branch-04b-Windows_Interface_Tests/851/
[SUCCESS] https://ci.openlp.io/job/Branch-05a-Code_Analysis/920/
[SUCCESS] https://ci.openlp.io/job/Branch-05b-Test_Coverage/788/
-- 
Your team OpenLP Core is requested to review the proposed merge of lp:~alisonken1/openlp/projector-cleanups into lp:openlp.
=== modified file 'openlp/core/common/__init__.py'
--- openlp/core/common/__init__.py	2016-06-18 02:45:02 +0000
+++ openlp/core/common/__init__.py	2016-07-09 11:31:39 +0000
@@ -195,7 +195,7 @@
     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 @@
     :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 @@
     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()

=== modified file 'openlp/core/lib/projector/pjlink1.py'
--- openlp/core/lib/projector/pjlink1.py	2016-06-18 02:45:02 +0000
+++ openlp/core/lib/projector/pjlink1.py	2016-07-09 11:31:39 +0000
@@ -49,7 +49,7 @@
 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 @@
             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))

=== modified file 'tests/functional/openlp_core_common/test_projector_utilities.py'
--- tests/functional/openlp_core_common/test_projector_utilities.py	2016-06-18 02:45:02 +0000
+++ tests/functional/openlp_core_common/test_projector_utilities.py	2016-07-09 11:31:39 +0000
@@ -124,7 +124,7 @@
         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 @@
         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 @@
         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 @@
         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 @@
         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')


Follow ups