-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathsolution.py
executable file
·95 lines (62 loc) · 2.12 KB
/
solution.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
#! /usr/bin/env python3
import json
import sys
# sha256.py in this directory
import sha256
inputs = json.load(sys.stdin)
outputs = {}
### Building Blocks
# Problem 1
outputs["problem1"] = [sha256.add32(a, b) for a, b in inputs["problem1"]]
# Problem 2
outputs["problem2"] = [sha256.rightrotate32(a, b) for a, b in inputs["problem2"]]
### The Message Schedule
# Problem 3
outputs["problem3"] = sha256.little_sigma0(inputs["problem3"])
# Problem 4
outputs["problem4"] = sha256.little_sigma1(inputs["problem4"])
# Problem 5
outputs["problem5"] = sha256.message_schedule_array(inputs["problem5"].encode())
### The Round Function
# Problem 6
outputs["problem6"] = sha256.big_sigma0(inputs["problem6"])
# Problem 7
outputs["problem7"] = sha256.big_sigma1(inputs["problem7"])
# Problem 8
outputs["problem8"] = sha256.choice(*inputs["problem8"])
# Problem 9
outputs["problem9"] = sha256.majority(*inputs["problem9"])
# Problem 10
obj = inputs["problem10"]
outputs["problem10"] = sha256.round(
obj["state"], obj["round_constant"], obj["schedule_word"]
)
### The Compression Function
# Problem 11
obj = inputs["problem11"]
outputs["problem11"] = sha256.compress_block(obj["state"], obj["block"].encode())
### Padding
# Problem 12
outputs["problem12"] = [sha256.padding_bytes(n).hex() for n in inputs["problem12"]]
### The Hash Function
# Problem 13
outputs["problem13"] = [sha256.sha256(s.encode()).hex() for s in inputs["problem13"]]
### The Length Extension Attack
# Problem 14
original_input = inputs["problem14"]["original_input"].encode()
chosen_suffix = inputs["problem14"]["chosen_suffix"].encode()
outputs["problem14"] = (
original_input + sha256.padding_bytes(len(original_input)) + chosen_suffix
).hex()
# Problem 15
outputs["problem15"] = sha256.reconstitute_state(bytes.fromhex(inputs["problem15"]))
# Problem 16
obj = inputs["problem16"]
original_hash = bytes.fromhex(obj["original_hash"])
original_len = obj["original_len"]
chosen_suffix = obj["chosen_suffix"].encode()
outputs["problem16"] = sha256.length_extend(
original_hash, original_len, chosen_suffix
).hex()
json.dump(outputs, sys.stdout, indent=" ")
print()