Bacula-users

Re: [Bacula-users] python implementation of bacula md5

2010-09-12 09:19:04
Subject: Re: [Bacula-users] python implementation of bacula md5
From: "James Harper" <james.harper AT bendigoit.com DOT au>
To: "Rory Campbell-Lange" <rory AT campbell-lange DOT net>, "bacula-users" <bacula-users AT lists.sourceforge DOT net>
Date: Sun, 12 Sep 2010 23:15:13 +1000
> 
> I noted in my email of 14 August that I had a file with a bacula md5
> that I was not able to match using the (Linux, Debian stable) md5 cli
> utility.
> 
> I would be grateful to learn if anyone has replicated the method of
> calculating an md5 hash using python. If so I'd be grateful to know
how
> it is done.
> 

>>From memory, the MD5 is 'correct', but the base64 translation is a bit
funny. Here's a C# version I wrote quite a while back, if it's any help.
It's the baculaBase64 routine that's probably the most useful:

        public static string toBase64(UInt64 val)
        {
            int digits = 0;
            UInt64 tmp = val;
            do
            {
                tmp >>= 6;
                digits++;
            } while (tmp != 0);
            string s = "";
            tmp = val;
            do
            {
                s = base64lookup[tmp & 0x3F] + s;
                tmp >>= 6;
            } while (tmp != 0);
            return s;
        }

        public static string toBase64(Int64 val)
        {
            int digits = 0;
            Int64 tmp = val;
            do
            {
                tmp >>= 6;
                digits++;
            } while (tmp != 0);
            string s = "";
            tmp = val;
            do
            {
                s = base64lookup[tmp & 0x3F] + s;
                tmp >>= 6;
            } while (tmp != 0);
            return s;
        }

        public static string toBase64(byte[] bytes)
        {
            return baculaBase64(bytes);
        }

        private static char[] base64lookup = {
                        'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I',
'J', 'K', 'L', 'M',
                        'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V',
'W', 'X', 'Y', 'Z',
                        'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i',
'j', 'k', 'l', 'm',
                        'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v',
'w', 'x', 'y', 'z',
                    '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
'+', '/'
        };

        public static string baculaBase64(byte[] bytes)
        {
            UInt32 bits;
            int count;
            string result = "";

            for (count = 1; count < bytes.Length; count++)
            {
                if ((bytes[count] & 0x80) == 0x80)
                {
                    switch (count % 3)
                    {
                        case 1:
                            bytes[count - 1] |= 0x03;
                            break;
                        case 2:
                            bytes[count - 1] |= 0x0F;
                            break;
                    }
                }
            }
            count = 0;
            while (count < bytes.Length)
            {
                int endflag = 0;
                bits = bytes[count++];
                if (count < bytes.Length)
                {
                    bits = (bits << 8) | bytes[count++];
                }
                else
                {
                    bits <<= 8;
                    endflag++;
                }
                if (count < bytes.Length)
                {
                    bits = (bits << 8) | bytes[count++];
                }
                else
                {
                    bits <<= 8;
                    endflag++;
                }
                for (int bytelet = 3; bytelet >= 0; bytelet--)
                {
                    uint val = (bits >> (bytelet * 6)) & 0x3F;
                    if (val != 0 || bytelet >= endflag)
                    {
                        if (endflag == 2 && bytelet == 2)
                            val >>= 4;
                        result += base64lookup[val];
                    }
                }
            }
            return result;
        }

        public static string properBase64(byte[] bytes)
        {
            UInt32 bits;
            int count;
            string result = "";
            count = 0;
            while (count < bytes.Length)
            {
                int endflag = 0;
                bits = bytes[count++];
                if (count < bytes.Length)
                    bits = (bits << 8) | bytes[count++];
                else
                {
                    bits <<= 8;
                    endflag++;
                }
                if (count < bytes.Length)
                    bits = (bits << 8) | bytes[count++];
                else
                {
                    bits <<= 8;
                    endflag++;
                }
                for (int bytelet = 3; bytelet >= 0; bytelet--)
                {
                    uint val = (bits >> (bytelet * 6)) & 0x3F;
                    if (val == 0 && bytelet < endflag)
                        result += "=";
                    else
                        result += base64lookup[val];
                }
            }
            return result;
        }

James

------------------------------------------------------------------------------
Start uncovering the many advantages of virtual appliances
and start using them to simplify application deployment and
accelerate your shift to cloud computing
http://p.sf.net/sfu/novell-sfdev2dev
_______________________________________________
Bacula-users mailing list
Bacula-users AT lists.sourceforge DOT net
https://lists.sourceforge.net/lists/listinfo/bacula-users

<Prev in Thread] Current Thread [Next in Thread>