Skip to content

Commit 4e4ffec

Browse files
committed
Refactoring. Using black code style
1 parent 449e1ea commit 4e4ffec

4 files changed

Lines changed: 50 additions & 50 deletions

File tree

Hash/available.py

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,29 @@
11
#!/usr/bin/env python3
22
# -*- coding: utf-8 -*-
33

4-
__author__ = 'ipetrash'
4+
__author__ = "ipetrash"
55

66

77
import hashlib
88
import sys
99

10-
if __name__ == '__main__':
11-
algorithms = list(hashlib.algorithms_available) # get list algorithms
12-
algorithms.sort()
13-
print("Algorithms available: %s" % ", ".join(algorithms))
14-
15-
text = input("Text: ")
16-
if not text:
17-
print("Empty text!")
18-
sys.exit(1)
19-
20-
alg_name = input("Name algorithm: ")
21-
if alg_name not in algorithms: # search in list
22-
print("Algorithm not found!")
23-
sys.exit(1)
24-
25-
alg = hashlib.new(alg_name) # create hash function from name
26-
alg.update(text.encode()) # set data in hash-function
27-
print("Result:")
28-
print(" hex: %s" % alg.hexdigest())
29-
print(" HEX: %s" % alg.hexdigest().upper())
10+
11+
algorithms = list(hashlib.algorithms_available) # get list algorithms
12+
algorithms.sort()
13+
print(f"Algorithms available: {', '.join(algorithms)}")
14+
15+
text = input("Text: ")
16+
if not text:
17+
print("Empty text!")
18+
sys.exit(1)
19+
20+
alg_name = input("Name algorithm: ")
21+
if alg_name not in algorithms: # search in list
22+
print("Algorithm not found!")
23+
sys.exit(1)
24+
25+
alg = hashlib.new(alg_name) # create hash function from name
26+
alg.update(text.encode()) # set data in hash-function
27+
print("Result:")
28+
print(f" hex: {alg.hexdigest()}")
29+
print(f" HEX: {alg.hexdigest().upper()}")

Hash/custom.py

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,21 @@
11
#!/usr/bin/env python3
22
# -*- coding: utf-8 -*-
33

4-
__author__ = 'ipetrash'
4+
__author__ = "ipetrash"
55

66

7+
import hashlib
78
from datetime import datetime
8-
import hashlib
99

1010

11-
if __name__ == '__main__':
12-
ts = datetime.today().timestamp()
13-
bts = str(ts).encode()
11+
ts = datetime.today().timestamp()
12+
bts = str(ts).encode()
1413

15-
# Long
16-
md5 = hashlib.md5()
17-
md5.update(bts)
14+
# Long
15+
md5 = hashlib.md5()
16+
md5.update(bts)
1817

19-
print(md5.hexdigest())
20-
21-
# Short
22-
print(hashlib.md5(bts).hexdigest())
18+
print(md5.hexdigest())
2319

20+
# Short
21+
print(hashlib.md5(bts).hexdigest())

Hash/encode_all_algo.py

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,22 @@
11
#!/usr/bin/env python3
22
# -*- coding: utf-8 -*-
33

4-
__author__ = 'ipetrash'
4+
__author__ = "ipetrash"
55

66

77
import hashlib
88

9-
if __name__ == '__main__':
10-
available = list(set(map(str.lower, hashlib.algorithms_available)))
11-
available.sort()
129

13-
text = 'hello world'
10+
available = list(set(map(str.lower, hashlib.algorithms_available)))
11+
available.sort()
1412

15-
for algo_name in available:
16-
try:
17-
alg = hashlib.new(algo_name) # create hash function from name
18-
alg.update(text.encode()) # set data in hash-function
19-
print("{}: {}".format(algo_name, alg.hexdigest().upper()))
13+
text = "hello world"
2014

21-
except ValueError:
22-
pass
15+
for algo_name in available:
16+
try:
17+
alg = hashlib.new(algo_name) # create hash function from name
18+
alg.update(text.encode()) # set data in hash-function
19+
print(f"{algo_name}: {alg.hexdigest().upper()}")
20+
21+
except ValueError:
22+
pass

Hash/to sha256.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
#!/usr/bin/env python3
22
# -*- coding: utf-8 -*-
33

4-
__author__ = 'ipetrash'
4+
__author__ = "ipetrash"
55

66

7-
def to_sha256(text):
8-
import hashlib
7+
import hashlib
8+
9+
10+
def to_sha256(text: str) -> str:
911
return hashlib.sha256(text.encode()).hexdigest()
1012

1113

12-
if __name__ == '__main__':
13-
print(to_sha256('Hello World!'))
14+
if __name__ == "__main__":
15+
print(to_sha256("Hello World!"))

0 commit comments

Comments
 (0)