Example demonstrating a cli for encrypting directories with NuCypher.

pull/2712/head
Kieran R. Prasch 2019-08-22 15:23:43 -07:00 committed by jMyles
parent 9296d1db83
commit 663a7b2b3c
1 changed files with 28 additions and 26 deletions

View File

@ -5,54 +5,56 @@ import json
import os
import click
import libnacl.secret
from nucypher.characters.lawful import Enrico
from nucypher.cli.actions import make_cli_character
from nucypher.config.characters import AliceConfiguration
from nucypher.crypto.powers import SigningPower
@click.command()
# @click.option('--plaintext-pass-through', type=click.BOOL, required=True) # FIXME
@click.option('--plaintext-dir', type=click.STRING, required=True)
@click.option('--outfile', type=click.STRING)
@click.option('--alice-config', type=click.STRING)
@click.option('--label', type=click.STRING, required=True)
def mario_box_cli(plaintext_dir, alice_config, label):
click.secho("Starting Up...", fg='green')
def mario_box_cli(plaintext_dir, alice_config, label, outfile):
# Derive Policy Encrypting Key
alice_configuration = AliceConfiguration.from_configuration_file(filepath=alice_config)
alice = make_cli_character(character_config=alice_configuration)
alice_signing_key = alice.public_keys(SigningPower)
policy_encrypting_key = alice.get_policy_encrypting_key_from_label(label=label.encode())
policy_encrypting_key_hex = bytes(policy_encrypting_key).hex()
message_kits = list()
output = list()
paths = list(os.listdir(plaintext_dir))
for path in paths:
filepath = os.path.join(plaintext_dir, path)
click.secho(f'Processing {filepath}...')
with open(filepath, 'rb') as file:
plaintext = file.read()
encoded_plaintext = base64.b64encode(plaintext)
click.secho(f"Encrypting {len(paths)} files for policy {policy_encrypting_key_hex}", fg='blue')
# Make the Box
box = libnacl.secret.SecretBox()
with click.progressbar(paths) as bar:
for path in bar:
filepath = os.path.join(plaintext_dir, path)
with open(filepath, 'rb') as file:
plaintext = file.read()
encoded_plaintext = base64.b64encode(plaintext)
# Encrypt file contents symmetrically
ciphertext = box.encrypt(encoded_plaintext)
base64_ciphertext = base64.b64encode(ciphertext).decode()
enrico = Enrico(policy_encrypting_key=policy_encrypting_key)
message_kit, _signature = enrico.encrypt_message(message=encoded_plaintext)
base64_message_kit = base64.b64encode(bytes(message_kit)).decode()
# Encrypt the symmetric key
enrico = Enrico(policy_encrypting_key=policy_encrypting_key)
message_kit, _signature = enrico.encrypt_message(message=box.sk)
base64_message_kit = base64.b64encode(bytes(message_kit)).decode()
# Collect Bob Retrieve JSON Requests
retrieve_payload = {'label': label,
'policy-encrypting-key': policy_encrypting_key_hex,
'alice-verifying-key': bytes(alice_signing_key).hex(),
'message-kit': base64_message_kit}
# Collect ciphertext-message-kit pairs.
message_kits.append((base64_ciphertext, base64_message_kit))
click.secho(f"Encrypted {filepath}...")
output.append(retrieve_payload)
# Generate the output
output = {'ciphertexts': message_kits, 'pek': bytes(policy_encrypting_key).hex()}
click.secho(json.dumps(output))
if not outfile:
outfile = f'{policy_encrypting_key_hex}.json'
with open(outfile, 'w') as file:
file.write(json.dumps(output, indent=2))
click.secho(f"Successfully wrote output to {outfile}", fg='green')
if __name__ == '__main__':