Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def __init__(self, protected_stream_key, subcon):
super(UnprotectedStream, self).__init__(subcon)
self.protected_stream_key = protected_stream_key
elem.text = base64.b64encode(
cipher.encrypt(
elem.text.encode('utf-8')
)
)
elem.attrib['Protected'] = 'True'
return tree
class ARCFourVariantStream(UnprotectedStream):
def get_cipher(self, protected_stream_key):
raise Exception("ARCFourVariant not implemented")
# https://github.com/dlech/KeePass2.x/blob/97141c02733cd3abf8d4dce1187fa7959ded58a8/KeePassLib/Cryptography/CryptoRandomStream.cs#L115-L119
class Salsa20Stream(UnprotectedStream):
def get_cipher(self, protected_stream_key):
key = hashlib.sha256(protected_stream_key).digest()
return Salsa20.new(
key=key,
nonce=b'\xE8\x30\x09\x4B\x97\x20\x5D\x2A'
)
# https://github.com/dlech/KeePass2.x/blob/97141c02733cd3abf8d4dce1187fa7959ded58a8/KeePassLib/Cryptography/CryptoRandomStream.cs#L103-L111
class ChaCha20Stream(UnprotectedStream):
def get_cipher(self, protected_stream_key):
key_hash = hashlib.sha512(protected_stream_key).digest()
key = key_hash[:32]
nonce = key_hash[32:44]
return ChaCha20.new(
key=key,
def get_cipher(self, protected_stream_key):
raise Exception("ARCFourVariant not implemented")
# https://github.com/dlech/KeePass2.x/blob/97141c02733cd3abf8d4dce1187fa7959ded58a8/KeePassLib/Cryptography/CryptoRandomStream.cs#L115-L119
class Salsa20Stream(UnprotectedStream):
def get_cipher(self, protected_stream_key):
key = hashlib.sha256(protected_stream_key).digest()
return Salsa20.new(
key=key,
nonce=b'\xE8\x30\x09\x4B\x97\x20\x5D\x2A'
)
# https://github.com/dlech/KeePass2.x/blob/97141c02733cd3abf8d4dce1187fa7959ded58a8/KeePassLib/Cryptography/CryptoRandomStream.cs#L103-L111
class ChaCha20Stream(UnprotectedStream):
def get_cipher(self, protected_stream_key):
key_hash = hashlib.sha512(protected_stream_key).digest()
key = key_hash[:32]
nonce = key_hash[32:44]
return ChaCha20.new(
key=key,
nonce=nonce
)
def Unprotect(protected_stream_id, protected_stream_key, subcon):
"""Select stream cipher based on protected_stream_id"""
return Switch(
protected_stream_id,
{'arcfourvariant': ARCFourVariantStream(protected_stream_key, subcon),
return tree
def _encode(self, tree, con, path):
cipher = self.get_cipher(self.protected_stream_key(con))
for elem in tree.xpath(self.unprotected_xpath):
if elem.text is not None:
elem.text = base64.b64encode(
cipher.encrypt(
elem.text.encode('utf-8')
)
)
elem.attrib['Protected'] = 'True'
return tree
class ARCFourVariantStream(UnprotectedStream):
def get_cipher(self, protected_stream_key):
raise Exception("ARCFourVariant not implemented")
# https://github.com/dlech/KeePass2.x/blob/97141c02733cd3abf8d4dce1187fa7959ded58a8/KeePassLib/Cryptography/CryptoRandomStream.cs#L115-L119
class Salsa20Stream(UnprotectedStream):
def get_cipher(self, protected_stream_key):
key = hashlib.sha256(protected_stream_key).digest()
return Salsa20.new(
key=key,
nonce=b'\xE8\x30\x09\x4B\x97\x20\x5D\x2A'
)
# https://github.com/dlech/KeePass2.x/blob/97141c02733cd3abf8d4dce1187fa7959ded58a8/KeePassLib/Cryptography/CryptoRandomStream.cs#L103-L111
class ChaCha20Stream(UnprotectedStream):