sshwot/src/write_file.py

40 lines
1.0 KiB
Python

def write_header(f):
"""write_header(file(wb))
Writes the header to the given file."""
# b'WOT' magic
f.write(b'WOT')
# Version number
f.write(bytes(0))
def write_entry(f, salt, hashed_host, fingerprint, comment):
"""write_entry(file(wb), bytes[32], bytes[32], bytes[32], bytes[0…2¹⁶-1])
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) == bytes and 0 <= len(comment) <= (1<<16) - 1
# u8[32]: salt
f.write(salt)
# u8[32]: hashed_host
f.write(hashed_host)
# u8[32]: fingerprint
f.write(fingerprint)
# u16le: len(comment)
comment_len = len(comment)
f.write(bytes([comment_len & 0xff, comment_len >> 8]))
# u8[]: comment
f.write(comment)
def write(f, entries):
"""write(file(wb), [Entry])
Creates a file containing all of the entries"""
write_header(f)
for entry in entries:
write_entry(f, entry.salt, entry.hashed_host, entry.fingerprint, entry.comment)