diff --git a/pySim/esim/saip/param_source.py b/pySim/esim/saip/param_source.py index fb6151ea..87842619 100644 --- a/pySim/esim/saip/param_source.py +++ b/pySim/esim/saip/param_source.py @@ -18,6 +18,7 @@ # along with this program. If not, see . import random +import re from pySim.utils import all_subclasses_of class ParamSourceExn(Exception): @@ -70,7 +71,28 @@ class ConstantSource(ParamSource): def get_next(self, csv_row:dict=None): return self.val -class RandomDigitSource(ParamSource): +class InputExpandingParamSource(ParamSource): + + @classmethod + def expand_str(cls, s:str): + # user convenience syntax '0*32' becomes '00000000000000000000000000000000' + if '*' not in s: + return s + tokens = re.split(r"([^ \t]+)[ \t]*\*[ \t]*([0-9]+)", s) + if len(tokens) < 3: + return s + parts = [] + for unchanged, snippet, repeat_str in zip(tokens[0::3], tokens[1::3], tokens[2::3]): + parts.append(unchanged) + repeat = int(repeat_str) + parts.append(snippet * repeat) + return ''.join(parts) + + @classmethod + def from_str(cls, s:str): + return cls(cls.expand_str(s)) + +class RandomDigitSource(InputExpandingParamSource): 'return a different sequence of random decimal digits each' is_abstract = False name = 'random decimal digits' @@ -100,6 +122,8 @@ class RandomDigitSource(ParamSource): @classmethod def from_str(cls, s:str): + s = cls.expand_str(s) + if '..' in s: first_str, last_str = s.split('..') first_str = first_str.strip() @@ -112,7 +136,7 @@ class RandomDigitSource(ParamSource): last_value = int(last_str) if last_str is not None else '9' * len(first_str) return cls(num_digits=len(first_str), first_value=first_value, last_value=last_value) -class RandomHexDigitSource(ParamSource): +class RandomHexDigitSource(InputExpandingParamSource): 'return a different sequence of random hexadecimal digits each' is_abstract = False name = 'random hexadecimal digits' @@ -133,6 +157,7 @@ class RandomHexDigitSource(ParamSource): @classmethod def from_str(cls, s:str): + s = cls.expand_str(s) return cls(num_digits=len(s.strip())) class IncDigitSource(RandomDigitSource): diff --git a/pySim/esim/saip/personalization.py b/pySim/esim/saip/personalization.py index 89c272cc..619f3819 100644 --- a/pySim/esim/saip/personalization.py +++ b/pySim/esim/saip/personalization.py @@ -430,7 +430,7 @@ class Iccid(DecimalParam): name = 'ICCID' min_len = 18 max_len = 20 - default_value = '0' * 18 + default_value = '0*18' default_source = param_source.IncDigitSource @classmethod @@ -581,7 +581,7 @@ class SdKey(BinaryParam): class SdKeyAes(SdKey): key_type = KeyType.aes allow_len = (16,24,32) - default_value = '00' * 32 + default_value = '00*32' class SdKeyScp80(SdKeyAes): @@ -873,7 +873,7 @@ class Puk(DecimalHexParam): allow_len = 8 rpad = 16 keyReference = None - default_value = '0' * allow_len + default_value = f'0*{allow_len}' default_source = param_source.RandomDigitSource @classmethod @@ -911,7 +911,7 @@ class Pin(DecimalHexParam): rpad = 16 min_len = 4 max_len = 8 - default_value = '0' * max_len + default_value = f'0*{max_len}' default_source = param_source.RandomDigitSource keyReference = None @@ -952,7 +952,7 @@ class Pin(DecimalHexParam): class Pin1(Pin): is_abstract = False name = 'PIN1' - default_value = '0' * 4 # PIN are usually 4 digits + default_value = '0*4' # PIN are usually 4 digits keyReference = 0x01 class Pin2(Pin1): @@ -1057,7 +1057,7 @@ class K(BinaryParam, AlgoConfig): name = 'K' algo_config_key = 'key' allow_len = int(128/8) # length in bytes (from BinaryParam) - default_value = '00' * allow_len + default_value = f'00*{allow_len}' default_source = param_source.RandomHexDigitSource class Opc(K):