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

This is merely a historical archive of years 2008-2021, before the migration to mailman3.

A maintained and still updated list archive can be found at https://lists.osmocom.org/hyperkitty/list/gerrit-log@lists.osmocom.org/.

laforge gerrit-no-reply at lists.osmocom.org
Fri Mar 12 07:35:38 UTC 2021


laforge has submitted this change. ( 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
Related: OS#4963
---
M pySim/utils.py
1 file changed, 22 insertions(+), 0 deletions(-)

Approvals:
  laforge: Looks good to me, approved
  Jenkins Builder: Verified



diff --git a/pySim/utils.py b/pySim/utils.py
index d75a000..f452e1d 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: 3
Gerrit-Owner: dexter <pmaier at sysmocom.de>
Gerrit-Reviewer: Jenkins Builder
Gerrit-Reviewer: laforge <laforge at osmocom.org>
Gerrit-Reviewer: pespin <pespin at sysmocom.de>
Gerrit-MessageType: merged
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osmocom.org/pipermail/gerrit-log/attachments/20210312/71c16bbc/attachment.htm>


More information about the gerrit-log mailing list