From 97140b19ba5f39867d6a5deae3aa61dfac6dc7bc Mon Sep 17 00:00:00 2001 From: Oliver Walters Date: Sun, 31 Jan 2021 21:45:34 +1100 Subject: [PATCH] Limit barcode hash to printable characters. This is a dirty filthy hack, as the web-input strips non printable chars when they are typed in (but will accept them if they are copy-pasted) --- InvenTree/barcode/barcode.py | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/InvenTree/barcode/barcode.py b/InvenTree/barcode/barcode.py index 87235db190..3837fef599 100644 --- a/InvenTree/barcode/barcode.py +++ b/InvenTree/barcode/barcode.py @@ -1,5 +1,6 @@ # -*- coding: utf-8 -*- +import string import hashlib import logging @@ -16,9 +17,18 @@ logger = logging.getLogger(__name__) def hash_barcode(barcode_data): """ - Calculate an MD5 hash of barcode data + Calculate an MD5 hash of barcode data. + + HACK: Remove any 'non printable' characters from the hash, + as it seems browers will remove special control characters... + + TODO: Work out a way around this! """ + printable_chars = filter(lambda x: x in string.printable, barcode_data) + + barcode_data = ''.join(list(printable_chars)) + hash = hashlib.md5(str(barcode_data).encode()) return str(hash.hexdigest())