Skip to content

Instantly share code, notes, and snippets.

View MartinThoma's full-sized avatar

Martin Thoma MartinThoma

View GitHub Profile
alphabet = "".join(chr(ord("a") + i) for i in range(26))
def derive_key(sentence: str) -> str:
key = ""
sentence = sentence.lower()
for letter in sentence + "".join(alphabet):
if letter not in key:
key += letter
return key
def xor_hash(input_bytes: bytes) -> bytes:
hash_length = 16 # in bytes
# Get the first 16 bytes
if len(input_bytes) < hash_length:
return input_bytes + b"\0" * (hash_length - len(input_bytes))
hash_value = input_bytes[:hash_length]
# iterate over blocks of the defined length
remaining = input_bytes[hash_length:]
import hashlib
import itertools
import time
from typing import Tuple
def find_hash(start: str = "00") -> Tuple[str, int]:
nb_probed = 0
for probe in generate_random_str():
nb_probed += 1
import hashlib
from collections import defaultdict
from pathlib import Path
from typing import List
def find_duplicates(directory: Path) -> List[List[Path]]:
fingerprint2paths = defaultdict(list)
for path in directory.glob("**/*"):
if not path.is_file():
from typing import Literal, List
from dataclasses import dataclass
from enum import Enum
class EnumRoles(Enum):
REGISTERED = "Registered"
MODERATOR = "Moderator"
ADMIN = "ADMIN"
from typing import Literal, List
from pydantic import BaseModel, Field
class User(BaseModel):
name: str
roles: List[Literal["Registered", "Moderator", "Admin"]] = Field(
default_factory=list
)
mysql:
host: localhost
user: root
password: something
preprocessing_queue: # Line comments are available!
- name: preprocessing.scale_and_center
width: 32
height: 32
- preprocessing.dot_reduction
use_anonymous: true
import resource
import contextlib
@contextlib.contextmanager
def limit(resource_type, limit):
"""Temporarily limit a resource."""
soft_limit, hard_limit = resource.getrlimit(resource_type)
resource.setrlimit(resource_type, (limit, hard_limit)) # set soft limit
try:
from lxml import etree
xml = """
<!DOCTYPE XML [
<!ENTITY ee SYSTEM "file:///etc/passwd" >
]>
<root>&ee;</root>
"""
parser = etree.XMLParser()
doc = etree.fromstring(xml.encode(), parser)
from lxml import etree
xml = """<root>Hello World!</root>"""
parser = etree.XMLParser()
doc = etree.fromstring(xml.encode(), parser)
parsed_xml = etree.tostring(doc).decode("utf8")
print(parsed_xml)