-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutilsMethod.py
89 lines (76 loc) · 2.12 KB
/
utilsMethod.py
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
import hashlib
import os
import binascii
import random
import time
import sqlite3
from sqlite3.dbapi2 import Error
def createSession(session, user):
session['username'] = str(user)
def destroySession(session):
session.pop('username', None)
session.pop('rolID', None)
def Loggedin(session):
if 'username' in session:
return True
else:
return False
def userIDSession(session):
if 'username' in session:
return str(session['username'])
else:
return ""
def rolIDSession(session):
# Not logged = 0
# usuario = 1
# administrador = 2
# SuperAdministrador = 3
if 'rolID' in session:
return int(session['rolID'])
else:
return int(0)
def convertToPesosCOP(value):
ceros = ""
for x in range(3, 10, 1):
if ((x+1)==len(str(value))):
price = "$ "+'{price:,.0f}'.format(price=int(value)).replace(',','.')
return str(price)
def blobImageUpload(request, param):
if (str(param) in request.files):
f = request.files[str(param)]
byteFileImg = f.read(1)
hex_byteImg = ""
while byteFileImg:
byte_array = bytearray(byteFileImg)
hex_byteImg = hex_byteImg + str(byte_array.hex())
byteFileImg = f.read(1)
bytesArrays = bytes.fromhex(hex_byteImg)
return (sqlite3.Binary(bytesArrays))
def file2Bytes(f):
byteFileImg = f.read(1)
hex_byteImg = ""
while byteFileImg:
byte_array = bytearray(byteFileImg)
hex_byteImg = hex_byteImg + str(byte_array.hex())
byteFileImg = f.read(1)
return hex_byteImg
def generateSaltPasswd():
size = os.urandom(32) # 32 bits
salt = binascii.b2a_hex(size).decode('utf-8')
return str(salt)
def passwordSalt(passwd, salt):
password = passwd.encode('utf-8')
key = hashlib.pbkdf2_hmac('sha256', password, salt.encode(), 100000)
return str(binascii.b2a_hex(key),'utf-8')
def TokenCSRF():
random.seed(time.time_ns())
rand = random.random()
CSRF = hashlib.md5()
b = bytes(str(rand), 'utf-8')
CSRF.update(b)
return str(CSRF.hexdigest())
def checkTokenCSRF(token, tokenClient):
if str(token) == str(tokenClient):
return True
else:
return False