#Coding=utf-8 #recognition of Character sequence transformation algorithms, currently supporting standard MD5, SHA-1, Base64, and mainstream URL encoding, HTML encoding ImportReImportSYS#MD5 judgment functiondefcheckMD5 (INSTR): Md5keystrs='0123456789abcdefABCDEF'inStr= Instr.strip ()#when judging MD5, cut out the blanks at both ends of the input. if(Len (inStr)! = 16) and(Len (inStr)! = 32): returnFalseElse: forEachcharinchINSTR:ifEachchar not inchMd5keystrs:returnFalsereturnTrue#SHA1 judgment functiondefcheckSHA1 (INSTR): Sha1keystrs='0123456789abcdefABCDEF'inStr= Instr.strip ()#when judging SHA-1, cut out the blanks at both ends of the input. ifLen (inStr)! = 40: returnFalseElse: forEachcharinchINSTR:ifEachchar not inchSha1keystrs:returnFalsereturnTrue#Base64 judgment functiondefcheckBase64 (INSTR): Base64keystrs='abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz0123456789+/='inStr= Instr.strip ()#when judging Base64, cut out the blanks at both ends of the input. ifLen (INSTR)% 4! =0:returnFalseElse: forEachcharinchINSTR:ifEachchar not inchBase64keystrs:returnFalsereturnTrue#URL encoding judgment functiondefCheckurlcode (INSTR): Reurlcode='%[0-9a-fa-f][0-9a-fa-f]' #Regular ExpressionsReresultlist =Re.findall (REURLCODE,INSTR)ifLen (reresultlist) = =0:returnFalseElse: returnTrue#HTML code judgment functiondefCheckhtmlcode (INSTR): Htmlencodetuple= ('<','>','&','& #039;','"',' ','& #x27;','& #x2F;') foreachinchhtmlencodetuple:ifeachinchINSTR:returnTruereturnFalse#The total dispatch function, which is responsible for invoking the judgment function of each algorithmdefCheckinput (INSTR):ifcheckMD5 (INSTR): Resstr='MD5' returnResstrifcheckSHA1 (INSTR): Resstr='SHA-1' returnResstrifcheckBase64 (INSTR): Resstr='Base64' returnResstrifCheckurlcode (INSTR):#consider that URL encoding and HTML encoding may appear at the same timeResstr ='Urlcode' ifCheckhtmlcode (INSTR): Resstr='Urlcode + htmlcode' returnResstrElse: returnResstrifCheckhtmlcode (INSTR): Resstr='Htmlcode' returnresstr resstr='UnKnown' returnResstr#Python Main program if __name__=='__main__': ifLen (SYS.ARGV) > 1:#Accept command Line inputInputstr = str (sys.argv[1]) ResultStr=checkinput (INPUTSTR)PrintU'your input is:'. Encode ('gb2312') +InputstrPrintU'The result of the judgment is:'. Encode ('gb2312') +ResultStrElse:#Interactive Interface Print '---------------------------------------------------------------------' PrintU'whatcodes V1.0----------of---------recognition cipher-text transform algorithm'. Encode ('gb2312') PrintU'---Currently supports identification of MD5, SHA-1, Base64, URL encoding, HTML encoding-----'. Encode ('gb2312') PrintU'-- supports interactive and command-line Operations (command line does not support direct input of special characters)--'. Encode ('gb2312') Print '---------------------------------------------------------------------' Print while(True): Inputstr= Raw_input (U'Please enter a sequence of characters (enter ' Q ' to exit the program):'. Encode ('gb2312')) ifInputstr = ='Q': Break elifInputstr = ="': Continue Else: ResultStr=checkinput (INPUTSTR)PrintU'your input is:'. Encode ('gb2312') +InputstrPrintU'The result of the judgment is:'. Encode ('gb2312') +ResultStrPrint
5 Kinds of Web common encoding, transformation algorithm automatic recognition