Skip to content

Commit 20e523c

Browse files
committed
added hashing algorithms tutorial
1 parent f7cf279 commit 20e523c

File tree

5 files changed

+80
-0
lines changed

5 files changed

+80
-0
lines changed

Diff for: README.md

+1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ This is a repository of all the tutorials of [The Python Code](https://www.thepy
2121
- [How to Use Steganography to Hide Secret Data in Images in Python](https://www.thepythoncode.com/article/hide-secret-data-in-images-using-steganography-python). ([code](ethical-hacking/steganography))
2222
- [How to Brute-Force SSH Servers in Python](https://www.thepythoncode.com/article/brute-force-ssh-servers-using-paramiko-in-python). ([code](ethical-hacking/bruteforce-ssh))
2323
- [How to Build a XSS Vulnerability Scanner in Python](https://www.thepythoncode.com/article/make-a-xss-vulnerability-scanner-in-python). ([code](ethical-hacking/xss-vulnerability-scanner))
24+
- [How to Use Hash Algorithms in Python using hashlib](https://www.thepythoncode.com/article/hashing-functions-in-python-using-hashlib). ([code](ethical-hacking/hashing-functions/))
2425

2526
- ### [Machine Learning](https://www.thepythoncode.com/topic/machine-learning)
2627
- ### [Natural Language Processing](https://www.thepythoncode.com/topic/nlp)

Diff for: ethical-hacking/hashing-functions/README.md

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# [How to Use Hash Algorithms in Python using hashlib](https://www.thepythoncode.com/article/hashing-functions-in-python-using-hashlib)
2+
To hash a checksum of the file `foo.pdf`:
3+
4+
python hashing_files.py foo.pdf
5+
6+
**Outputs:**
7+
8+
MD5: 49260116db5c6dab4cda627fbc6e6455
9+
SHA-256: 7d6c24e45b38375e322419281cee1335ddb97e00fc86abe9f22afcd8b42e6d82
10+
SHA-512: dd7bdd49300fc2aa76babb4afada70621f35235ff472342fb6efef7150b840ad06c1e081fa20469fc4801139939ce18a3c891f4ca15a45e31d487160b2e6f22e
11+
SHA-3-256: 975ef59e80d7eefa5068f7876fa63139436f47582cc7c29d3540c85d241e16be
12+
SHA-3-512: 07b8ee04cd2ac390ac6fc4f1598083347ae001d4cc8ddf6e3e0ef35eb7af254232ab70d8105f5b11af704770de81a15f7895499ee8a86a2479352627dac0b594
13+
BLAKE2c: 536245433930603620f007577d19b6d993b5dd75267e013c8965e73921db1f81
14+
BLAKE2b: 726bfdeb5fed358ce9827ca013e2dc0d7c34ba1d1900ddb5f46b5dd29d46c6e7bd87b610ea8e290765c3aac4d44ddea01d14e5073b8c743c67cd1da1ecdcef01
15+

Diff for: ethical-hacking/hashing-functions/foo.pdf

82.2 KB
Binary file not shown.

Diff for: ethical-hacking/hashing-functions/hashing_files.py

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import hashlib
2+
import sys
3+
4+
def read_file(file):
5+
"""Reads en entire file and returns file bytes."""
6+
BUFFER_SIZE = 16384 # 16 kilo bytes
7+
b = b""
8+
with open(file, "rb") as f:
9+
while True:
10+
# read 16K bytes from the file
11+
bytes_read = f.read(BUFFER_SIZE)
12+
if bytes_read:
13+
# if there is bytes, append them
14+
b += bytes_read
15+
else:
16+
# if not, nothing to do here, break out of the loop
17+
break
18+
return b
19+
20+
if __name__ == "__main__":
21+
# read some file
22+
file_content = read_file(sys.argv[1])
23+
# some chksums:
24+
# hash with MD5 (not recommended)
25+
print("MD5:", hashlib.md5(file_content).hexdigest())
26+
27+
# hash with SHA-2 (SHA-256 & SHA-512)
28+
print("SHA-256:", hashlib.sha256(file_content).hexdigest())
29+
30+
print("SHA-512:", hashlib.sha512(file_content).hexdigest())
31+
32+
# hash with SHA-3
33+
print("SHA-3-256:", hashlib.sha3_256(file_content).hexdigest())
34+
35+
print("SHA-3-512:", hashlib.sha3_512(file_content).hexdigest())
36+
37+
# hash with BLAKE2
38+
# 256-bit BLAKE2 (or BLAKE2s)
39+
print("BLAKE2c:", hashlib.blake2s(file_content).hexdigest())
40+
# 512-bit BLAKE2 (or BLAKE2b)
41+
print("BLAKE2b:", hashlib.blake2b(file_content).hexdigest())

Diff for: ethical-hacking/hashing-functions/simple_hashing.py

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import hashlib
2+
3+
# encode it to bytes using UTF-8 encoding
4+
message = "Some text to hash".encode()
5+
6+
# hash with MD5 (not recommended)
7+
print("MD5:", hashlib.md5(message).hexdigest())
8+
9+
# hash with SHA-2 (SHA-256 & SHA-512)
10+
print("SHA-256:", hashlib.sha256(message).hexdigest())
11+
12+
print("SHA-512:", hashlib.sha512(message).hexdigest())
13+
14+
# hash with SHA-3
15+
print("SHA-3-256:", hashlib.sha3_256(message).hexdigest())
16+
17+
print("SHA-3-512:", hashlib.sha3_512(message).hexdigest())
18+
19+
# hash with BLAKE2
20+
# 256-bit BLAKE2 (or BLAKE2s)
21+
print("BLAKE2c:", hashlib.blake2s(message).hexdigest())
22+
# 512-bit BLAKE2 (or BLAKE2b)
23+
print("BLAKE2b:", hashlib.blake2b(message).hexdigest())

0 commit comments

Comments
 (0)