Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
))
if not isinstance(data, byte_cls):
raise TypeError(pretty_message(
'''
data must be a byte string, not %s
''',
type_name(data)
))
if cipher != 'rc4' and not isinstance(iv, byte_cls):
raise TypeError(pretty_message(
'''
iv must be a byte string, not %s
''',
type_name(iv)
))
if cipher != 'rc4' and not padding:
# AES in CBC mode can be allowed with no padding if
# the data is an exact multiple of the key size
if not (cipher == 'aes' and padding is False and len(data) % len(key) == 0):
raise ValueError('padding must be specified')
if _backend == 'winlegacy':
return _advapi32_encrypt(cipher, key, data, iv, padding)
return _bcrypt_encrypt(cipher, key, data, iv, padding)
))
if iterations < 1:
raise ValueError(pretty_message(
'''
iterations must be greater than 0 - is %s
''',
repr(iterations)
))
if not isinstance(key_length, int_types):
raise TypeError(pretty_message(
'''
key_length must be an integer, not %s
''',
type_name(key_length)
))
if key_length < 1:
raise ValueError(pretty_message(
'''
key_length must be greater than 0 - is %s
''',
repr(key_length)
))
if hash_algorithm not in set(['md5', 'sha1', 'sha224', 'sha256', 'sha384', 'sha512']):
raise ValueError(pretty_message(
'''
hash_algorithm must be one of "md5", "sha1", "sha224", "sha256",
"sha384", "sha512", not %s
''',
"""
if not isinstance(key, byte_cls):
raise TypeError(pretty_message(
'''
key must be a byte string, not %s
''',
type_name(key)
))
if not isinstance(data, byte_cls):
raise TypeError(pretty_message(
'''
data must be a byte string, not %s
''',
type_name(data)
))
if cipher != 'rc4' and not isinstance(iv, byte_cls):
raise TypeError(pretty_message(
'''
iv must be a byte string, not %s
''',
type_name(iv)
))
if cipher != 'rc4' and not padding:
# AES in CBC mode can be allowed with no padding if
# the data is an exact multiple of the key size
if not (cipher == 'aes' and padding is False and len(data) % len(key) == 0):
raise ValueError('padding must be specified')
))
if not isinstance(data, byte_cls):
raise TypeError(pretty_message(
'''
data must be a byte string, not %s
''',
type_name(data)
))
if cipher != Security.kSecAttrKeyTypeRC4 and not isinstance(iv, byte_cls):
raise TypeError(pretty_message(
'''
iv must be a byte string, not %s
''',
type_name(iv)
))
if cipher != Security.kSecAttrKeyTypeRC4 and not padding:
raise ValueError('padding must be specified')
cf_dict = None
cf_key = None
cf_data = None
cf_iv = None
sec_key = None
sec_transform = None
try:
cf_dict = CFHelpers.cf_dictionary_from_pairs([(Security.kSecAttrKeyType, cipher)])
cf_key = CFHelpers.cf_data_from_bytes(key)
cf_data = CFHelpers.cf_data_from_bytes(data)
:raises:
ValueError - when any of the parameters contain an invalid value
TypeError - when any of the parameters are of the wrong type
OSError - when an error is returned by the OS crypto library
:return:
A byte string of the ciphertext
"""
if not isinstance(certificate_or_public_key, (Certificate, PublicKey)):
raise TypeError(pretty_message(
'''
certificate_or_public_key must be an instance of the Certificate or
PublicKey class, not %s
''',
type_name(certificate_or_public_key)
))
if not isinstance(data, byte_cls):
raise TypeError(pretty_message(
'''
data must be a byte string, not %s
''',
type_name(data)
))
if not isinstance(rsa_oaep_padding, bool):
raise TypeError(pretty_message(
'''
rsa_oaep_padding must be a bool, not %s
''',
type_name(rsa_oaep_padding)
"""
if not isinstance(key, byte_cls):
raise TypeError(pretty_message(
'''
key must be a byte string, not %s
''',
type_name(key)
))
if not isinstance(data, byte_cls):
raise TypeError(pretty_message(
'''
data must be a byte string, not %s
''',
type_name(data)
))
if cipher != Security.kSecAttrKeyTypeRC4 and not isinstance(iv, byte_cls):
raise TypeError(pretty_message(
'''
iv must be a byte string, not %s
''',
type_name(iv)
))
if cipher != Security.kSecAttrKeyTypeRC4 and not padding:
raise ValueError('padding must be specified')
cf_dict = None
cf_key = None
cf_data = None
socket.socket - when a non-TLS socket error occurs
oscrypto.errors.TLSError - when a TLS-related error occurs
ValueError - when any of the parameters contain an invalid value
TypeError - when any of the parameters are of the wrong type
OSError - when an error is returned by the OS crypto library
:return:
A byte string of the data read
"""
if not isinstance(max_length, int_types):
raise TypeError(pretty_message(
'''
max_length must be an integer, not %s
''',
type_name(max_length)
))
if self._context_handle_pointer is None:
# Allow the user to read any remaining decrypted data
if self._decrypted_bytes != b'':
output = self._decrypted_bytes[0:max_length]
self._decrypted_bytes = self._decrypted_bytes[max_length:]
return output
self._raise_closed()
# The first time read is called, set up a single contiguous buffer that
# it used by DecryptMessage() to populate the three output buffers.
# Since we are creating the buffer, we do not need to free it other
# than allowing Python to GC it once this object is GCed.
))
if not isinstance(signature, byte_cls):
raise TypeError(pretty_message(
'''
signature must be a byte string, not %s
''',
type_name(signature)
))
if not isinstance(data, byte_cls):
raise TypeError(pretty_message(
'''
data must be a byte string, not %s
''',
type_name(data)
))
valid_hash_algorithms = set(['md5', 'sha1', 'sha256', 'sha384', 'sha512'])
if certificate_or_public_key.algorithm == 'rsa' and not rsa_pss_padding:
valid_hash_algorithms |= set(['raw'])
if hash_algorithm not in valid_hash_algorithms:
valid_hash_algorithms_error = '"md5", "sha1", "sha256", "sha384", "sha512"'
if certificate_or_public_key.algorithm == 'rsa' and not rsa_pss_padding:
valid_hash_algorithms_error += ', "raw"'
raise ValueError(pretty_message(
'''
hash_algorithm must be one of %s, not %s
''',
valid_hash_algorithms_error,
repr(hash_algorithm)
if not isinstance(certificate_or_public_key, (Certificate, PublicKey)):
raise TypeError(pretty_message(
'''
certificate_or_public_key must be an instance of the Certificate or
PublicKey class, not %s
''',
type_name(certificate_or_public_key)
))
if not isinstance(data, byte_cls):
raise TypeError(pretty_message(
'''
data must be a byte string, not %s
''',
type_name(data)
))
if not isinstance(rsa_oaep_padding, bool):
raise TypeError(pretty_message(
'''
rsa_oaep_padding must be a bool, not %s
''',
type_name(rsa_oaep_padding)
))
if _backend == 'winlegacy':
return _advapi32_encrypt(certificate_or_public_key, data, rsa_oaep_padding)
return _bcrypt_encrypt(certificate_or_public_key, data, rsa_oaep_padding)