pySim-shell: Validate that argument to 'apdu' command is proper hexstr
Let's not even send anything to the card if it's not an even number
of hexadecimal digits
Change-Id: I58465244101cc1a976e5a17af2aceea1cf9f9b54
diff --git a/pySim/utils.py b/pySim/utils.py
index 92bf70f..ea1c9e6 100644
--- a/pySim/utils.py
+++ b/pySim/utils.py
@@ -1478,3 +1478,12 @@
if len(instr) & 1:
raise ValueError('Input has un-even number of hex digits')
return instr
+
+def is_hexstr(instr: str) -> str:
+ """Method that can be used as 'type' in argparse.add_argument() to validate the value consists of
+ an even sequence of hexadecimal digits only."""
+ if not all(c in string.hexdigits for c in instr):
+ raise ValueError('Input must be hexadecimal')
+ if len(instr) & 1:
+ raise ValueError('Input has un-even number of hex digits')
+ return instr