#-------------------------------------------------------------------------------
# Name:        hex encoder, and decoder
# Purpose:     A small tool created to assisit in <<SQL INJECTION>>
#              And of course, for us to learn together.
#-------------------------------------------------------------------------------

import os,sys, base64
from time import sleep
from Tkinter import *
import tkMessageBox

def centerWindow(frame):
    w = 300; h=250
    frame.maxsize(300,250)
    #frame.minsize(300,220)

    sw = frame.winfo_screenwidth()
    sh = frame.winfo_screenheight()

    x = (sw-w)/2
    y = (sh-h)/2
    frame.geometry('%dx%d+%d+%d' % (w,h,x,y))
    
def msg(title,message):
    tkMessageBox.showerror(title,message)

    
def hexit():
    clear_box()
    txt = txt_box.get()
    if txt == '':
        msg('Error','Please Enter String to Convert')
        Entry.focus_set(txt_box)
    else:
        txt_box.delete(0,len(str(txt_box.get())))
        hexed = '0x'+txt.encode('hex')
        txt_box_display.insert(END,hexed)

def clear_box():
    txt_box_display.delete(1.0,END)

def dehexit():
    clear_box()
    hexed = txt_box.get()
    txt_box.delete(0,len(str(txt_box.get())))
    if hexed == '': msg('Error','Please Enter hex to convert')
    if '0x' in hexed: hexed = hexed.replace('0x','')
    try:
        dehexed = hexed.decode('hex')
    except TypeError:
        msg("Error","That didn't seem like hex")
    else:
        txt_box_display.insert(END,dehexed)

def about():
    tkMessageBox.showinfo('About','This script is written by Anubis from MSF forum\n\
This script supports conversion of string to hex and vice versa.\n\
And conversion of md5 is also supported.\n\n\
                                            \t\tAnubis [MSF Moderator]')

def base():
    clear_box()
    string = txt_box.get()
    txt_box.delete(0,len(str(txt_box.get())))
    if string == '': msg('Error','Please Enter String to convert')
    else:
        bas64 = base64.standard_b64encode(string)
        txt_box_display.insert(END,bas64)

def b2s():
    clear_box()
    enc_string = txt_box.get()
    txt_box.delete(0,len(str(txt_box.get())))
    if enc_string == '': msg('Error','Please Enter String to convert')
    else:
        string = base64.standard_b64decode(enc_string)
        txt_box_display.insert(END,string)
        
def sha():
    clear_box()
    from hashlib import sha1
    string = txt_box.get()
    txt_box.delete(0,len(str(txt_box.get())))
    if string == '': msg('Error','Please Enter String to convert')
    else:
        s = sha1()
        s.update(string)
        sha_hash = s.hexdigest()
        txt_box_display.insert(END,sha_hash)
    
def md5it():
    clear_box()
    from hashlib import md5
    string = txt_box.get()
    txt_box.delete(0,len(str(txt_box.get())))
    if string == '': msg('Error','Please Enter string to convert')
    else:
        m = md5()
        m.update(string)
        md5_hash = m.hexdigest()
        txt_box_display.insert(END,md5_hash)

def goodbye():
    import sys
    sys.exit()
    
    
def gui():
    global txt_box,txt_box_display,root
    
    root = Tk()
    root.title('Hex-converter')
    lbl_welcome = Label(root,text="String to hash/hex converter")
    lbl_welcome.pack()
    centerWindow(root)

    txt_box = Entry(root,width='35')
    txt_box.pack()

    #btn_frame = LabelFrame(root)
    #btn_frame.pack()
    
    btn_str2hex = Button(root,text="Str2Hex",command=hexit,width='10')
    btn_str2hex.place(x=25,y=42)

    btn_hex2str = Button(root,text="Hex2Str",command=dehexit,width='10')
    btn_hex2str.place(x=25,y=67)

    btn_md5 = Button(root,text="Md5",command=md5it,width='10')
    btn_md5.place(x=195,y=42)

    btn_sha1 = Button(root,text="Sha1",command=sha,width='10')
    btn_sha1.place(x=195,y=67)

    btn_b64 = Button(root,text="Str2Base64",command=base,width='10')
    btn_b64.place(x=110,y=42)

    btn_b642str = Button(root,text="Base64toStr",command=b2s,width='10')
    btn_b642str.place(x=110,y=67)
    
    txt_box_display = Text(root)
    txt_box_display.config(width='33',height='6')
    txt_box_display.place(x=20,y=100)

    btn_quit = Button(root,text="QUIT",command=exit,width='15')
    btn_quit.place(x=90,y=210)

    menubar = Menu(root)
    help_menu = Menu(menubar,tearoff=0)
    help_menu.add_command(label="About",command=about)
    menubar.add_cascade(label="Help",menu=help_menu)
    root.config(menu=menubar)
    
    root.mainloop()

if __name__ == '__main__':
    gui()



