import hashlib import math import re import random import string from sys import getsizeof from binaryornot.check import is_binary_string def obfuscate_text(text: str) -> str: match_len = len(text) start_reveled_len = math.ceil(match_len / 8) end_reveled_len = match_len - (math.ceil(match_len / 8)) obfuscated = obfuscate_regex.sub("*", text) return f'{text[:start_reveled_len]}{obfuscated[start_reveled_len:end_reveled_len]}{text[end_reveled_len:]}' obfuscate_regex = re.compile(r"[^+\-\s]") def is_binary_content(content: str) -> bool: """Get the first 1024 chars and check if it's binary or not.""" chunk = content[:1024] chunk_bytes = convert_string_to_bytes(chunk) return is_binary_string(chunk_bytes) def get_content_size(content: str): return getsizeof(content) def convert_string_to_bytes(content: str): return bytes(content, 'utf-8') def hash_string_to_sha256(content: str): return hashlib.sha256(content.encode()).hexdigest() def generate_random_string(string_len: int): # letters, digits, and symbols characters = string.ascii_letters + string.digits + string.punctuation return ''.join(random.choice(characters) for _ in range(string_len)) def get_position_in_line(text: str, position: int) -> int: return position - text.rfind('\n', 0, position) - 1