-
Notifications
You must be signed in to change notification settings - Fork 2
/
scrypt_test.py
140 lines (97 loc) · 4.55 KB
/
scrypt_test.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
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
#!/usr/bin/python3
import binascii
import getpass
import time
# pip3 install scrypt
import scrypt as scrypt_locally_installed
from hashlib import scrypt as scrypt_from_hashlib
#from Crypto.Protocol.KDF import scrypt as scrypt_from_Crypto
from Cryptodome.Protocol.KDF import scrypt as scrypt_from_pyCryptodome
from cryptography.hazmat.primitives.kdf import scrypt as scrypt_from_hazmat
from cryptography.hazmat.backends import default_backend
import importlib.util
spec = importlib.util.spec_from_file_location("scrypt", "/usr/lib/python3/dist-packages/scrypt.py")
scrypt_old = importlib.util.module_from_spec(spec)
spec.loader.exec_module(scrypt_old)
mypass = "password"
mysalt = "salt"
iterations = 1
memory = 1048576
#memory = 64*1024
mypass = mypass.encode()
mysalt = mysalt.encode()
#print ("Version of scrypt:", scrypt.__version__)
# ******************************************************************************************
digest=mypass
print ("**** Testing with Scrypt from scrypt...")
print ("File of locally installed scrypt:", scrypt_locally_installed.__file__)
# ******** Time **********
start = time.time()
for counter in range(iterations):
print ("Iteration %s from %s..." % (counter+1, iterations) )
digest = scrypt_locally_installed.hash(digest, mysalt, N = memory, r = 8, p = 1, buflen = 128)
# ******** Time **********
end = time.time()
print ("*** Time: ", end - start, "**********************************************")
print ("Digest in base64 format:", binascii.b2a_base64(digest).decode("utf-8"))
print ("\n\n")
# ******************************************************************************************
digest=mypass
print ("**** Testing with Scrypt from scrypt (old version)...")
print ("File of scrypt:", scrypt_old.__file__)
# ******** Time **********
start = time.time()
for counter in range(iterations):
print ("Iteration %s from %s..." % (counter+1, iterations) )
digest = scrypt_old.hash(digest, mysalt, N = memory, r = 8, p = 1, buflen = 128)
# ******** Time **********
end = time.time()
print ("*** Time: ", end - start, "**********************************************")
print ("Digest in base64 format:", binascii.b2a_base64(digest).decode("utf-8"))
print ("\n\n")
# ******************************************************************************************
digest=mypass
print ("**** Testing with Scrypt from Cryptodome...")
# ******** Time **********
start = time.time()
for counter in range(iterations):
print ("Iteration %s from %s..." % (counter+1, iterations) )
digest = scrypt_from_pyCryptodome(digest, mysalt, key_len=128, N=memory, r=8, p=1)
# ******** Time **********
end = time.time()
print ("*** Time: ", end - start, "**********************************************")
print ("Digest in base64 format:", binascii.b2a_base64(digest).decode("utf-8"))
print ("\n\n")
# ******************************************************************************************
# ******************************************************************************************
digest=mypass
print ("**** Testing with Scrypt from hashlib...")
# ******** Time **********
start = time.time()
for counter in range(iterations):
print ("Iteration %s from %s..." % (counter+1, iterations) )
digest = scrypt_from_hashlib(password=digest, salt=mysalt, n=memory, r=8, p=1, maxmem=2147483646, dklen=128)
#ValueError: maxmem must be positive and smaller than 2147483647
# ******** Time **********
end = time.time()
print ("*** Time: ", end - start, "**********************************************")
print ("Digest in base64 format:", binascii.b2a_base64(digest).decode("utf-8"))
print ("\n\n")
# ******************************************************************************************
# ******************************************************************************************
digest=mypass
print ("**** Testing with Scrypt from hazmat...")
# ******** Time **********
start = time.time()
for counter in range(iterations):
print ("Iteration %s from %s..." % (counter+1, iterations) )
# digest = scrypt_from_hashlib(password=digest, salt=mysalt, n=memory, r=8, p=1, maxmem=2147483646, dklen=128)
backend = default_backend()
kdf = scrypt_from_hazmat.Scrypt(salt=mysalt, length=128, n=memory, r=8, p=1, backend=backend)
digest = kdf.derive(digest)
# ******** Time **********
end = time.time()
print ("*** Time: ", end - start, "**********************************************")
print ("Digest in base64 format:", binascii.b2a_base64(digest).decode("utf-8"))
print ("\n\n")
# ******************************************************************************************