コード例 #1
0
ファイル: erfr-keysplit.py プロジェクト: urbanware-org/erfr
def main():
    from core import clap
    from core import keyfile
    from datetime import datetime as dt

    try:
        p = clap.Parser()
    except Exception as e:
        print "%s: error: %s" % (os.path.basename(sys.argv[0]), e)
        sys.exit(1)

    p.set_description("Split a key file into seperate parts and merge " \
                      "multiple key file parts to a single key file.")
    p.set_epilog("Further information and usage examples can be found " \
                 "inside the documentation file for this script.")

    # Define required arguments
    p.add_predef("-a", "--action", "action to perform", "action",
                 ["merge", "split"], True)
    p.add_avalue("-k", "--key-file", "key file path", "key_file", None, True)

    # Define optional arguments
    p.add_avalue("-b", "--buffer-size", "buffer size in bytes", "buffer_size",
                 4096, False)
    p.add_switch("-h", "--help", "print this help message and exit", None,
                 True, False)
    p.add_switch(None, "--overwrite", "overwrite existing file", "overwrite",
                 True, False)
    p.add_avalue("-p", "--parts", "split key into separate parts", "parts", 1,
                 False)
    p.add_switch(None, "--version", "print the version number and exit", None,
                 True, False)

    if len(sys.argv) == 1:
        p.error("At least one required argument is missing.")
    elif ("-h" in sys.argv) or ("--help" in sys.argv):
        p.print_help()
        sys.exit(0)
    elif "--version" in sys.argv:
        print keyfile.get_version()
        sys.exit(0)

    args = p.parse_args()
    if args.action == "merge" and args.parts > 1:
        p.error("The parts argument does not make any sense when merging " \
                "files.")

    try:
        timestamp = dt.now()
        if args.action == "split":
            keyfile.split_key(args.key_file, args.parts, args.buffer_size,
                              args.overwrite)
        else:
            keyfile.merge_key(args.key_file, args.buffer_size, args.overwrite)
        print "Elapsed time: %s" % (dt.now() - timestamp)
    except Exception as e:
        p.error(e)
コード例 #2
0
def main():
    from core import clap
    from core import keyfile
    from datetime import datetime as dt

    try:
        p = clap.Parser()
    except Exception as e:
        print "%s: error: %s" % (os.path.basename(sys.argv[0]), e)
        sys.exit(1)

    p.set_description("Find the corresponding key for an encrypted file " \
                      "and vice versa.")
    p.set_epilog("Further information and usage examples can be found " \
                 "inside the documentation file for this script.")

    # Define required arguments
    p.add_avalue("-d", "--directory", "directory to check for the " \
                 "corresponding files", "directory", None, True)
    p.add_avalue("-f", "--file", "file to compare (either the key or the " \
                 "encrypted file)", "input_file", None, True)

    # Define optional arguments
    p.add_switch("-h", "--help", "print this help message and exit", None,
                 True, False)
    p.add_switch("-i", "--ignore-read-errors", "ignore read errors inside "\
                 "given directory", "ignore_read_errors", True, False)
    p.add_switch(None, "--version", "print the version number and exit", None,
                 True, False)
    p.add_avalue("-e", "--obfuscate-enc", "number of bytes used to " \
                 "obfuscate the encrypted file", "obfuscate_enc", 0, False)
    p.add_avalue("-k", "--obfuscate-key", "number of bytes used to " \
                 "obfuscate the key file", "obfuscate_key", 0, False)

    if len(sys.argv) == 1:
        p.error("At least one required argument is missing.")
    elif ("-h" in sys.argv) or ("--help" in sys.argv):
        p.print_help()
        sys.exit(0)
    elif "--version" in sys.argv:
        print keyfile.get_version()
        sys.exit(0)

    args = p.parse_args()
    if args.ignore_read_errors == None:
        args.ignore_read_errors = False

    try:
        timestamp = dt.now()
        list_matches = keyfile.compare_files(args.input_file, args.directory,
                                             args.ignore_read_errors,
                                             args.obfuscate_enc,
                                             args.obfuscate_key)
        if len(list_matches) == 0:
            print "\nNo matches found for \"%s\"." % args.input_file
        else:
            if len(list_matches) == 1:
                print "\nFound one possible match for \"%s\":\n" \
                      % args.input_file
            else:
                print "\nFound %s possible matches for \"%s\":\n" \
                      % (str(len(list_matches)), args.input_file)
            print "-" * 78
            for match in list_matches:
                print "%s" % match
            print "-" * 78
        print "\nElapsed time: %s" % (dt.now() - timestamp)
    except Exception as e:
        p.error(e)
コード例 #3
0
def main():
    from core import clap
    from core import common
    from core import keyfile
    from datetime import datetime as dt

    try:
        p = clap.Parser()
    except Exception as e:
        print "%s: error: %s" % (os.path.basename(sys.argv[0]), e)
        sys.exit(1)

    p.set_description("Generate key files which can either be used for " \
                      "encryption or obfuscation purposes (as fake key " \
                      "files).")
    p.set_epilog("Further information and usage examples can be found " \
                 "inside the documentation file for this script.")

    # Define required arguments
    p.add_avalue("-s", "--key-size", "key size in bytes", "key_size", None,
                 True)

    # Define optional arguments
    p.add_switch(None, "--base64", "generate Base64 key string", "base64",
                 True, False)
    p.add_avalue("-b", "--buffer-size", "buffer size in bytes", "buffer_size",
                 4096, False)
    p.add_switch(None, "--dev-random", "use \"/dev/random\" as random " \
                 "number generator (Unix-like systems only)", "dev_random",
                 True, False)
    p.add_switch(None, "--fortuna", "use Fortuna as random number generator",
                 "fortuna", True, False)
    p.add_switch("-h", "--help", "print this help message and exit", None,
                 True, False)
    p.add_avalue("-k", "--key-file", "key file path", "key_file", None, False)
    p.add_switch(None, "--overwrite", "overwrite existing file", "overwrite",
                 True, False)
    p.add_avalue("-p", "--parts", "split key into separate parts", "parts", 1,
                 False)
    p.add_avalue("-t", "--task-id", "user-defined task ID", "task_id", None,
                 False)
    p.add_switch(None, "--version", "print the version number and exit", None,
                 True, False)

    if len(sys.argv) == 1:
        p.error("At least one required argument is missing.")
    elif ("-h" in sys.argv) or ("--help" in sys.argv):
        p.print_help()
        sys.exit(0)
    elif "--version" in sys.argv:
        print keyfile.get_version()
        sys.exit(0)

    args = p.parse_args()
    if not args.base64 and args.key_file == None:
        p.error("The argument to either generate a key file or a Base64 " \
                "encoded key string is missing.")
    elif args.base64 and not args.key_file == None:
        p.error("The arguments to generate a key file and a Base64 key " \
                "string cannot be given at the same time.")
    elif args.base64 and args.overwrite:
        p.error("The overwrite argument does not make any sense when " \
                "generating a Base64 string.")
    elif args.base64 and args.parts > 1:
        p.error("The parts argument does not make any sense when " \
                "generating a Base64 string.")

    if args.base64:
        if not args.task_id == None:
            p.error("No task ID can be given when creating a Base64 key " \
                    "string.")
        try:
            print keyfile.generate_key_string(args.key_size, args.dev_random,
                                              args.fortuna)
        except Exception as e:
            p.error(e)
    else:
        try:
            task_id = common.get_task_id(args.task_id)
        except Exception as e:
            task_id = args.task_id
            p.error(e)

        try:
            timestamp = dt.now()
            common.status(task_id, "key generation", "start")
            keyfile.generate_key_file(task_id, args.key_file, args.key_size,
                                      args.buffer_size, 0, False,
                                      args.dev_random, args.fortuna,
                                      args.overwrite, args.parts)
            common.status(task_id, "key generation", "finish")
            print "Elapsed time: %s" % (dt.now() - timestamp)
        except Exception as e:
            common.status(task_id, "key generation", "cancel")
            p.error(e)
        finally:
            common.delete_temp_files(task_id)