aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorPhilipp Maier <pmaier@sysmocom.de>2021-03-09 21:28:25 +0100
committerlaforge <laforge@osmocom.org>2021-03-12 07:35:37 +0000
commit47236500fefec5c2b466752849672ce43bc9b4b7 (patch)
tree3beb1752c633b1bd304202d4a1f5323a49cba254
parent786f781a5fdf178c8c73a02e84dee6467e321b72 (diff)
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
-rw-r--r--pySim/utils.py22
1 files changed, 22 insertions, 0 deletions
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 @@ def enc_addr_tlv(addr, addr_type='00'):
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