From: Alain Spineux Date: Tue, 12 Apr 2022 10:10:11 +0000 (+0200) Subject: Add script scripts/md5tobase64.py X-Git-Tag: Beta-15.0.0~473 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=652278a0421b87f29093f8fc16703be0de8c734e;p=thirdparty%2Fbacula.git Add script scripts/md5tobase64.py usage: md5tobase64.py [-h] [--keep-padding] [FILENAME [FILENAME ...]] Convert md5 in Hexa into base64. positional arguments: FILENAME input file. If no file use STDIN optional arguments: -h, --help show this help message and exit --keep-padding keep the '=' at the end if any --- diff --git a/bacula/scripts/md5tobase64.py b/bacula/scripts/md5tobase64.py new file mode 100755 index 000000000..893cb0fc8 --- /dev/null +++ b/bacula/scripts/md5tobase64.py @@ -0,0 +1,39 @@ +#!/usr/bin/env python3 +# Copyright (C) 2000-2022 Kern Sibbald +# License: BSD 2-Clause; see file LICENSE-FOSS + +# Script used to convert the md5sum output to the md5 bacula format + +import sys +import argparse +import re +#import codecs +import base64 + +md5_re=re.compile('[0-9A-Fa-f]{32}') + +parser=argparse.ArgumentParser(description='Convert md5 in Hexa into base64.') +parser.add_argument('file', metavar='FILENAME', help="input file. If no file use STDIN", nargs='*') +parser.add_argument('--keep-padding', action='store_true', help="keep the '=' at the end if any") +args=parser.parse_args() + +def handle_file(input_file, remove_padding): + for line in input_file: + line=line.rstrip() + if md5_re.match(line): + # don't use the line below that does mime encoding (split at 76c & add a \n) + # b64=codecs.encode(codecs.decode(line, 'hex'), 'base64').decode() + b64=base64.b64encode(bytes.fromhex(line)).decode() + if remove_padding: + b64=b64.rstrip('=') + print(b64) + +remove_padding=not args.keep_padding +if args.file: + for filename in args.file: + input_file=open(filename) + handle_file(input_file, remove_padding) +else: + # use stdin + handle_file(sys.stdin, remove_padding) +