Skip to content

Instantly share code, notes, and snippets.

@imakin
Last active February 14, 2025 14:17
Show Gist options
  • Save imakin/6eaf11d7b405647aab9a920df14a23c3 to your computer and use it in GitHub Desktop.
Save imakin/6eaf11d7b405647aab9a920df14a23c3 to your computer and use it in GitHub Desktop.
import os
import shutil
from sys import argv
import sys
from importlib import import_module, invalidate_caches
import time
from random import randrange
import datetime
def datetimestamp():
return datetime.datetime.now().strftime("%Y%m%d_%H%M%S")
def fresh_import(module_filename):
"""
trick to fresh reload every changes because in 3dsmax python, __cache__ is ignored and literally all imported script will never reloaded
use the return value as the module object
just like doing import module_name as mylib
example:
mylib = fresh_import('path/to/myfile.py')
mylib.method_from_myfile_py()
"""
if module_filename.endswith('.py'):
module_filename = module_filename[:-3]
random_name = f"{module_filename}_delete_{int(time.time())}{randrange(0,4000000)}"
shutil.copy(f"{module_filename}.py",f"{random_name}.py")
invalidate_caches()
print(f"freshly import {random_name}")
path = os.path.dirname(random_name)
sys.path.append(path)
modul = import_module(f"{os.path.split(random_name)[-1]}")
sys.path.pop()
try:
os.remove(random_name+'.py')
except Exception as e:
print(e)
return modul
# https://github.com/imakin/securelemakin/tree/main/reusable/asymetriclight
def modexp(base, exp, modulus, debug=False):
#Compute (base^exp) % modulus using the square-and-multiply algorithm
result = 1
while exp>0:
if exp%2==1: #odd number
result = (result*base) % modulus
if debug:print(result)
base = (base*base)%modulus
exp = exp>>1
if debug:print(exp)
return result
#pad and obscure 8bit value b into 16bit
def pad(b):
r1 = os.urandom(1)[0] & 0xf0
r2 = os.urandom(1)[0] & 0x0f
return ((r1<<8) | (b<<4) | r2)
#unpad and extract obscured data from 16bit n
def unpad(n):
return ( (0x0ff0 & n) >>4)
def asyml_enc(message,pub,n,hexstring=False,obscure=False):
def p(c):
if obscure:
return pad(c)
else:
return c
try:
if hexstring:
return [hex(modexp(p(ord(c)),pub,n)) for c in message]
return [str(modexp(p(ord(c)),pub,n)) for c in message]
except:
return [modexp(p(c),pub,n) for c in message]
def asyml_dec(chiper,sec,n,obscure=False):
def unp(c):
if obscure:
return unpad(c)
else:
return c
try:
if type(chiper[0])==str and chiper[0].startswith('0x'):
return [chr(unp(modexp(int(c,16),sec,n))) for c in chiper]
return [chr(unp(modexp(int(c),sec,n))) for c in chiper]
except:
return [unp(modexp(int(c),sec,n)) for c in chiper]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment