sshwot/src/write_file.py

62 lines
1.5 KiB
Python

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