2018-08-29 21:16:22 +00:00
|
|
|
import hashing
|
2018-08-29 09:32:01 +00:00
|
|
|
|
|
|
|
def write_header(f, file_comment):
|
|
|
|
"""write_header(file(wb), str)
|
2018-08-28 09:27:54 +00:00
|
|
|
Writes the header to the given file."""
|
2018-08-29 09:32:01 +00:00
|
|
|
assert type(file_comment) == str
|
|
|
|
# b'SSHWOT' magic
|
|
|
|
f.write(b'SSHWOT')
|
2018-08-29 21:16:22 +00:00
|
|
|
# Separating space
|
|
|
|
f.write(b' ')
|
2018-08-28 09:27:54 +00:00
|
|
|
# Version number
|
2018-08-29 09:32:01 +00:00
|
|
|
f.write(b'0')
|
|
|
|
# b' ' + file_comment, if there is one
|
|
|
|
if len(file_comment) > 0:
|
|
|
|
f.write(b' ')
|
|
|
|
assert b'\n' not in file_comment
|
|
|
|
f.write(file_comment)
|
|
|
|
# End of header marked with b'\n'
|
|
|
|
f.write(b'\n')
|
2018-08-28 09:27:54 +00:00
|
|
|
|
|
|
|
def write_entry(f, salt, hashed_host, fingerprint, comment):
|
2018-08-29 09:32:01 +00:00
|
|
|
"""write_entry(file(wb), bytes[32], bytes[32], bytes[32], str)
|
2018-08-28 09:27:54 +00:00
|
|
|
Writes an entry to the given file."""
|
|
|
|
assert type(salt) == bytes and len(salt) == 32
|
|
|
|
assert type(hashed_host) == bytes and len(hashed_host) == 32
|
|
|
|
assert type(fingerprint) == bytes and len(fingerprint) == 32
|
2018-08-29 09:32:01 +00:00
|
|
|
assert type(comment) == str
|
2018-08-28 09:27:54 +00:00
|
|
|
|
2018-08-29 21:16:22 +00:00
|
|
|
# base64 encoded salt
|
|
|
|
f.write(hashing.base64enc(salt))
|
2018-08-28 09:27:54 +00:00
|
|
|
|
2018-08-29 21:16:22 +00:00
|
|
|
# Separating space
|
|
|
|
f.write(b' ')
|
2018-08-28 09:27:54 +00:00
|
|
|
|
2018-08-29 21:16:22 +00:00
|
|
|
# base64 encoded hashed_host
|
|
|
|
f.write(hashing.base64enc(hashed_host))
|
|
|
|
|
|
|
|
# Separating space
|
|
|
|
f.write(b' ')
|
|
|
|
|
|
|
|
# base64 encoded fingerprint
|
|
|
|
f.write(hashing.base64enc(fingerprint))
|
2018-08-28 09:27:54 +00:00
|
|
|
|
2018-08-29 09:32:01 +00:00
|
|
|
# b' ' + comment, if there is one
|
|
|
|
if len(comment) > 0:
|
|
|
|
f.write(b' ')
|
|
|
|
assert '\n' not in comment
|
|
|
|
f.write(comment.encode('utf-8'))
|
2018-08-28 09:27:54 +00:00
|
|
|
|
2018-08-29 09:32:01 +00:00
|
|
|
# End of entry marked with b'\n'
|
|
|
|
f.write(b'\n')
|
2018-08-28 09:27:54 +00:00
|
|
|
|
2018-08-29 09:32:01 +00:00
|
|
|
def write(f, entries, file_comment = ''):
|
|
|
|
"""write(file(wb), [Entry], str)
|
2018-08-28 09:27:54 +00:00
|
|
|
Creates a file containing all of the entries"""
|
2018-08-29 09:32:01 +00:00
|
|
|
assert type(file_comment) == str
|
|
|
|
|
|
|
|
write_header(f, file_comment)
|
2018-08-28 09:27:54 +00:00
|
|
|
|
|
|
|
for entry in entries:
|
|
|
|
write_entry(f, entry.salt, entry.hashed_host, entry.fingerprint, entry.comment)
|