def get_random(valid: bool = True) -> dict: """Returns a random ID with it's corresponding private key""" key = create_key() pubkey = parse_key(key.publickey()) username = random_word(random.randint(1, USERNAME_LIMIT)) if valid is False: username = "******" * (USERNAME_LIMIT + 1) return {"private_key": parse_key(key), "id": ID(username, pubkey)}
def create_keys(self): """Create a new pair of private and public keys""" try: # If we've already created a private key before, import it # Otherwise, create it with open(self.key_path, "r") as f: private_key = RSA.import_key(f.read(), passphrase=self.__password) public_key = private_key.publickey() except IOError: private_key = create_key() public_key = private_key.publickey() with open(self.key_path, "wb") as f: f.write( private_key.export_key("PEM", passphrase=self.__password)) self.__private_key = parse_key(private_key) self.__public_key = parse_key(public_key)
def container(rio, mp_id): s = "{}@container@{}@{}" ks = rio.get_keys(s.format(mp_id, "*", "title")) ret = [] for k in sorted(ks): _, _, idx, _, _, _ = utils.parse_key(k) ctrl_key = s.format(mp_id, idx, "ctrl") ret.append({"value_key": utils.client_key(k), "ctrl_key": utils.client_key(ctrl_key), "ctrl_value": utils.val_to_class(rio.get_val(ctrl_key)), "value":rio.get_val(k), "idx": idx, "link":"/{}/container/{}".format(mp_id, idx)}) return ret
def definitions(rio, mp_id): s = "{}@definitions@{}@{}" ks = rio.get_keys(s.format(mp_id, "*","descr" )) ret = [] for k in sorted(ks): _, _, idx, _, _, _ = utils.parse_key(k) ctrl_key = s.format(mp_id, idx, "ctrl") ret.append({"value_key":utils.client_key(k), "ctrl_key": utils.client_key(ctrl_key), "ctrl_value": utils.val_to_class(rio.get_val(ctrl_key)), "value":rio.get_val(k), "idx": idx, "link":"/{}/definitions/{}".format(mp_id, idx)}) return ret
def build_val_dict(rio, keys, parse=False): val ={} for key in keys: _, _, _, _, j, k = utils.parse_key(key) if not val.get(j): val[j] = {} if not val.get(j).get(k): val[j][k] = {} val[j][k]["key"] = utils.client_key(key) v = rio.get_val(key) if parse: v = json.loads(v) val[j][k]["value"] = v return val
MIN_RULES = contract_config["minimum_rules"] MAX_RULES = contract_config["maximum_rules"] SENDER_CAN_JUDGE = contract_config["allow_sender_to_judge"] SENTECE_CHAR_LIMIT = verdict_config["sentence_char_limit"] DESCRIPTION_CHAR_LIMIT = verdict_config["description_char_limit"] HASH_DIFFICULTY = id_config["hash_difficulty"] NONCE_LIMIT = id_config["nonce_limit"] USERNAME_LIMIT = id_config["username_char_limit"] valid_id = ID.get_random() userid = valid_id["id"] key = import_key(valid_id["private_key"]) public_key = key.publickey() parsed_pubkey = parse_key(public_key) def test_contract(): # Valid random contract c = Contract.get_random()["contract"] assert c.is_valid() == True # Invalid random contract c = Contract.get_random(valid=False)["contract"] assert c.is_valid() == False judges = [ID("Agatha Christie", parsed_pubkey) for _ in range(MIN_JUDGES)] rule = { "content": "You shall not kill",