def generate_verification_code(n_bits=64): """ Compact human-inputable strong verification code. :param n_bits: Bit size. 64 is default. :returns: A base58-encoded random unsigned integral human-inputable compact verification code. """ return base58_encode(generate_random_bits(n_bits))
def generate_nonce(n_bits=64): """ Generates a random ASCII-encoded unsigned integral number in decimal representation. The nonce/timestamp pair should always be unique to prevent replay attacks. :see: Nonce and Timestamp (http://tools.ietf.org/html/rfc5849#section-3.3) :param n_bits: Bit size. Default 64. :returns: A string representation of a randomly-generated ASCII-encoded decimal-representation unsigned integral number based on the bit size specified. """ return decimal_encode(generate_random_bits(n_bits))
def generate_client_secret(n_bits=144): """ Generates a random Base-64-encoded client secret to assign to a registered client application. Consumer secrets are base64-encoded but not URL-safe. This is done to force the user into properly percent-encoding the secrets before generating OAuth signatures for base strings. As a side note, Google OAuth generates client secrets with similar encoding and the above problem is a little hard to trace for the client, but it is a problem that should be fixed by the client after all. OAuth protocol version 1.0a requires percent-encoding the secrets before generating signatures. (See, :func:``generate_plaintext_signature``). :param n_bits: Bit size. 144 is default. :returns: A base-64-encoded random unsigned-integral consumer secret based on the bit size specified. """ return base64_encode(generate_random_bits(n_bits))
def test_uniqueness(self): # The likely-hood of recurrence should be tiny if a large enough # bit size is chosen. self.assertNotEqual(random.generate_random_bits(64), random.generate_random_bits(64))
def test_range(self): for _ in range(999): n_bits = 4 value = integer.bytes_to_uint(random.generate_random_bits(n_bits)) self.assertTrue(value >= 0 and value < (1 << n_bits))