Files
flees/code/utils/crypt.py

84 lines
1.8 KiB
Python

import random
import string
import base64
#~ from Crypto.Cipher import AES
import hashlib
#~ class Crypto:
#~ def __init__(self, secret):
#~ self.secret = add_pad(secret[0:16])
#~ self.cipher = AES.new(self.secret, AES.MODE_ECB)
#~ def encrypt(self, msg):
#~ return base64.urlsafe_b64encode(
#~ self.cipher.encrypt(
#~ add_pad(
#~ msg
#~ )
#~ )
#~ ).decode("utf-8")
#~ def decrypt(self, enc):
#~ return remove_pad(
#~ self.cipher.decrypt(
#~ base64.urlsafe_b64decode(
#~ enc
#~ )
#~ ).decode("utf-8")
#~ )
def add_pad(string):
""" Add spaces until length is multiple of 16 """
while len(string)%16 != 0:
string+=" "
return string
def get_direct_token(share, filename):
if not 'pass_hash' in share:
return None
return password_hash(
share['pass_hash'] + filename
)
def has_token(token, share):
if not 'tokens' in share:
return False
return token in share['tokens']
def password_hash(string, salt=""):
if type(string) == str:
string = string.encode("utf-8")
if type(salt) == str:
salt = salt.encode("utf-8")
return hashlib.sha1(
string+salt
).hexdigest()
def random_token():
chars = [random.choice(string.ascii_letters + string.digits) for n in range(30)]
token = "".join(chars)
return token
def random_expiring_hash():
codes = []
for i in range(3):
codes.append("".join([random.choice(string.ascii_letters + string.digits) for n in range(4)]))
ehash = "-".join(codes)
return ehash
def remove_pad(string):
""" Remove spaces from right """
return string.rstrip(" ")