forked from cycodehq/cycode-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstring_utils.py
More file actions
49 lines (31 loc) · 1.31 KB
/
Copy pathstring_utils.py
File metadata and controls
49 lines (31 loc) · 1.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
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