Skip to content

Commit f89ce40

Browse files
atombrellaalex
authored andcommitted
Replace legacy file handling with a context manager. (pyca#5092)
* Replace legacy file handling with a context manager. * flake8 fix Co-authored-by: Alex Gaynor <alex.gaynor@gmail.com>
1 parent a849f40 commit f89ce40

File tree

1 file changed

+22
-21
lines changed

1 file changed

+22
-21
lines changed

docs/development/custom-vectors/cast5/generate_cast5.py

Lines changed: 22 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -23,33 +23,34 @@ def encrypt(mode, key, iv, plaintext):
2323

2424

2525
def build_vectors(mode, filename):
26-
vector_file = open(filename, "r")
27-
2826
count = 0
2927
output = []
3028
key = None
3129
iv = None
3230
plaintext = None
33-
for line in vector_file:
34-
line = line.strip()
35-
if line.startswith("KEY"):
36-
if count != 0:
37-
output.append("CIPHERTEXT = {}".format(
38-
encrypt(mode, key, iv, plaintext))
39-
)
40-
output.append("\nCOUNT = {}".format(count))
41-
count += 1
42-
name, key = line.split(" = ")
43-
output.append("KEY = {}".format(key))
44-
elif line.startswith("IV"):
45-
name, iv = line.split(" = ")
46-
iv = iv[0:16]
47-
output.append("IV = {}".format(iv))
48-
elif line.startswith("PLAINTEXT"):
49-
name, plaintext = line.split(" = ")
50-
output.append("PLAINTEXT = {}".format(plaintext))
5131

52-
output.append("CIPHERTEXT = {}".format(encrypt(mode, key, iv, plaintext)))
32+
with open(filename, "r") as vector_file:
33+
for line in vector_file:
34+
line = line.strip()
35+
if line.startswith("KEY"):
36+
if count != 0:
37+
output.append("CIPHERTEXT = {}".format(
38+
encrypt(mode, key, iv, plaintext))
39+
)
40+
output.append("\nCOUNT = {}".format(count))
41+
count += 1
42+
name, key = line.split(" = ")
43+
output.append("KEY = {}".format(key))
44+
elif line.startswith("IV"):
45+
name, iv = line.split(" = ")
46+
iv = iv[0:16]
47+
output.append("IV = {}".format(iv))
48+
elif line.startswith("PLAINTEXT"):
49+
name, plaintext = line.split(" = ")
50+
output.append("PLAINTEXT = {}".format(plaintext))
51+
output.append(
52+
"CIPHERTEXT = {}".format(encrypt(mode, key, iv, plaintext))
53+
)
5354
return "\n".join(output)
5455

5556

0 commit comments

Comments
 (0)