All pastes #573819 Raw Edit

Mine

public text v1 · immutable
·published 2007-06-18 08:40 UTC
rendered paste body
#!/usr/bin/env python
"""
bcode - bacula[en|de]coder v1.0

bcode converts bacula native hashes to/from standard hashes

Usage: bcode [option] [hash]

Example:
        bcode -b asdf== 
        bcode -x a1dd3
"""

import base64
import sys
import getopt

def tobase64(hash):
        md5 = base64.b16decode(hash.upper())
        return base64.b64encode(md5)[:-2]

def tohex(hash):
        # Need to add == for padding because bacula strips this
        md5 = base64.b64decode(hash + "==")
        return base64.b16encode(md5).lower()

def invalid():
        print
        print "bcode requires option -b or -x"
        print "for help use --help"
        print
        sys.exit(0)

def main():
        # parse command line options
        try:
                opts, args = getopt.getopt(sys.argv[1:], "hb:x:", ["help", "base64=", "hex="])
        except getopt.error, msg:
                print msg
                print "for help use --help"
                sys.exit(2)

        for opt, arg in opts:
                if opt in ("-h", "--help"):
                        print __doc__
                        sys.exit(0)
                if opt in ("-b", "--base64"):
                        print tobase64(arg)
                if opt in ("-x", "--hex"):
                        print tohex(arg)

        if len(opts) == 0:
                invalid()


if __name__ == "__main__":
        main()