-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
82 lines (59 loc) · 1.73 KB
/
utils.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
import os
import re
import random
import hashlib
# import binascii
import datetime
import uuid
from rdoclient import RandomOrgClient
import config
# get time in format I like
def get_timestamp():
"""
Function to generate timestamp for use in application
:return timestamp:
"""
dt = datetime.datetime.now()
return dt.strftime("%Y-%m-%d %X")
def gen_uid(length=10):
"""
Function to generate random uuid of varying length for application
:param length: length of uid
:return uid: formatted string
"""
# TODO - find one that works in both v2.x/3.x...
# python 3.x version
uid = uuid.uuid4()
tmp_uid = re.sub('-', '', str(uid))
return ''.join(random.sample(list(tmp_uid), length))
def hash_password(password, salt_length=16, iterations=1000000, encoding='utf-8'):
"""
Function to securely hash password with variable salt and iterations
:param password: input secret
:param salt_length: length of salt
:param iterations: number of times to cycle this algorithm
:param encoding: character encoding
:return: hashed password
"""
salt = os.urandom(salt_length)
hashed_password = hashlib.pbkdf2_hmac(
hash_name='sha256',
password=bytes(password, encoding),
salt=salt,
iterations=iterations,
)
# Non-bytes version
# return binascii.hexlify(hashed_password)
# Bytes version
return hashed_password
def get_roc(api_key=config.API_KEY):
"""
Get instance of RandomOrgClient for testing.
:param api_key: API key to fetch API client
:return: instance of ROC
"""
try:
roc = RandomOrgClient(api_key)
return roc
except (ValueError, AttributeError) as e:
print(e)