def fastrandInt(count=None, max=512): """ Wrapper for random.randint(min, max) WARNING: this is not intended for cryptographic randomness, in that case use os.urandom(). count - int the number of integers to return. Defaults to 1. min - fast as 0. max - int the largest value of integers to return. Defaults to 512. """ min = 0 if max is None or (isinstance(max, int) is False) or max <= 0 or max <= min: max = 512 x_count = ensurePositiveCount(count) try: if x_count <= 1: try: import six if six.PY2: return int(int(bytearray(os.urandom(16))[8]) % max) else: return int(os.urandom(max), max) except Exception: return int(int(bytearray(os.urandom(16))[8]) % max) else: return [fastrandInt(1, max) for someInt in range(x_count)] except Exception as err: remediation.error_breakpoint(err, str(u'FAILED DURING RAND-INT. ABORT.')) err = None del err raise AssertionError("IMPOSSIBLE STATE REACHED IN RAND-INT. ABORT.")
def randInt(count=None, min=0, max=512): """ Wrapper for random.randint(min, max) WARNING: this is not intended for cryptographic randomness, in that case use os.urandom(). count - int the number of integers to return. Defaults to 1. min - int the smallest absolute value of integers to return. Defaults to 0. max - int the largest absolute value of integers to return. Defaults to 512. """ if min is None or (isinstance(min, int) is False) or min <= 0: return int(fastrandInt(1, max)) if max is None or (isinstance(max, int) is False) or max <= 0 or max <= min: max = 512 x_count = ensurePositiveCount(count) try: if x_count <= 1: entropy_seed = int(fastrandInt(1, max)) if min <= entropy_seed: return entropy_seed else: return int(min) else: return [randInt(1, min, max) for someInt in range(x_count)] except Exception as err: remediation.error_breakpoint(err, str(u'FAILED DURING RAND-INT. ABORT.')) err = None del err raise AssertionError("IMPOSSIBLE STATE REACHED IN RAND-INT. ABORT.")
def randIP(count=None, min=0, max=256): """ mostly for testing, generates random ip WARNING: this is not intended for cryptographic randomness, in that case use os.urandom(). count - int the number of integers to return. Defaults to 1. min - int the smallest value of integers to return. Defaults to 0. max - int the largest value of integers to return. Defaults to 512. """ if min is None or (isinstance(min, int) is False) or min <= 0: min = 1 if max is None or (isinstance(max, int) is False) or max <= 0 or max <= min or max >= 256: max = 255 x_count = ensurePositiveCount(count) try: if x_count <= 1: return str( str("{0}.{1}.{2}.{3}").format(randInt(1, min, max), randInt(1, min, max), randInt(1, min, max), randInt(1, min, max))) else: theResult = [] for someIP in range(x_count): theResult.append(randIP(1, min, max)) return theResult except Exception as err: remediation.error_breakpoint(err, str(u'FAILED DURING RAND-IIP. ABORT.')) err = None del err raise AssertionError("IMPOSSIBLE STATE REACHED IN RAND-IP. ABORT.")
def upgradepip(): """Upgrade pip via pip.""" try: pip.main(args=["install", "--upgrade", "pip"]) except Exception as permErr: remediation.error_breakpoint(permErr) permErr = None del(permErr) return None
def main(argv=None): """The main event""" # print("PiAP Keyring") ecode = int(0) try: args, extra = parseArgs(argv) keyring_cmd = args.keyring_unit useKeyTool(keyring_cmd, argv[1:]) except Exception as cerr: remediation.error_breakpoint(cerr, str(u'piaplib.keyring.__MAIN__.main()')) ecode = int(3) return ecode
def useKeyTool(tool, arguments=[None]): """Handler for launching pocket-tools.""" theResult = None if tool is not None and tool in KEYRING_UNITS.keys(): try: theResult = KEYRING_UNITS[tool].main(arguments) except Exception as err: remediation.error_breakpoint( err, u'piaplib.keyring.__MAIN__.useKeyTool') err = None del err theResult = None return theResult
def randStr(count=None): """wrapper for str(os.urandom())""" x_count = sanitizeCount(count) try: import string choices = [randInt(1, 1, 99) for x in range(x_count)] return str("").join( [string.printable[m] for m in choices if m is not int(69)]) except Exception as err: remediation.error_breakpoint(err, str(u'FAILED DURING RAND-STR. ABORT.')) err = None del err raise AssertionError("BUG: BAD STATE REACHED IN RAND-STR. ABORT.")
def randBool(count=None): """wrapper for str(os.urandom())""" x_count = ensurePositiveCount(count) try: if x_count == 1: return (bool(((randInt(1, 0, 512) % 2) == 0)) is True) else: theResult = [] for someInt in range(x_count): theResult.append(randBool(1)) return theResult except Exception as err: remediation.error_breakpoint(err, str(u'FAILED DURING RAND-BOOL. ABORT.')) err = None del err raise AssertionError("IMPOSSIBLE STATE REACHED IN RAND-BOOL. ABORT.")
def upgradeAPT(): """Upgrade system via apt.""" try: import apt import apt.cache cache = apt.Cache() cache.update() cache.open(None) cache.upgrade() for pkg in cache.get_changes(): logs.log(str((pkg.name, pkg.isupgradeable)), "Info") raise NotImplementedError("[CWE-758] - Pocket upgrade upgradeAPT() not implemented. Yet.") except Exception as permErr: remediation.error_breakpoint(permErr, "upgradeAPT") permErr = None del(permErr) return None
def randChar(count=None): """wrapper for str(os.urandom())""" x_count = ensurePositiveCount(count) try: theRandomResult = str("") for char_x in range(x_count): char_rand_seed = str(RAND_CHARS[randInt(1, 0, 65)]) while (str(char_rand_seed).isalnum() or str(char_rand_seed).isspace()) is False: char_rand_seed = str(RAND_CHARS[randInt(1, 0, 65)]) if (randBool(1)): char_rand_seed = str(char_rand_seed).upper() theRandomResult = str("{0}{1}").format(theRandomResult, str(char_rand_seed)) return theRandomResult except Exception as err: remediation.error_breakpoint(err, str(u'FAILED DURING RAND-CHAR. ABORT.')) print(str(RAND_CHARS)) print(str(len(RAND_CHARS))) err = None del err raise AssertionError("IMPOSSIBLE STATE REACHED IN RAND-CHAR. ABORT.")
def randSSID(count=None): """ generate random SSID WARNING: this is a placeholder. count - length to return. Defaults to 20. """ x_count = ensurePositiveCount(count) try: if x_count <= 1: return randChar(1) else: theResult = "" for someChar in range(x_count): nextChar = randChar(1) while not (nextChar.isalpha() or nextChar.isdigit()): nextChar = randChar(1) theResult = theResult.join(nextChar) return theResult except Exception as err: remediation.error_breakpoint(err, str(u'FAILED DURING RAND-SSID. ABORT.')) err = None del err raise AssertionError("IMPOSSIBLE STATE REACHED IN RAND-SSID. ABORT.")
return RANDOM_TASKS[tool](*args, **kwargs) else: raise NotImplementedError( "[CWE-758] IMPOSSIBLE STATE REACHED IN RAND-TOOL. ABORT.") @remediation.bug_handling def main(argv=None): """Simple but Random Main event.""" try: args = parseArgs(argv) print(useRandTool(args.random_action, args.count)) except Exception as err: err = None del (err) return 3 return 0 if __name__ in u'__main__': try: error_code = main(sys.argv[1:]) exit(error_code) except Exception as err: remediation.error_breakpoint( err, str(u'[CWE-233] MAIN FAILED DURING RAND. ABORT.')) del err exit(255) finally: exit(0)
theResult = None return theResult @remediation.error_handling def main(argv=None): """The main event""" # print("PiAP Keyring") ecode = int(0) try: args, extra = parseArgs(argv) keyring_cmd = args.keyring_unit useKeyTool(keyring_cmd, argv[1:]) except Exception as cerr: remediation.error_breakpoint(cerr, str(u'piaplib.keyring.__MAIN__.main()')) ecode = int(3) return ecode if __name__ in u'__main__': try: error_code = main(sys.argv[1:]) exit(error_code) except Exception as err: remediation.error_breakpoint(err, str(u'piaplib.keyring.__MAIN__')) del err exit(255) finally: exit(0)