forked from openlp/openlp
Allow md5check to use non-ascii encoding, added md5_filecheck
This commit is contained in:
parent
cfa8e4f2df
commit
b02d44416a
@ -190,31 +190,58 @@ def verify_ip_address(addr):
|
||||
return True if verify_ipv4(addr) else verify_ipv6(addr)
|
||||
|
||||
|
||||
def md5_hash(salt, data):
|
||||
def md5_filecheck(input_file, check):
|
||||
"""
|
||||
Validate a downloaded file has the same MD5 as recorded.
|
||||
|
||||
:param input_file: File to test
|
||||
:param check: MD5Sum to validate
|
||||
:return: File MD5 == check
|
||||
"""
|
||||
log.debug('md5_filecheck(file="{}", check="{}"'.format(input_file, check))
|
||||
try:
|
||||
file_ = open(input_file, 'rb')
|
||||
except OSError as err:
|
||||
log.error('Unable to check "{}"'.format(input_file))
|
||||
log.error('Error code: {}'.format(err.errno))
|
||||
log.error('Error msg: {}'.format(err.strerror))
|
||||
return False
|
||||
hash_obj = hashlib.new('md5')
|
||||
while True:
|
||||
data = file_.read(1024)
|
||||
if not data:
|
||||
break
|
||||
hash_obj.update(data)
|
||||
log.debug('md5_filecheck validating "{}"'.format(hash_obj.hexdigest()))
|
||||
return hash_obj.hexdigest() == check
|
||||
|
||||
|
||||
def md5_hash(salt, data=None):
|
||||
"""
|
||||
Returns the hashed output of md5sum on salt,data
|
||||
using Python3 hashlib
|
||||
|
||||
:param salt: Initial salt
|
||||
:param data: Data to hash
|
||||
:param data: OPTIONAL Data to hash
|
||||
:returns: str
|
||||
"""
|
||||
log.debug('md5_hash(salt="%s")' % salt)
|
||||
hash_obj = hashlib.new('md5')
|
||||
hash_obj.update(salt.encode('ascii'))
|
||||
hash_obj.update(data.encode('ascii'))
|
||||
hash_obj.update(salt)
|
||||
if data:
|
||||
hash_obj.update(data)
|
||||
hash_value = hash_obj.hexdigest()
|
||||
log.debug('md5_hash() returning "%s"' % 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
|
||||
using PyQt4.QCryptographicHash.
|
||||
|
||||
:param salt: Initial salt
|
||||
:param data: Data to hash
|
||||
:param data: OPTIONAL Data to hash
|
||||
:returns: str
|
||||
"""
|
||||
log.debug('qmd5_hash(salt="%s"' % salt)
|
||||
@ -223,7 +250,7 @@ def qmd5_hash(salt, data):
|
||||
hash_obj.addData(data)
|
||||
hash_value = hash_obj.result().toHex()
|
||||
log.debug('qmd5_hash() returning "%s"' % hash_value)
|
||||
return decode(hash_value.data(), 'ascii')
|
||||
return hash_value.data()
|
||||
|
||||
|
||||
def clean_button_text(button_text):
|
||||
|
@ -343,7 +343,7 @@ class PJLink1(QTcpSocket):
|
||||
# Authenticated login with salt
|
||||
log.debug('(%s) Setting hash with salt="%s"' % (self.ip, data_check[2]))
|
||||
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], data=self.pin).decode('ascii')
|
||||
else:
|
||||
salt = None
|
||||
# We're connected at this point, so go ahead and do regular I/O
|
||||
|
@ -23,9 +23,16 @@
|
||||
Package to test the openlp.core.ui.projector.networkutils package.
|
||||
"""
|
||||
|
||||
import os
|
||||
|
||||
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, md5_filecheck
|
||||
|
||||
TEST_PATH = os.path.abspath(os.path.join(os.path.dirname(__file__),
|
||||
'..', '..', 'resources', 'projector'))
|
||||
TEST_FILENAME = 'Wheat.otz'
|
||||
TEST_FILE_HASH = '2d7daae6f8ce6dc0963ea8c0fa4d67aa'
|
||||
|
||||
salt = '498e4a67'
|
||||
pin = 'JBMIAProjectorLink'
|
||||
@ -120,7 +127,7 @@ class testProjectorUtilities(TestCase):
|
||||
Test MD5 hash from salt+data pass (python)
|
||||
"""
|
||||
# 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
|
||||
self.assertEquals(hash_, test_hash, 'MD5 should have returned a good hash')
|
||||
@ -130,7 +137,7 @@ class testProjectorUtilities(TestCase):
|
||||
Test MD5 hash from salt+data fail (python)
|
||||
"""
|
||||
# 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
|
||||
self.assertNotEquals(hash_, test_hash, 'MD5 should have returned a bad hash')
|
||||
@ -143,7 +150,7 @@ class testProjectorUtilities(TestCase):
|
||||
hash_ = qmd5_hash(salt=salt, data=pin)
|
||||
|
||||
# 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):
|
||||
"""
|
||||
@ -153,4 +160,17 @@ class testProjectorUtilities(TestCase):
|
||||
hash_ = qmd5_hash(salt=pin, data=salt)
|
||||
|
||||
# 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 md5sum_filecheck_test(self):
|
||||
"""
|
||||
Test file MD5SUM check
|
||||
"""
|
||||
# GIVEN: Test file to verify
|
||||
test_file = os.path.join(TEST_PATH, TEST_FILENAME)
|
||||
|
||||
# WHEN: md5_filecheck is called
|
||||
valid = md5_filecheck(test_file, TEST_FILE_HASH)
|
||||
|
||||
# THEN: Check should pass
|
||||
self.assertEqual(valid, True, 'Test file MD5 should match test MD5')
|
||||
|
BIN
tests/resources/projector/Wheat.otz
Normal file
BIN
tests/resources/projector/Wheat.otz
Normal file
Binary file not shown.
Loading…
Reference in New Issue
Block a user