Exemple #1
0
def oracle_cbc_ecb(plaintext):
    # Generate random key
    key = urandom(16)
    # Alternative way
    # key = long_to_bytes(randbits(128)) --> Error sometimes it generates less bytes
    # Probably long_to_bytes didn't work properly

    # Instance secret_generator
    secrets_generator = SystemRandom()

    # Prepend and append chunk of 5 --> 10 bytes
    chunk = secrets_generator.randint(5, 10)
    plaintext = urandom(chunk) + plaintext
    chunk = secrets_generator.randint(5, 10)
    plaintext += urandom(chunk)

    # Choose to encrypt using CBC or ECB
    option = secrets_generator.randint(0, 1)
    if option == 0:
        cbc = CBC(key, blocksize)
        iv = urandom(16)
        return cbc.encrypt(iv, plaintext)
    else:
        pad = PKCS7(blocksize)
        ecb = AES.new(key, AES.MODE_ECB)
        return ecb.encrypt(pad.encode(plaintext))
Exemple #2
0
def split(secret: str, recombination_threshold: int,
          num_shares: int) -> (Share, ...):
    """split secret into shares using Shamir's Secret Sharing Algorithm"""
    # https://en.wikipedia.org/wiki/Shamir%27s_Secret_Sharing
    assert num_shares >= recombination_threshold > 1, 'Hmmm... invalid n of m specification'
    assert secret, 'Hmmm... need a secret to split'
    secret = secret.encode()
    secret_identifier = str(uuid4())
    f_0 = int.from_bytes(secret, byteorder='big', signed=False)
    mersenne, modulus = _find_suitable_mersenne_prime(f_0)
    rand = SystemRandom()
    coefficients = (f_0, ) + tuple(
        rand.randint(1, modulus - 1)
        for _ in range(recombination_threshold - 1))

    # value of polynomial defined by coefficients at x in modulo modulus
    def f_x(x: int) -> int:
        return sum((a * ((x**i) % modulus) % modulus)
                   for i, a in enumerate(coefficients)) % modulus

    x_values = set()
    while len(x_values) < num_shares:
        x_values.add(rand.randint(1, 10**6))
    return tuple(
        Share(id=secret_identifier, mersenne=mersenne, x=x, y=f_x(x))
        for x in x_values)
Exemple #3
0
 def test__modulo_inverse(self):
     from secrets import SystemRandom
     rand = SystemRandom()
     for i in (32, 64, 128, 256):
         x = rand.randint(0, 2**i)
         _, p = cloak.secret_sharing._find_suitable_mersenne_prime(x)
         y = cloak.secret_sharing._modulo_inverse(x=x, modulus=p)
         self.assertEqual(1, (x * y) % p)
Exemple #4
0
def system_random_without_seed():
    system_random = SystemRandom()

    result = [system_random.randint(1, 10) for i in range(1000)]
    counter = Counter(result)

    for key, value in sorted(counter.items()):
        print(key, ': ', value)
Exemple #5
0
def generate_token(length=30, chars=UNICODE_ASCII_CHARACTER_SET):
    """Generates a non-guessable OAuth token

    OAuth (1 and 2) does not specify the format of tokens except that they
    should be strings of random characters. Tokens should not be guessable
    and entropy when generating the random characters is important. Which is
    why SystemRandom is used instead of the default random.choice method.
    """
    rand = SystemRandom()
    return ''.join(rand.choice(chars) for x in range(length))
def randInt(n):
    cryptogen = SystemRandom()
    k = log2(n)
    if(k <= 0):
        k=1
    k = ceil(k)
    r = n
    while(r >= n or r<=0):
        r = cryptogen.getrandbits(k)
    return  r
Exemple #7
0
def generate_token(length=30, chars=UNICODE_ASCII_CHARACTER_SET):
    """Generates a non-guessable OAuth token

    OAuth (1 and 2) does not specify the format of tokens except that they
    should be strings of random characters. Tokens should not be guessable
    and entropy when generating the random characters is important. Which is
    why SystemRandom is used instead of the default random.choice method.
    """
    rand = SystemRandom()
    return ''.join(rand.choice(chars) for x in range(length))
Exemple #8
0
 def test__find_suitable_mersenne_prime(self):
     from secrets import SystemRandom
     rand = SystemRandom()
     for i in (32, 64, 128, 256):
         x = rand.randint(0, 2**i)
         m, p = cloak.secret_sharing._find_suitable_mersenne_prime(x)
         self.assertIsInstance(m, int)
         self.assertIsInstance(p, int)
         self.assertGreater(p, x)
         for m_ in (i for i in cloak.known_mersenne_primes if 100 < i < m):
             self.assertLess(cloak.mersenne_prime(m_), x)
         self.assertIn(m, cloak.known_mersenne_primes)
def randPrime(k):
    p = 0
    if(k==2):
        while(p <= 1):
            p = SystemRandom().getrandbits(1)
            mask = 1 << 1
            p |= mask
        return p
    while(not isPrime(p, PrimAcc)):
        p = SystemRandom().getrandbits(k-2)
        p |= 1
        mask = 1 << (k-1)
        p |= mask
    return p
 def test_mersenne_prime(self):
     from math import log2
     from secrets import SystemRandom
     rand = SystemRandom()
     for x in tuple(
             rand.randint(1, 10 ^ 9)
             for _ in range(100)) + cloak.known_mersenne_primes[0:10]:
         if x in cloak.known_mersenne_primes:
             m = cloak.mersenne_prime(x)
             self.assertIsInstance(m, int)
             self.assertEqual(x, int(log2(m + 1)))
         else:
             with self.assertRaises(AssertionError):
                 cloak.mersenne_prime(x)
Exemple #11
0
    def __init__(self, vol: float = 1, maxlength: int = 100):
        """Create a Probalist object.

        @param vol: system volume for constant conversion
        @type vol: float
        @param maxlength: maximum probalistic events to be stored
            (when run out of space, size is increased by maxlength increments)
        @type maxlength: int

        """
        # FIX check if vol can be removed and placed somewhere else !
        self.vol: float = vol
        """system volume"""
        # Check replacement with list instead of numpy array !
        self._mapobj: np.ndarray = np.zeros(maxlength, dtype=np.dtype("O"))
        """List of probalistic objects"""
        self._problist: np.ndarray = np.zeros(maxlength, dtype=np.float64)
        """List of probas"""
        self._actlist: int = 0
        """Next available position"""
        self._maxlength: int = maxlength
        """maximum probalistic event increment to be stored"""
        self.probtot: float = 0.0
        """total probability"""
        self._queue: Deque[int] = deque()
        """Queue of freed positions"""
        self.sysrand: SystemRandom = SystemRandom()
        """random number generator (uses system generator for optimal entropy)"""
Exemple #12
0
def generate_token(request, length=30, chars=UNICODE_ASCII_CHARACTER_SET):
    """Generates a non-guessable OAuth Json Web Token
    OAuth (1 and 2) does not specify the format of tokens except that they
    should be strings of random characters. Tokens should not be guessable
    and entropy when generating the random characters is important. Which is
    why SystemRandom is used instead of the default random.choice method.
    """
    from django.conf import settings
    from jose import jwt

    rand = SystemRandom()
    secret = getattr(settings, 'SECRET_KEY')

    token = ''.join(rand.choice(chars) for x in range(length))
    jwtted_token = jwt.encode({'token': token}, secret, algorithm='HS256')
    return jwtted_token
Exemple #13
0
def sample_poly_ternary(parms):
    """Generate a ternary polynomial uniformally with elements [-1, 0, 1]
    where -1 is represented as (modulus - 1) because -1 % modulus == modulus - 1.

    Used for generating secret key using coeff_modulus(list of prime nos) which
    represents as 'q' in the research paper.

    Args:
       parms (EncryptionParam): Encryption parameters.

    Returns:
        A 2-dim list having integer from [-1, 0, 1].
    """
    coeff_modulus = parms.coeff_modulus
    coeff_count = parms.poly_modulus
    coeff_mod_size = len(coeff_modulus)

    result = [0] * coeff_mod_size
    for i in range(coeff_mod_size):
        result[i] = [0] * coeff_count

    for i in range(coeff_count):
        rand_index = SystemRandom().choice([-1, 0, 1])
        if rand_index == 1:
            for j in range(coeff_mod_size):
                result[j][i] = 1
        elif rand_index == -1:
            for j in range(coeff_mod_size):
                result[j][i] = coeff_modulus[j] - 1
        else:
            for j in range(coeff_mod_size):
                result[j][i] = 0
    return result
Exemple #14
0
async def fetch_vacancy_page(link: str, session: ClientSession):
    # Put the link, title and content in a dict – so far without skills.
    for _ in range(5):
        try:
            async with session.get(link) as resp:
                html = await resp.text()
                soup = BeautifulSoup(html, "html.parser")
                title = soup.find(attrs={"data-qa": "vacancy-title"}).text
                content = soup.find(attrs={"data-qa": "vacancy-description"}).text
                vacancy_page = {"url": link, "title": title, "content": content}
                timeout = SystemRandom().uniform(1.0, 5.0)
                await asyncio.sleep(timeout)
                return vacancy_page
        except AttributeError:
            logger.warning(f"🚨 AttributeError occurred while fetching: {link}")
            return None
        except ClientPayloadError:
            logger.warning(f"🚨 ClientPayloadError occurred while fetching: {link}")
            return None
        except ServerDisconnectedError:
            logger.warning(f"🚨 ServerDisconnectedError occurred while fetching: {link}")
            await asyncio.sleep(60)
        except asyncio.TimeoutError:
            logger.warning(f"🚨 TimeoutError occurred while fetching: {link}")
            await asyncio.sleep(60)
    return None
Exemple #15
0
def main():
    # Normalize arguments
    args = docopt(__doc__)
    word_count = int(args['--length'])

    # Read and transform dictionary file
    if args['--dictionary']:
        dict_path = args['--dictionary']
    else:
        dict_path = os.path.join(os.path.dirname(__file__), 'words.txt')
    dictionary = [w for w in [l.strip() for l in open(dict_path)] if w]
    if args['--truncate']:
        dictionary = dictionary[:int(args['--truncate'])]
    elif not args['--dictionary']:
        # Default truncation for built-in dictionary
        dictionary = dictionary[:8192]

    # Basic entropy calculation
    if args['--uncontrolled']:
        entropy = math.log(math.pow(len(dictionary), word_count), 2)
    else:
        batch_size = len(dictionary) // word_count
        entropy = math.log(math.pow(batch_size, word_count) *
                           math.factorial(word_count), 2)
    if args['--verbose']:
        print("Pessimistic password entropy: %.1f bits" % entropy)
        print("Approximate time to crack at 20k/s: %.1f days" %
              (math.pow(2, entropy) / 20000 / 60 / 60 / 24))

    # Generate password
    rng = SystemRandom()
    if args['--uncontrolled']:
        # Select random words
        words = [rng.choice(dictionary) for i in range(word_count)]
    else:
        # Generate batches in random order
        batches = [dictionary[i*batch_size:(i+1)*batch_size]
                   for i in range(word_count)]
        rng.shuffle(batches)

        # Select word from each batch
        words = [rng.choice(batches[i]) for i in range(word_count)]

    # Reveal to user
    print(" ".join(words))
    if args['--complex']:
        print("Complexified: %s1." % "".join(words).capitalize())
Exemple #16
0
def encryption_oracle(plain):
    key_length = 16
    block_size = 16
    rng = SystemRandom()
    key = random_bytes(key_length)
    mode = rng.randrange(1, 3)
    iv = random_bytes(block_size)

    if mode == AES.MODE_ECB:
        aes = AES.new(key, mode)
    else:
        aes = AES.new(key, mode, iv=iv)

    prefix = random_bytes(rng.randrange(5, 11))
    suffix = random_bytes(rng.randrange(5, 11))
    plain = pad(prefix + plain + suffix, block_size)
    return aes.encrypt(plain)
Exemple #17
0
def random(key: str, length: int = None) -> str:
    LOGGER.debug("Requesting secret from %s", key)

    if key not in RANDOM_STORE:
        RANDOM_STORE[key] = "".join(
            SystemRandom().choice(string.ascii_lowercase + string.digits)

            for _ in range(length or SystemRandom().randint(12, 32))
        )

    if length is not None:
        if len(RANDOM_STORE[key]) != length:
            raise AssertionError(
                "Secret '{}' did not have expected length of {}".format(key, length)
            )

    return RANDOM_STORE[key]
Exemple #18
0
def generate_password(length=9):
    """
    Generate a random password suitable to be typed using alternated hands.
    """

    rng = SystemRandom()
    right_hand = "23456qwertasdfgzxcvbQWERTASDFGZXCVB"
    left_hand = "789yuiophjknmYUIPHJKLNM"
    first_hand = rng.randint(0, 1)
    pw = []

    for i in range(length):
        if (i + first_hand) % 2:
            pw.append(rng.choice(left_hand))
        else:
            pw.append(rng.choice(right_hand))

    return "".join(pw)
Exemple #19
0
def random_gen(size=32):
    """
		generates the size-bit random number
		size denotes the number of bits
	"""
    # with open("/dev/urandom", 'rb') as f:
    # 	return int.from_bytes(f.read(4), 'big')
    random_num = SystemRandom().getrandbits(size)
    return random_num
Exemple #20
0
    def authorization_url(self, **kwargs):
        """Generates an authorization URL.

        This is the first step in the OAuth 2.0 Authorization Flow. The user's
        browser should be redirected to the returned URL.

        This method calls
        :meth:`requests_oauthlib.OAuth2Session.authorization_url`
        and specifies the client configuration's authorization URI (usually
        Google's authorization server) and specifies that "offline" access is
        desired. This is required in order to obtain a refresh token.

        Args:
            kwargs: Additional arguments passed through to
                :meth:`requests_oauthlib.OAuth2Session.authorization_url`

        Returns:
            Tuple[str, str]: The generated authorization URL and state. The
                user must visit the URL to complete the flow. The state is used
                when completing the flow to verify that the request originated
                from your application. If your application is using a different
                :class:`Flow` instance to obtain the token, you will need to
                specify the ``state`` when constructing the :class:`Flow`.
        """
        kwargs.setdefault("access_type", "offline")
        if self.autogenerate_code_verifier:
            chars = ascii_letters + digits + "-._~"
            rnd = SystemRandom()
            random_verifier = [rnd.choice(chars) for _ in range(0, 128)]
            self.code_verifier = "".join(random_verifier)

        if self.code_verifier:
            code_hash = hashlib.sha256()
            code_hash.update(str.encode(self.code_verifier))
            unencoded_challenge = code_hash.digest()
            b64_challenge = urlsafe_b64encode(unencoded_challenge)
            code_challenge = b64_challenge.decode().split("=")[0]
            kwargs.setdefault("code_challenge", code_challenge)
            kwargs.setdefault("code_challenge_method", "S256")
        url, state = self.oauth2session.authorization_url(
            self.client_config["auth_uri"], **kwargs
        )

        return url, state
Exemple #21
0
def pad(msg, debug=0):
    """
    Apply OAEP to the given message.
    :param msg: string to pad
    :param debug: if 1, prints will be shown during execution; default 0, no prints are shown
    :return: string of 0s and 1s representing the concatenation of OAEP result, X and Y
    """

    # Create two oracles using sha-256 hash function
    oracle1 = hashlib.sha256()  # used to hash a random integer
    oracle2 = hashlib.sha256(
    )  # used to hash the result of XOR(paddedMsg, hash(randBitStr))

    # Generate a random integer that has a size of k0bits. Format the random int as a binary string making sure to
    # maintain leading zeros with the K0BitsFill argument
    rand_bit_str = format(SystemRandom().getrandbits(k0BitsInt), k0BitsFill)

    if debug:  # ONLY USE FOR DEBUG
        print('RAND BIT STRING = (%d) %s' % (len(rand_bit_str), rand_bit_str))

    # Change msg string to a binary string
    bin_msg = hex_to_binary(msg)

    if debug:  # ONLY USE FOR DEBUG
        print('BIN MSG = (%d) %s' % (len(bin_msg), bin_msg))

    zero_padded_msg = bin_msg

    # If the input msg has a binary length shorter than (nBits-k0Bits) then append k1Bits 0s to the end of the msg
    # (where k1Bits is the number of bits to make len(binMsg) = (nBits-k0Bits))
    if len(bin_msg) <= (nBits - k0BitsInt):
        k1_bits = nBits - k0BitsInt - len(bin_msg)
        zero_padded_msg += ('0' * k1_bits)

    if debug:  # ONLY USE FOR DEBUG
        print('ZERO PADDED MSG = (%d) %s' %
              (len(zero_padded_msg), zero_padded_msg))

    # Use the hashlib update method to pass the values we wish to be hashed to the oracle. Then use the hashlib
    # hexdigest method to hash the value placed in the oracle by the update method, and return the hex representation of
    # this hash. Change our hash output, zeroPaddedMsg, and randBitStr to integers to use XOR operation. Format the
    # resulting ints as binary strings. Hashing and XOR ordering follows OAEP algorithm
    oracle1.update(rand_bit_str.encode(encoding))
    x = format(
        int(zero_padded_msg, 2) ^ int(oracle1.hexdigest(), 16), n_k0BitsFill)

    if debug:  # ONLY USE FOR DEBUG
        print('X = ', x)

    oracle2.update(x.encode(encoding))
    y = format(int(oracle2.hexdigest(), 16) ^ int(rand_bit_str, 2), k0BitsFill)

    if debug:  # ONLY USE FOR DEBUG
        print('Y = ', y)

    return x + y
Exemple #22
0
    def __init__(self, seed=None, use_qrn=False, qrn_n_preload=1024):
        # Random() can be seeded for reproducability but should not be used in
        # real world applications.
        self._random = SystemRandom() if seed is None else Random(seed)

        # Storage for oracles
        self._mapping = {}

        # ANU QRNG api
        self._qrn_data = []
        self._qrn_n_preload = qrn_n_preload
        self._use_qrn = use_qrn

        if use_qrn:
            print('rng source: ANU QRNG api')
            self._qrn_preload()
        elif seed is None:
            print('rng source: secrets.SystemRandom')
        else:
            print('rng source: random.Random')
Exemple #23
0
def sameCbits():
    hashes = {}
    randomObj = SystemRandom()
    r, c = 40, 160
    nrounds = 2
    rformat = "{:0" + str(r - 2) + "b}"
    for i in range(2**(c // 2)):
        m = rformat.format(randomObj.getrandbits(r - 2))
        lastcbits = SHA3_d(m, c // 2, r + c, nrounds)[1]
        c_int = int(lastcbits, 2)
        if c_int not in hashes:
            hashes[c_int] = int(m, 2)
        elif hashes[c_int] != int(m, 2):
            print("i=", i)
            # print("Collision! Pair")
            # print(hashes[c_int], int(m, 2))
            m1, m2 = hashes[c_int], int(m, 2)
            print("m1 = ", m1, "m2 = ", m2)
            break
    return m1, m2
Exemple #24
0
    def xor_R(self):
        """
			find xor of any random c/2 + 1 r-bit strings to set the epoch randomness
		"""
        # ToDo: set_of_Rs must be atleast c/2 + 1, so make sure this - done this!
        randomset = SystemRandom().sample(self.set_of_Rs, c // 2 + 1)
        xor_val = 0
        for R in randomset:
            xor_val = xor_val ^ int(R, 2)
        self.epoch_randomness = ("{:0" + str(r) + "b}").format(xor_val)
        return ("{:0" + str(r) + "b}").format(xor_val), randomset
Exemple #25
0
    def _updateDiskCrypto(self):
        # If passphrase is empty, create one.
        if self.passphrase == "":
            self.passphrase = "".join(
                SystemRandom().choice(string.ascii_letters + string.digits +
                                      string.punctuation) for _ in range(20))

        self._auto_part_observer.proxy.SetPassphrase(self.passphrase)
        self.storage.encryption_passphrase = self._auto_part_observer.proxy.Passphrase
        if self.storage.encrypted_autopart is False:
            self.storage.encrypted_autopart = True
Exemple #26
0
    def get_pool(self) -> List[onionrtypes.BlockHash]:
        """Get the hash pool in secure random order."""
        if len(self._pool) != self._pool_size:
            raise PoolNotReady

        final_pool: List[onionrtypes.BlockHash] = list(self._pool)
        SystemRandom().shuffle(final_pool)

        self._pool.clear()
        self.birthday = onionrutils.epoch.get_epoch()
        return final_pool
Exemple #27
0
    def reset_piv(self):
        '''Resets YubiKey PIV app and generates new key for GAM to use.'''
        reply = str(
            input(
                'This will wipe all PIV keys and configuration from your YubiKey. Are you sure? (y/N) '
            ).lower().strip())
        if reply != 'y':
            sys.exit(1)
        try:
            conn = self._connect()
            with conn:
                piv = PivSession(conn)
                piv.reset()
                rnd = SystemRandom()
                pin_puk_chars = string.ascii_letters + string.digits + string.punctuation
                new_puk = ''.join(rnd.choice(pin_puk_chars) for _ in range(8))
                new_pin = ''.join(rnd.choice(pin_puk_chars) for _ in range(8))
                piv.change_puk('12345678', new_puk)
                piv.change_pin('123456', new_pin)
                print(f'PIN set to:  {new_pin}')
                piv.authenticate(MANAGEMENT_KEY_TYPE.TDES,
                                 DEFAULT_MANAGEMENT_KEY)

                piv.verify_pin(new_pin)
                print('YubiKey is generating a non-exportable private key...')
                pubkey = piv.generate_key(SLOT.AUTHENTICATION,
                                          KEY_TYPE.RSA2048, PIN_POLICY.ALWAYS,
                                          TOUCH_POLICY.NEVER)
                now = datetime.datetime.utcnow()
                valid_to = now + datetime.timedelta(days=36500)
                subject = 'CN=GAM Created Key'
                piv.authenticate(MANAGEMENT_KEY_TYPE.TDES,
                                 DEFAULT_MANAGEMENT_KEY)
                piv.verify_pin(new_pin)
                cert = generate_self_signed_certificate(
                    piv, SLOT.AUTHENTICATION, pubkey, subject, now, valid_to)
                piv.put_certificate(SLOT.AUTHENTICATION, cert)
                piv.put_object(OBJECT_ID.CHUID, generate_chuid())
        except ValueError as err:
            controlflow.system_error_exit(8, f'YubiKey - {err}')
Exemple #28
0
    def fit(self, X_train, Y_train):
        # Initialize x and y
        X = np.empty(shape=(0, 0))
        Y = np.empty(shape=(0, 0))

        if isinstance(X_train, pd.DataFrame) and isinstance(
                Y_train, pd.core.series.Series):
            X = pd.DataFrame.to_numpy(X_train)
            Y = pd.DataFrame.to_numpy(Y_train)
        elif isinstance(X_train, np.ndarray) and isinstance(
                Y_train, np.ndarray):
            X = X_train
            Y = Y_train

        Y = Y.reshape((np.shape(Y)[0], 1))

        # Check for NaN in numpy arrays
        if np.any(np.isnan(X)) or np.any(np.isnan(Y)):
            raise ValueError(
                "There is a NaN value in either your X or Y array.")

        # Add bias to X (column of 1's)
        X = np.insert(X, 0, [1] * np.shape(X)[0], axis=1)

        # Initialize beta values and velocity values
        self.betas = np.zeros(shape=(np.shape(X)[1], 1))
        self.velocity = np.zeros(shape=(np.shape(X)[1], 1))
        self.r = np.zeros(shape=(np.shape(X)[1], 1))
        self.delta = np.full(shape=(np.shape(X)[1], 1), fill_value=10e-8)

        # Go through num_iterations epochs
        for epoch in range(0, self.num_iterations):
            new_betas = self.betas.copy()

            # Get random row of X and Y
            randI = SystemRandom().randint(0, np.shape(X)[0] - 1)

            # Calculate gradient
            g = self.__calc_grad(X, Y, randI)

            # Calculate r
            self.r += g * g

            # Calculate velocity
            self.velocity = (
                (self.learning_rate / np.sqrt(self.delta + self.r)) * g) * -1

            # Calculate gradient for each beta
            new_betas += self.velocity

            # Make these the updated values for beta matrix
            self.betas = new_betas.copy()
Exemple #29
0
    def get_random_password(self):
        length = len(self.__policy)
        value = ""
        if self.__policy.has_lowercase():
            value += ascii_lowercase
        if self.__policy.has_uppercase():
            value += ascii_uppercase
        if self.__policy.has_numbers():
            value += digits
        if self.__policy.has_special_chars():
            value += punctuation

        return "".join(SystemRandom().choice(value) for _ in range(length))
Exemple #30
0
def generate_seed():
    """Generate and return an int that when converted to bytes contains no nulls."""
    seed = 0

    while nulls_in_hex(seed) is True:
        seed = SystemRandom().randrange(0x0000FFFF,
                                        0xFFFFFFFF)  # generate a random seed
    if nulls_in_hex(
            seed) is False:  # Ensure that we're returning a seed without nulls
        return seed
    else:
        raise Exception(
            "generate_seed() tried to return a seed with null bytes")
Exemple #31
0
    def __init__(self, configuration):
        self.obs = None
        self.config = configuration
        self.history = pd.DataFrame(
            columns=["step", "action", "opponent_action"])
        self.history.set_index("step", inplace=True)
        self.history = self.history.astype({
            "action": np.int,
            "opponent_action": np.int
        })
        # How the game would have happened if we always chose the actions selected by our agent
        self.alternate_history = pd.DataFrame(
            columns=["step", "action", "opponent_action"])
        self.alternate_history.set_index("step", inplace=True)
        self.history = self.history.astype({
            "action": np.int,
            "opponent_action": np.int
        })

        self.step = 0
        self.score = 0
        self.random = SystemRandom()
Exemple #32
0
async def scan_single_search_page(job_title: str, page_num: int,
                                  session: ClientSession):
    # Scan search page for vacancy links.
    payload = {"q": f'"{job_title}"', "fdb": 7, "pn": page_num}
    for _ in range(10):
        try:
            async with session.get("https://www.simplyhired.com/search",
                                   params=payload) as resp:
                try:
                    html = await asyncio.shield(resp.text())
                    soup = BeautifulSoup(html, "html.parser")
                    all_vacancies = soup.find_all("a",
                                                  href=re.compile(r"/job/"))
                    # Extract valid links to vacancy pages and clean the tail.
                    links = {("https://www.simplyhired.com" +
                              vacancy["href"]).split("?")[0]
                             for vacancy in all_vacancies}
                    timeout = SystemRandom().uniform(1.0, 10.0)
                    await asyncio.sleep(timeout)
                    return links
                except AttributeError:
                    logger.warning(
                        f"🚨 AttributeError occurred while scanning: {resp.url}"
                    )
                    return None
                except ClientPayloadError:
                    logger.warning(
                        f"🚨 ClientPayloadError occurred while scanning: {resp.url}"
                    )
                    return None
                except asyncio.TimeoutError:
                    logger.warning(
                        f"🚨 TimeoutError occurred while scanning: {resp.url}"
                    )
                    await asyncio.sleep(60)
        except ClientConnectorError:
            logger.warning(
                "🚨 ClientConnectorError occurred while scanning simplyhired.com."
            )
            await asyncio.sleep(60)
        except ServerDisconnectedError:
            logger.warning(
                "🚨 ServerDisconnectedError occurred while scanning simplyhired.com."
            )
            await asyncio.sleep(60)
        except ClientOSError:
            logger.warning(
                "🚨 ClientOSError occurred while scanning simplyhired.com")
            await asyncio.sleep(60)
    return None
Exemple #33
0
def message_new(dynamodb, username, title, markdown, rights, student_readable):
    if rights != 'teacher' and rights != 'admin':
        return {
            'success': False,
            'error_code': 401,
            'error': 'Only teachers and administrators can post'
        }
    import time
    from secrets import SystemRandom
    try:
        dynamodb.put_item(TableName='messages',
                          Item={
                              'id': {
                                  'N':
                                  str(SystemRandom().randint(10000, 99999))
                              },
                              'title': {
                                  'S': title
                              },
                              'poster': {
                                  'S': username
                              },
                              'posttime': {
                                  'N': str(round(time.time() * 1000))
                              },
                              'markdown': {
                                  'S': markdown
                              },
                              'student': {
                                  'BOOL': student_readable
                              }
                          },
                          ConditionExpression='attribute_not_exists(id)')
        return {'success': True}
    except dynamodb.exceptions.ConditionalCheckFailedException:
        message_new(dynamodb, username, title, markdown, rights,
                    student_readable)
    except ClientError as e:
        return {
            'success': False,
            'error_code': 500,
            'error': e.response['Error']['Message']
        }