big rewrite of token system
This commit is contained in:
1
code/utils/__init__.py
Normal file
1
code/utils/__init__.py
Normal file
@@ -0,0 +1 @@
|
||||
|
||||
58
code/utils/crypt.py
Normal file
58
code/utils/crypt.py
Normal file
@@ -0,0 +1,58 @@
|
||||
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 password_hash(string):
|
||||
if type(string) == str:
|
||||
string = string.encode("utf-8")
|
||||
return hashlib.sha1(
|
||||
string
|
||||
).hexdigest()
|
||||
|
||||
|
||||
def remove_pad(string):
|
||||
""" Remove spaces from right """
|
||||
return string.rstrip(" ")
|
||||
|
||||
49
code/utils/utils.py
Normal file
49
code/utils/utils.py
Normal file
@@ -0,0 +1,49 @@
|
||||
import os
|
||||
import hashlib
|
||||
from datetime import datetime
|
||||
from flask import current_app as app
|
||||
|
||||
|
||||
def file_date_human(num):
|
||||
return datetime.fromtimestamp(
|
||||
num
|
||||
).strftime(app.config['DATE_FORMAT'])
|
||||
|
||||
|
||||
def file_stat(filename):
|
||||
s = os.stat(filename)
|
||||
return {
|
||||
'size': file_size_MB(s.st_size),
|
||||
'mtime': file_date_human(s.st_mtime),
|
||||
'name': os.path.basename(filename)
|
||||
}
|
||||
|
||||
|
||||
def file_size_human(num,HTML=True):
|
||||
space = ' ' if HTML else ' '
|
||||
for x in [space + 'B', 'KB', 'MB', 'GB', 'TB']:
|
||||
if num < 1024.0:
|
||||
if x == space + 'B':
|
||||
return "%d%s%s" % (num, space, x)
|
||||
return "%3.1f%s%s" % (num, space, x)
|
||||
num /= 1024.0
|
||||
|
||||
|
||||
def file_size_MB(num):
|
||||
return "{:,.2f}".format(num/(1024*1024))
|
||||
|
||||
|
||||
def get_folder_size(path):
|
||||
total_size = 0
|
||||
for dirpath, dirnames, filenames in os.walk(path):
|
||||
for f in filenames:
|
||||
fp = os.path.join(dirpath, f)
|
||||
total_size += os.path.getsize(fp)
|
||||
return total_size
|
||||
|
||||
|
||||
def get_or_none(key,d,none = None):
|
||||
if key in d:
|
||||
return d[key]
|
||||
else:
|
||||
return none
|
||||
Reference in New Issue
Block a user