#!/usr/bin/env python
# @file		/home/mfukar/src/anontool/test/md5_test.py
# @author	Michael Foukarakis
# @version	1.0
# @date 	Created:     Wed Feb 09, 2011 08:26 EET
# 		Last Update: Wed Feb 09, 2011 09:48 EET
#------------------------------------------------------------------------
# Description:	MD5 test
#------------------------------------------------------------------------
# History:	None
# TODO:		Nothing yet
#------------------------------------------------------------------------

import sys
from ctypes import *

# For strlen()
stdlib  = cdll.LoadLibrary('libc.so.6')
nidslib = CDLL('../lib/libnids.so', RTLD_GLOBAL)
anonlib = CDLL('../lib/anonlib.so')

class MD5_Sum(Structure):
        _fields_ = [("buffer", c_ubyte * 16)]

def MD5_test():
        keys = [('', [0xD4, 0x1D, 0x8C, 0xD9, 0x8F, 0x00, 0xB2, 0x04, 0xE9, 0x80, 0x09, 0x98, 0xEC, 0xF8, 0x42, 0x7E]),
                ('a', [0x0C, 0xC1, 0x75, 0xB9, 0xC0, 0xF1, 0xB6, 0xA8, 0x31, 0xC3, 0x99, 0xE2, 0x69, 0x77, 0x26, 0x61]),
                ('abc', [0x90, 0x01, 0x50, 0x98, 0x3C, 0xD2, 0x4F, 0xB0, 0xD6, 0x96, 0x3F, 0x7D, 0x28, 0xE1, 0x7F, 0x72]),
                ('message digest', [0xF9, 0x6B, 0x69, 0x7D, 0x7C, 0xB7, 0x93, 0x8D, 0x52, 0x5A, 0x2F, 0x31, 0xAA, 0xF1, 0x61, 0xD0]),
                ('abcdefghijklmnopqrstuvwxyz', [0xC3, 0xFC, 0xD3, 0xD7, 0x61, 0x92, 0xE4, 0x00, 0x7D, 0xFB, 0x49, 0x6C, 0xCA, 0x67, 0xE1, 0x3B]),
                ('ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789', [0xD1, 0x74, 0xAB, 0x98, 0xD2, 0x77, 0xD9, 0xF5, 0xA5, 0x61, 0x1C, 0x2C, 0x9F, 0x41, 0x9D, 0x9F]),
                ('12345678901234567890123456789012345678901234567890123456789012345678901234567890', [0x57, 0xED, 0xF4, 0xA2, 0x2B, 0xE3, 0xC9, 0x55, 0xAC, 0x49, 0xDA, 0x2E, 0x21, 0x07, 0xB6, 0x7A])
               ]
        digest = MD5_Sum()
        for key, val in keys:
                p = create_string_buffer(key)
                anonlib.md5_sum(c_char_p(key), stdlib.strlen(p), byref(digest))
                result = [byte for byte in digest.buffer]
                if result != val:
                        raise AssertionError("[-] MD5 test failed.")
        print("[+] MD5 test passed.")

if __name__ == '__main__':
	MD5_test()
