def test_codec_identity(self): # Not zero-destructive. self.assertEqual(base64_decode(base64_encode(zero_bytes)), zero_bytes) self.assertEqual(base64_decode(base64_encode(random_bytes_1024)), random_bytes_1024) self.assertEqual(base64_decode(base64_encode(random_bytes_2048)), random_bytes_2048) self.assertEqual(base64_decode(base64_encode(random_bytes_len_4093)), random_bytes_len_4093)
def test_codec_identity(self): # Not zero-destructive. self.assertEqual(codec.base64_decode(codec.base64_encode(ZERO_BYTES)), ZERO_BYTES) self.assertEqual(codec.base64_decode(codec.base64_encode(RANDOM_BYTES_1024)), RANDOM_BYTES_1024) self.assertEqual(codec.base64_decode(codec.base64_encode(RANDOM_BYTES_2048)), RANDOM_BYTES_2048) self.assertEqual(codec.base64_decode(codec.base64_encode(RANDOM_BYTES_4093)), RANDOM_BYTES_4093)
def md5_base64_digest(*inputs): """ Calculates Base-64-encoded MD5 digest of a variable number of inputs. :param inputs: A variable number of inputs for which the digest will be calculated. :returns: Base-64-encoded MD5 digest. """ return codec.base64_encode(md5_digest(*inputs))
def sha1_base64_digest(*inputs): """ Calculates Base-64-encoded SHA-1 digest of a variable number of inputs. :param inputs: A variable number of inputs for which the digest will be calculated. :returns: Base-64-encoded SHA-1 digest. """ return codec.base64_encode(sha1_digest(*inputs))
def sha1_base64_digest(*inputs): """ Calculates Base-64-encoded SHA-1 digest of a variable number of inputs. :param inputs: A variable number of inputs for which the digest will be calculated. :returns: Base-64-encoded SHA-1 digest. """ return base64_encode(sha1_digest(*inputs))
def hmac_sha1_base64_digest(key, data): """ Calculates a base64-encoded HMAC SHA-1 signature. :param key: The key for the signature. :param data: The data to be signed. :returns: Base64-encoded HMAC SHA-1 signature. """ return codec.base64_encode(hmac_sha1_digest(key, data))
def hmac_sha1_base64_digest(key, data): """ Calculates a base64-encoded HMAC SHA-1 signature. :param key: The key for the signature. :param data: The data to be signed. :returns: Base64-encoded HMAC SHA-1 signature. """ return base64_encode(hmac_sha1_digest(key, data))
def test_correctness(self): self.assertEqual(base64_urlsafe_encode(url_safety_test_bytes), url_safety_test_safe_encoded) self.assertEqual(base64_encode(url_safety_test_bytes), url_safety_test_standard_encoded) self.assertEqual(base64_urlsafe_decode(url_safety_test_safe_encoded), url_safety_test_bytes) self.assertEqual(base64_decode(url_safety_test_standard_encoded), url_safety_test_bytes) # Tests whether this decoder can decode standard encoded base64 # representation too. self.assertEqual( base64_urlsafe_decode(url_safety_test_standard_encoded), url_safety_test_bytes )
def test_correctness(self): self.assertEqual(codec.base64_urlsafe_encode(URL_SAFETY_TEST_BYTES), URL_SAFETY_TEST_SAFE_ENCODED) self.assertEqual(codec.base64_encode(URL_SAFETY_TEST_BYTES), URL_SAFETY_TEST_STANDARD_ENCODED) self.assertEqual(codec.base64_urlsafe_decode(URL_SAFETY_TEST_SAFE_ENCODED), URL_SAFETY_TEST_BYTES) self.assertEqual(codec.base64_decode(URL_SAFETY_TEST_STANDARD_ENCODED), URL_SAFETY_TEST_BYTES) # Tests whether this decoder can decode standard encoded base64 # representation too. self.assertEqual( codec.base64_urlsafe_decode(URL_SAFETY_TEST_STANDARD_ENCODED), URL_SAFETY_TEST_BYTES )
def generate_rsa_sha1_signature(base_string, client_private_key, *args, **kwargs): """ Calculates an RSA-SHA1 OAuth signature. :see: RSA-SHA1 (http://tools.ietf.org/html/rfc5849#section-3.4.3) :param base_string: Base string. :param client_private_key: PEM-encoded RSA private key. :returns: RSA-SHA1 signature. """ from mom.security.rsa import parse_private_key key = parse_private_key(client_private_key) return base64_encode(key.pkcs1_v1_5_sign(sha1_digest(base_string)))
def der_to_pem(der_cert_bytes, pem_header, pem_footer): """ Takes a certificate in binary DER format and returns the PEM version of it as a string. Taken from the Python SSL module. :param der_cert_bytes: A byte string of the DER. :param pem_header: The PEM header to use. :param pem_footer: The PEM footer to use. """ # Does what base64.b64encode without the `altchars` argument does. import textwrap encoded = codec.base64_encode(der_cert_bytes) return (pem_header + "\n" + textwrap.fill(encoded, 64) + "\n" + pem_footer + "\n")
def der_to_pem(der_cert_bytes, pem_header, pem_footer): """ Takes a certificate in binary DER format and returns the PEM version of it as a string. Taken from the Python SSL module. :param der_cert_bytes: A byte string of the DER. :param pem_header: The PEM header to use. :param pem_footer: The PEM footer to use. """ # Does what base64.b64encode without the `altchars` argument does. import textwrap encoded = base64_encode(der_cert_bytes) return (pem_header + '\n' + textwrap.fill(encoded, 64) + '\n' + pem_footer + '\n')
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_value(self): self.assertEqual(hash.sha1_base64_digest(*INPUTS), codec.base64_encode(INPUT_SHA1_DIGEST))
def test_value(self): self.assertEqual(md5_base64_digest(*inputs), base64_encode(input_md5_digest))
def test_encodes_without_trailing_newline(self): self.assertFalse(codec.base64_encode(ZERO_BYTES).endswith(b("\n"))) self.assertFalse(codec.base64_encode(RANDOM_BYTES_1024).endswith(b("\n"))) self.assertFalse(codec.base64_encode(RANDOM_BYTES_2048).endswith(b("\n"))) self.assertFalse(codec.base64_encode(RANDOM_BYTES_4093).endswith( b("\n")))
def test_encodes_without_trailing_newline(self): self.assertFalse(base64_encode(zero_bytes).endswith(b("\n"))) self.assertFalse(base64_encode(random_bytes_1024).endswith(b("\n"))) self.assertFalse(base64_encode(random_bytes_2048).endswith(b("\n"))) self.assertFalse(base64_encode(random_bytes_len_4093).endswith( b("\n")))
def test_value(self): self.assertEqual(hash.md5_base64_digest(*INPUTS), codec.base64_encode(INPUT_MD5_DIGEST))