Change in pysim[master]: utils: add is_hex function to check hex strings

dexter gerrit-no-reply at lists.osmocom.org
Thu Mar 11 13:43:10 UTC 2021


dexter has uploaded this change for review. ( https://gerrit.osmocom.org/c/pysim/+/23320 )


Change subject: utils: add is_hex function to check hex strings
......................................................................

utils: add is_hex function to check hex strings

since we have added pySim-shell.py that has a lot of locations where the
user can enter hexadecimal data there is an increased need for input
validation. Lets add a central is_hex function that verifies hex
strings.

Change-Id: Ia29a13c9215357dd2adf141f2ef222c823f8456d
---
M pySim/utils.py
1 file changed, 22 insertions(+), 0 deletions(-)



  git pull ssh://gerrit.osmocom.org:29418/pysim refs/changes/20/23320/1

diff --git a/pySim/utils.py b/pySim/utils.py
index 6dbd980..e92dbdf 100644
--- a/pySim/utils.py
+++ b/pySim/utils.py
@@ -577,6 +577,28 @@
 
 	return s
 
+def is_hex(string, minlen=2, maxlen=None) -> bool:
+	"""
+	Check if a string is a valid hexstring
+	"""
+
+	# Filter obviously bad strings
+	if not string:
+		return False
+	if len(string) < minlen or minlen < 2:
+		return False
+	if len(string) % 2:
+		return False
+	if maxlen and len(string) > maxlen:
+		return False
+
+	# Try actual encoding to be sure
+	try:
+		try_encode = h2b(string)
+		return True
+	except:
+		return False
+
 def sanitize_pin_adm(pin_adm, pin_adm_hex = None):
 	"""
 	The ADM pin can be supplied either in its hexadecimal form or as

-- 
To view, visit https://gerrit.osmocom.org/c/pysim/+/23320
To unsubscribe, or for help writing mail filters, visit https://gerrit.osmocom.org/settings

Gerrit-Project: pysim
Gerrit-Branch: master
Gerrit-Change-Id: Ia29a13c9215357dd2adf141f2ef222c823f8456d
Gerrit-Change-Number: 23320
Gerrit-PatchSet: 1
Gerrit-Owner: dexter <pmaier at sysmocom.de>
Gerrit-MessageType: newchange
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osmocom.org/pipermail/gerrit-log/attachments/20210311/d2731a47/attachment.htm>


More information about the gerrit-log mailing list