Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
"https://stuvel.eu/python-rsa-doc/usage.html#working-with-big-files "
"for more information.",
DeprecationWarning, stacklevel=2)
if not isinstance(pub_key, key.PublicKey):
raise TypeError('Public key required, but got %r' % pub_key)
key_bytes = common.bit_size(pub_key.n) // 8
blocksize = key_bytes - 11 # keep space for PKCS#1 padding
# Write the version number to the VARBLOCK file
outfile.write(byte(varblock.VARBLOCK_VERSION))
# Encrypt and write each block
for block in varblock.yield_fixedblocks(infile, blocksize):
crypto = pkcs1.encrypt(block, pub_key)
varblock.write_varint(outfile, len(crypto))
outfile.write(crypto)
"""
warnings.warn("The 'rsa.bigfile.encrypt_bigfile' function was deprecated in Python-RSA version "
"3.4 due to security issues in the VARBLOCK format. See "
"https://stuvel.eu/python-rsa-doc/usage.html#working-with-big-files "
"for more information.",
DeprecationWarning, stacklevel=2)
if not isinstance(pub_key, key.PublicKey):
raise TypeError('Public key required, but got %r' % pub_key)
key_bytes = common.bit_size(pub_key.n) // 8
blocksize = key_bytes - 11 # keep space for PKCS#1 padding
# Write the version number to the VARBLOCK file
outfile.write(byte(varblock.VARBLOCK_VERSION))
# Encrypt and write each block
for block in varblock.yield_fixedblocks(infile, blocksize):
crypto = pkcs1.encrypt(block, pub_key)
varblock.write_varint(outfile, len(crypto))
outfile.write(crypto)
def test_private_to_public(self):
with cli_args('-i', self.priv_fname, '-o', 'test_private_to_public.pem'):
with captured_output():
rsa.util.private_to_public()
# Check that the key is indeed valid.
with open('test_private_to_public.pem', 'rb') as pemfile:
key = rsa.PublicKey.load_pkcs1(pemfile.read())
self.assertEqual(self.priv_key.n, key.n)
self.assertEqual(self.priv_key.e, key.e)
def test_sign_blob(self):
private_key_id, signature = self.credentials.sign_blob('Google')
self.assertEqual(self.private_key_id, private_key_id)
pub_key = rsa.PublicKey.load_pkcs1_openssl_pem(
datafile('publickey_openssl.pem'))
self.assertTrue(rsa.pkcs1.verify(b'Google', signature, pub_key))
with self.assertRaises(rsa.pkcs1.VerificationError):
rsa.pkcs1.verify(b'Orest', signature, pub_key)
with self.assertRaises(rsa.pkcs1.VerificationError):
rsa.pkcs1.verify(b'Google', b'bad signature', pub_key)
def test_encrypt_decrypt_bigfile(self):
# Expected block size + 11 bytes padding
pub_key, priv_key = rsa.newkeys((6 + 11) * 8)
# Encrypt the file
message = b('123456Sybren')
infile = BytesIO(message)
outfile = BytesIO()
bigfile.encrypt_bigfile(infile, outfile, pub_key)
# Test
crypto = outfile.getvalue()
cryptfile = BytesIO(crypto)
clearfile = BytesIO()
bigfile.decrypt_bigfile(cryptfile, clearfile, priv_key)
self.assertEquals(clearfile.getvalue(), message)
def decrypt_windows_password(self, instance_id):
password = ""
password_data = self._client. \
get_password_data(InstanceId=instance_id)['PasswordData']
if password_data:
password = base64.b64decode(password_data)
with open(self.get_ssh_key_path(AWS_SSH_KEY_NAME), 'r') \
as privkeyfile:
priv = rsa.PrivateKey.load_pkcs1(privkeyfile.read())
password = rsa.decrypt(password, priv).decode('utf-8')
return password
def test_correctness_against_base_implementation(self):
# Slow test.
values = [
1 << 512,
1 << 8192,
1 << 77,
]
for value in values:
self.assertEqual(bytes2int(int2bytes(value)),
value,
"Boom %d" % value)
def test_values(self):
self.assertEqual(byte_size(1 << 1023), 128)
self.assertEqual(byte_size((1 << 1024) - 1), 128)
self.assertEqual(byte_size(1 << 1024), 129)
self.assertEqual(byte_size(255), 1)
self.assertEqual(byte_size(256), 2)
self.assertEqual(byte_size(0xffff), 2)
self.assertEqual(byte_size(0xffffff), 3)
self.assertEqual(byte_size(0xffffffff), 4)
self.assertEqual(byte_size(0xffffffffff), 5)
self.assertEqual(byte_size(0xffffffffffff), 6)
self.assertEqual(byte_size(0xffffffffffffff), 7)
self.assertEqual(byte_size(0xffffffffffffffff), 8)
def test_values(self):
self.assertEqual(byte_size(1 << 1023), 128)
self.assertEqual(byte_size((1 << 1024) - 1), 128)
self.assertEqual(byte_size(1 << 1024), 129)
self.assertEqual(byte_size(255), 1)
self.assertEqual(byte_size(256), 2)
self.assertEqual(byte_size(0xffff), 2)
self.assertEqual(byte_size(0xffffff), 3)
self.assertEqual(byte_size(0xffffffff), 4)
self.assertEqual(byte_size(0xffffffffff), 5)
self.assertEqual(byte_size(0xffffffffffff), 6)
self.assertEqual(byte_size(0xffffffffffffff), 7)
self.assertEqual(byte_size(0xffffffffffffffff), 8)
def test_find_signature_hash(self):
"""Test happy flow of sign and find_signature_hash"""
message = b'je moeder'
signature = pkcs1.sign(message, self.priv, 'SHA-256')
self.assertEqual('SHA-256', pkcs1.find_signature_hash(signature, self.pub))