From 640acb3f86aa39177f050a262adf7db95d860125 Mon Sep 17 00:00:00 2001 From: KnugiHK <24708955+KnugiHK@users.noreply.github.com> Date: Sat, 25 Mar 2023 18:33:22 +0800 Subject: [PATCH] Move some utility functions to a separated python file --- Whatsapp_Chat_Exporter/extract.py | 37 ++------------------ Whatsapp_Chat_Exporter/extract_iphone.py | 18 +--------- Whatsapp_Chat_Exporter/utility.py | 44 ++++++++++++++++++++++++ 3 files changed, 47 insertions(+), 52 deletions(-) create mode 100644 Whatsapp_Chat_Exporter/utility.py diff --git a/Whatsapp_Chat_Exporter/extract.py b/Whatsapp_Chat_Exporter/extract.py index 3c1b9f8..a565326 100644 --- a/Whatsapp_Chat_Exporter/extract.py +++ b/Whatsapp_Chat_Exporter/extract.py @@ -9,13 +9,11 @@ import re import io import hmac from pathlib import Path -from bleach import clean as sanitize -from markupsafe import Markup -from datetime import datetime -from enum import Enum from mimetypes import MimeTypes from hashlib import sha256 from Whatsapp_Chat_Exporter.data_model import ChatStore, Message +from Whatsapp_Chat_Exporter.utility import sanitize_except, determine_day, Crypt +from Whatsapp_Chat_Exporter.utility import brute_force_offset, CRYPT14_OFFSETS try: import zlib @@ -31,37 +29,6 @@ except ModuleNotFoundError: else: support_crypt15 = True -def sanitize_except(html): - return Markup(sanitize(html, tags=["br"])) - - -def determine_day(last, current): - last = datetime.fromtimestamp(last).date() - current = datetime.fromtimestamp(current).date() - if last == current: - return None - else: - return current - -CRYPT14_OFFSETS = ( - {"iv": 67, "db": 191}, - {"iv": 67, "db": 190}, - {"iv": 66, "db": 99}, - {"iv": 67, "db": 193} -) - - -class Crypt(Enum): - CRYPT15 = 15 - CRYPT14 = 14 - CRYPT12 = 12 - - -def brute_force_offset(): - for iv in range(0, 200): - for db in range(0, 200): - yield iv, iv + 16, db - def _generate_hmac_of_hmac(key_stream): key = hmac.new( diff --git a/Whatsapp_Chat_Exporter/extract_iphone.py b/Whatsapp_Chat_Exporter/extract_iphone.py index ce1fbd6..89914f0 100644 --- a/Whatsapp_Chat_Exporter/extract_iphone.py +++ b/Whatsapp_Chat_Exporter/extract_iphone.py @@ -6,25 +6,9 @@ import jinja2 import os import shutil from pathlib import Path -from bleach import clean as sanitize -from markupsafe import Markup from datetime import datetime from mimetypes import MimeTypes - -APPLE_TIME = datetime.timestamp(datetime(2001, 1, 1)) - - -def sanitize_except(html): - return Markup(sanitize(html, tags=["br"])) - - -def determine_day(last, current): - last = datetime.fromtimestamp(last).date() - current = datetime.fromtimestamp(current).date() - if last == current: - return None - else: - return current +from Whatsapp_Chat_Exporter.utility import sanitize_except, determine_day, APPLE_TIME def messages(db, data): diff --git a/Whatsapp_Chat_Exporter/utility.py b/Whatsapp_Chat_Exporter/utility.py new file mode 100644 index 0000000..6adcfde --- /dev/null +++ b/Whatsapp_Chat_Exporter/utility.py @@ -0,0 +1,44 @@ +from bleach import clean as sanitize +from markupsafe import Markup +from datetime import datetime +from enum import Enum + + +def sanitize_except(html): + return Markup(sanitize(html, tags=["br"])) + + +def determine_day(last, current): + last = datetime.fromtimestamp(last).date() + current = datetime.fromtimestamp(current).date() + if last == current: + return None + else: + return current + + +# Android Specific + +CRYPT14_OFFSETS = ( + {"iv": 67, "db": 191}, + {"iv": 67, "db": 190}, + {"iv": 66, "db": 99}, + {"iv": 67, "db": 193} +) + + +class Crypt(Enum): + CRYPT15 = 15 + CRYPT14 = 14 + CRYPT12 = 12 + + +def brute_force_offset(max_iv=200, max_db=200): + for iv in range(0, max_iv): + for db in range(0, max_db): + yield iv, iv + 16, db + + +# iOS Specific + +APPLE_TIME = datetime.timestamp(datetime(2001, 1, 1))