-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsecure_hash.py
More file actions
123 lines (97 loc) · 2.1 KB
/
secure_hash.py
File metadata and controls
123 lines (97 loc) · 2.1 KB
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# secure_hash.py
"""
This modules handles the authentication and
the creation / deletion of users
within the dynamodb. All fields are
encrypted with private keys depending on the
type of hash function used
"""
import hashlib
import boto3
import random
import string
def totalHash(field1, field2):
"""
Larger hash
"""
return hashlib.sha256(
(simpleHash(field1)+simpleHash(field2)+simpleHash('state')) \
.encode('utf-8')).hexdigest()
def simpleHash(field):
"""
Simple one field hash
"""
return hashlib.sha256((field).encode('utf-8')).hexdigest()
def authenticate(field1, field2):
"""
Prompted a username and password, ensure that
the user has made an account and the login information
is correct
"""
salt = ['3.2..400zx','34ff4ff']
client = boto3.client('dynamodb')
response = client.get_item(
TableName='userFields',
Key={
'Key':{
'S':totalHash(field1, (salt[0] + field2))
}
}
)
return response
def createUser(field1, field2):
"""
Create a new user and add it to the
usersdb
"""
salt = ['3.2..400zx','34ff4ff']
client = boto3.client('dynamodb')
response = client.put_item(
TableName='userFields',
Item={
'Key':{
'S':totalHash(field1, (salt[0]+ field2))
},
'Username':{
'S': totalHash(field1, salt[1])
},
'Password':{
'S': ''.join(random.choices(string.ascii_uppercase +
string.digits, k=25))
}
}
)
return response
def deleteUser(field1, field2):
"""
Delete the user from the db
"""
salt = '3.2..400zx'
client = boto3.client('dynamodb')
response = client.delete_item(
TableName='userFields',
Key={
'Key':{
'S': totalHash(field1, (salt + field2))
}
}
)
return response
def createUser_hander(event, context):
"""
AWS Lambda handler for to create
user within database
"""
field1 = event['field1']
field2 = event['field2']
response = createUser(field1, field2)
return {'Response':response}
def login_handler(event, context):
"""
AWSLambda handler for authenticate
function
"""
field1 = event['field1']
field2 = event['field2']
response = authenticate(field1, field2)
return {'Response': response}