mirror of
https://github.com/unshackle-dl/unshackle.git
synced 2026-11-29 12:35:13 +00:00
fix(drm): keep the input when a cbcs, ClearKey, stripe or segment decrypt fails
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import contextlib
|
||||
from pathlib import Path
|
||||
|
||||
from Cryptodome.Cipher import Blowfish
|
||||
@@ -24,7 +25,10 @@ class BlowfishStripe:
|
||||
return Blowfish.new(self.key, Blowfish.MODE_CBC, iv=self.IV).decrypt(chunk) # nosec B304
|
||||
|
||||
def decrypt(self, path: Path) -> None:
|
||||
"""Decrypt the file to a new file that then replaces it. A short trailing chunk stays clear."""
|
||||
"""Decrypt the file to a new file that then replaces it. A short trailing chunk stays clear.
|
||||
|
||||
A failure keeps ``path`` as it is and removes the new file.
|
||||
"""
|
||||
if not path or not path.exists():
|
||||
raise ValueError("Tried to decrypt a file that does not exist.")
|
||||
log_event(
|
||||
@@ -35,12 +39,16 @@ class BlowfishStripe:
|
||||
file=path.name,
|
||||
)
|
||||
decrypted_path = path.with_suffix(f".decrypted{path.suffix}")
|
||||
with path.open("rb") as src, decrypted_path.open("wb") as dst:
|
||||
while stripe := src.read(self.CHUNK * 3):
|
||||
if len(stripe) >= self.CHUNK:
|
||||
stripe = self.decrypt_chunk(stripe[: self.CHUNK]) + stripe[self.CHUNK :]
|
||||
dst.write(stripe)
|
||||
decrypted_path.replace(path)
|
||||
try:
|
||||
with path.open("rb") as src, decrypted_path.open("wb") as dst:
|
||||
while stripe := src.read(self.CHUNK * 3):
|
||||
if len(stripe) >= self.CHUNK:
|
||||
stripe = self.decrypt_chunk(stripe[: self.CHUNK]) + stripe[self.CHUNK :]
|
||||
dst.write(stripe)
|
||||
decrypted_path.replace(path)
|
||||
finally:
|
||||
with contextlib.suppress(OSError):
|
||||
decrypted_path.unlink(missing_ok=True)
|
||||
|
||||
|
||||
__all__ = ("BlowfishStripe",)
|
||||
|
||||
Reference in New Issue
Block a user