def validate(number, strip_check_digit=True): """Checks to see if the number provided is a valid MEID number. This converts the representation format of the number (if it is decimal it is not converted to hexadecimal).""" from stdnum import luhn # first parse the number number, cd = _parse(number) if len(number) == 18: # decimal format can be easily determined if cd: luhn.validate(number + cd) # convert to hex manufacturer_code = int(number[0:10]) serial_num = int(number[10:18]) if manufacturer_code.bit_length() > 32 or serial_num.bit_length() > 24: raise InvalidComponent() number = "%08X%06X" % (manufacturer_code, serial_num) cd = calc_check_digit(number) elif number.isdigit(): # if the remaining hex format is fully decimal it is an IMEI number from stdnum import imei imei.validate(number + cd) else: # normal hex Luhn validation if cd: luhn.validate(number + cd, alphabet=_hex_alphabet) if strip_check_digit: cd = "" return number + cd
def validate(number, strip_check_digit=True): """Checks to see if the number provided is a valid MEID number. This converts the representation format of the number (if it is decimal it is not converted to hexadecimal).""" from stdnum import luhn # first parse the number number, cd = _parse(number) if len(number) == 18: # decimal format can be easily determined if cd: luhn.validate(number + cd) # convert to hex number = '%08X%06X' % (int(number[0:10]), int(number[10:18])) cd = calc_check_digit(number) elif number.isdigit(): # if the remaining hex format is fully decimal it is an IMEI number from stdnum import imei imei.validate(number + cd) else: # normal hex Luhn validation if cd: luhn.validate(number + cd, alphabet=_hex_alphabet) if strip_check_digit: cd = '' return number + cd
def validate(number, strip_check_digit=True): """Check if the number is a valid MEID number. This converts the representation format of the number (if it is decimal it is not converted to hexadecimal).""" # first parse the number number, cd = _parse(number) from stdnum import luhn if len(number) == 18: # decimal format can be easily determined if cd: luhn.validate(number + cd) # convert to hex manufacturer_code = int(number[0:10]) serial_num = int(number[10:18]) if _bit_length(manufacturer_code) > 32 or _bit_length(serial_num) > 24: raise InvalidComponent() number = '%08X%06X' % (manufacturer_code, serial_num) cd = calc_check_digit(number) elif isdigits(number): # if the remaining hex format is fully decimal it is an IMEI number from stdnum import imei imei.validate(number + cd) else: # normal hex Luhn validation if cd: luhn.validate(number + cd, alphabet=_hex_alphabet) if strip_check_digit: cd = '' return number + cd
def convert_check_imei(self, data): if data.get('imei', None): data['imei'] = int(imei.validate(data['imei'])) return data
def is_valid_imei(imei_num: str) -> bool: try: imei.validate(imei_num) except Exception as e: return False return True