luksrku/gen_config

47 lines
1.4 KiB
Plaintext
Raw Permalink Normal View History

2016-09-22 20:40:58 +02:00
#!/usr/bin/python3
import os
import uuid
import collections
import getpass
import string
def tohex(data):
return "".join("%02x" % (c) for c in data)
def suggest_passphrase():
alphabet = string.ascii_lowercase + string.ascii_uppercase + string.digits
passphrase = os.urandom(24)
passphrase_int = sum(value << (8 * byteno) for (byteno, value) in enumerate(passphrase))
passphrase_ascii = ""
while passphrase_int > 0:
(passphrase_int, charno) = divmod(passphrase_int, len(alphabet))
passphrase_ascii += alphabet[charno]
return passphrase_ascii
Disk = collections.namedtuple("Disk", [ "uuid", "name", "passphrase" ])
host_uuid = str(uuid.uuid4())
host_psk = tohex(os.urandom(32))
disks = [ ]
while True:
disk_uuid = input("Disk UUID : ")
if disk_uuid == "":
break
disk_name = input("Disk name : ")
print("Suggestion: %s" % (suggest_passphrase()))
disk_passphrase = getpass.getpass("Passphrase: ")
disks.append(Disk(uuid = disk_uuid, name = disk_name, passphrase = disk_passphrase))
print()
server_string = ",".join("%s=%s" % (disk.uuid, disk.name) for disk in disks)
client_string = ",".join("%s=%s" % (disk.uuid, tohex(disk.passphrase.encode())) for disk in disks)
print("# server.txt")
print("# Host UUID Host PSK Disk UUIDs")
print("%s %s %s" % (host_uuid, host_psk, server_string))
print()
print("# client.txt")
print("# Host UUID Host PSK Disk UUIDs")
print("%s %s %s" % (host_uuid, host_psk, client_string))