added homework files
This commit is contained in:
parent
cea43dda51
commit
dca5b156c7
@ -0,0 +1,66 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Block Integrity -> Collision: Tutorial 1.1
|
||||
|
||||
The goal of this tutorial is to learn how a cryptographic hash function works.
|
||||
In addition, how a collision can occur. A hash function takes a data input and returns a fixed length of bits.
|
||||
Be aware that some hash algorithms are now known for their hash collision weaknesses.
|
||||
In this tutorial you will be able to observe collisions using blake2b digest algorithm.
|
||||
This algorithm can have different features depend on the module your are using, such as:
|
||||
* salting,
|
||||
* personalization, and
|
||||
* tree hashing
|
||||
|
||||
In tutorials 1 and 2 you will work with different modules like hashlib and pyblake2.
|
||||
Both modules contain blake2b digest algorithm.
|
||||
Changing the size of the resulting digest in byte of the blake2b algorithm will change the chance of a collision.
|
||||
|
||||
Your task is to:
|
||||
* locate the TODOs in this file
|
||||
* run this tutorial and observe the output
|
||||
|
||||
To test run 'Collision.py' in your command line
|
||||
|
||||
Notes:
|
||||
* do not change class structure or method signature to not break unit tests
|
||||
* visit these urls for more information on this topic:
|
||||
https://docs.python.org/3/library/hashlib.html
|
||||
https://pythonhosted.org/pyblake2/module.html#pyblake2.blake2b
|
||||
"""
|
||||
from pyblake2 import blake2b
|
||||
|
||||
# TODO 1: Read the code and understand each statement
|
||||
# TODO 2: Run the code and observe the output
|
||||
# TODO 3: Change or increment the size of the resulting digest in byte
|
||||
# TODO 4: Re-run this program after changing the size
|
||||
hash_size_in_bytes = 1
|
||||
|
||||
path = 'files'
|
||||
names_list = []
|
||||
with open(path + '/names.txt', 'r') as f_names:
|
||||
names_list = [name.lower() for name in f_names.read().splitlines()]
|
||||
|
||||
print(len(names_list))
|
||||
|
||||
myname = input('please enter your name: ').lower()
|
||||
digest = blake2b(digest_size = hash_size_in_bytes)
|
||||
|
||||
digest.update(bytes(myname, 'utf-8'))
|
||||
hash_of_myname = digest.hexdigest()
|
||||
print(hash_of_myname)
|
||||
|
||||
found = False
|
||||
counter = 0
|
||||
for name in names_list:
|
||||
digest = blake2b(digest_size = hash_size_in_bytes)
|
||||
digest.update(bytes(name, 'utf-8'))
|
||||
h = digest.hexdigest()
|
||||
|
||||
if h == hash_of_myname and name != myname:
|
||||
print(f'hash of "{name.lower()}" has collision with your name "{myname}": hash = {h}')
|
||||
found = True
|
||||
counter += 1
|
||||
|
||||
if found:
|
||||
print(f'There are {counter} number of collision with the entered name!')
|
||||
else:
|
||||
print('could not find any collision!')
|
@ -0,0 +1,65 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Block Integrity -> Collision: Tutorial 1.2
|
||||
|
||||
The goal of this tutorial is to learn how a cryptographic hash function works.
|
||||
In addition, how a collision can occur. A hash function takes a data input and returns a fixed length of bits.
|
||||
Be aware that some hash algorithms are now known for their hash collision weaknesses.
|
||||
In this tutorial you will be able to observe collisions using blake2b digest algorithm.
|
||||
This algorithm can have different features dependent on the module your are using, such as:
|
||||
* salting,
|
||||
* personalization
|
||||
* and tree hashing
|
||||
In tutorial 1 and 2 you will work with different modules like hashlib and pyblake2.
|
||||
Both modules contain blake2b digest algorithm.
|
||||
Changing the size of the resulting digest in byte of the blake2b algorithm will reduce the chance of a collision.
|
||||
|
||||
Your task is to:
|
||||
* locate the TODOs in this file
|
||||
* run this tutorial and observe the output
|
||||
|
||||
To test run 'Collision-2.py' in your command line
|
||||
|
||||
Notes:
|
||||
* do not change class structure or method signature to not break unit tests
|
||||
* visit these urls for more information on this topic:
|
||||
https://docs.python.org/3/library/hashlib.html
|
||||
https://pythonhosted.org/pyblake2/module.html#pyblake2.blake2b
|
||||
"""
|
||||
from hashlib import blake2b
|
||||
|
||||
# TODO 1: Read the code and understand each statement
|
||||
# TODO 2: Run the code and observe the output
|
||||
# TODO 3: Change or increment the size of the resulting digest in byte
|
||||
# TODO 4: Rerun this program after changing the size
|
||||
hash_size_in_bytes = 1
|
||||
|
||||
path = 'files'
|
||||
names_list = []
|
||||
with open(path + '/names.txt', 'r') as f_names:
|
||||
names_list = [name.lower() for name in f_names.read().splitlines()]
|
||||
|
||||
print(len(names_list))
|
||||
|
||||
myname = input('please enter your name: ').lower()
|
||||
digest = blake2b(digest_size = hash_size_in_bytes)
|
||||
|
||||
digest.update(bytes(myname, 'utf-8'))
|
||||
hash_of_myname = digest.hexdigest()
|
||||
print(hash_of_myname)
|
||||
|
||||
found = False
|
||||
counter = 0
|
||||
for name in names_list:
|
||||
digest = blake2b(digest_size = hash_size_in_bytes)
|
||||
digest.update(bytes(name, 'utf-8'))
|
||||
h = digest.hexdigest()
|
||||
|
||||
if h == hash_of_myname and name != myname:
|
||||
print(f'hash of "{name.lower()}" has collision with your name "{myname}": hash = {h}')
|
||||
found = True
|
||||
counter += 1
|
||||
|
||||
if found:
|
||||
print(f'There are {counter} number of collision with the entered name!')
|
||||
else:
|
||||
print('could not find any collision!')
|
18239
period_1/04-blockchain_class/401_T01_Collision_Attack_1/names.txt
Normal file
18239
period_1/04-blockchain_class/401_T01_Collision_Attack_1/names.txt
Normal file
File diff suppressed because it is too large
Load Diff
@ -0,0 +1,66 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Block Integrity -> Collision Attack: Tutorial 2
|
||||
|
||||
The goal of this tutorial is to learn how a cryptographic hash function can be attacked.
|
||||
In such an attack the attacker tries to find two different messages m1 and m2 such that hash(m1) = hash(m2).
|
||||
In other words he tries to find two inputs producing the same hash value
|
||||
Changing the size of the resulting digest in byte of the blake2b algorithm will reduce the chance of a collision attack.
|
||||
|
||||
Your task is to:
|
||||
* locate the TODOs in this file
|
||||
* run this tutorial and observe the output
|
||||
|
||||
To test run 'Collision.py' in your command line
|
||||
|
||||
Notes:
|
||||
* do not change class structure or method signature to not break unit tests
|
||||
"""
|
||||
from pyblake2 import blake2b
|
||||
import random
|
||||
import string
|
||||
|
||||
# TODO 1: Read the code and understand each statement
|
||||
# TODO 2: Run the code and observe the output
|
||||
# TODO 3: Change or increment the size of the resulting digest in byte
|
||||
# TODO 4: Re-run this program after changing the size
|
||||
hash_size_in_bytes = 2
|
||||
|
||||
def gen_random_string(n):
|
||||
rnd_char = string.ascii_letters + string.digits + string.punctuation + string.punctuation
|
||||
|
||||
rnd_string = ''
|
||||
for _ in range(n):
|
||||
rnd_string += random.choice(rnd_char)
|
||||
|
||||
rnd_list = list(rnd_string)
|
||||
random.SystemRandom().shuffle(rnd_list)
|
||||
rnd_string = ''.join(rnd_list)
|
||||
|
||||
return rnd_string
|
||||
|
||||
|
||||
myname = input('please enter your name: ').lower()
|
||||
digest = blake2b(digest_size = hash_size_in_bytes)
|
||||
|
||||
digest.update(bytes(myname, 'utf-8'))
|
||||
hash_of_myname = digest.hexdigest()
|
||||
print(hash_of_myname)
|
||||
|
||||
found = False
|
||||
counter = 0
|
||||
for _ in range(1000000):
|
||||
digest = blake2b(digest_size = hash_size_in_bytes)
|
||||
rnd_str = gen_random_string(8)
|
||||
print(rnd_str, end='\r')
|
||||
digest.update(bytes(rnd_str, 'utf-8'))
|
||||
h = digest.hexdigest()
|
||||
|
||||
if h == hash_of_myname:
|
||||
print(f'hash of "{rnd_str}" has collision with your name "{myname}": hash = {h}')
|
||||
found = True
|
||||
counter += 1
|
||||
|
||||
if found:
|
||||
print(f'Total number of collisions = {counter}')
|
||||
else:
|
||||
print('Could not find a collision!')
|
@ -0,0 +1,55 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Block Integrity -> File Comparison: Tutorial 3
|
||||
|
||||
The goal of this tutorial is to learn how a cryptographic hash function can be applied on files.
|
||||
This technique is called 'compare-by-Hash'. Instead of comparing files byte-by-byte, we compare their hashes.
|
||||
In case they differ, then the files are certainly different.
|
||||
This technique can be useful when file comparison is needed over the network. Instead of sending
|
||||
a large file we sending a small hash digest. Another use case is to check if a file has been tampered.
|
||||
This technique calculates a digest and does not alter the original file.
|
||||
|
||||
Your task is to:
|
||||
* locate the TODOs in this file
|
||||
* complete the code and compare the hash of the original file with other received files
|
||||
* find out which file is not tampered and is exaclty equal to the original file
|
||||
|
||||
To test run 'FileComparison.py' in your command line
|
||||
|
||||
Notes:
|
||||
* do not change class structure or method signature to not break unit tests
|
||||
* visit this url for more information on this topic:
|
||||
https://home.cs.colorado.edu/~jrblack/papers/cbh.html
|
||||
"""
|
||||
from cryptography.hazmat.primitives import hashes
|
||||
from os import listdir
|
||||
from os.path import isfile, join
|
||||
|
||||
|
||||
path = 'files'
|
||||
|
||||
with open(path + '/original.png', 'rb') as original_file:
|
||||
content = original_file.read()
|
||||
|
||||
# TODO 1: Find the hash of the original file
|
||||
# use SHA256() hash function
|
||||
|
||||
original_hash = None # you need to modify this
|
||||
|
||||
|
||||
file_list = [f for f in listdir(path + '/received/') if isfile(join(path + '/received/', f))]
|
||||
|
||||
|
||||
# TODO 2: Find the hash of the received files using the same hash function in a loop
|
||||
# and compare them with the hash of original file
|
||||
# find out which file is not tampered?
|
||||
|
||||
for f in file_list:
|
||||
with open(path + '/received/' + f, 'rb') as copy_file:
|
||||
content = copy_file.read()
|
||||
|
||||
hash = f # you need to modify this
|
||||
|
||||
if hash == original_hash:
|
||||
print(f, 'is original!')
|
||||
else:
|
||||
print(f, 'is tampered!')
|
Binary file not shown.
After Width: | Height: | Size: 2.9 MiB |
Binary file not shown.
After Width: | Height: | Size: 2.9 MiB |
Binary file not shown.
After Width: | Height: | Size: 2.9 MiB |
Binary file not shown.
After Width: | Height: | Size: 2.9 MiB |
Binary file not shown.
After Width: | Height: | Size: 2.9 MiB |
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Loading…
x
Reference in New Issue
Block a user