Automatic recognition of 5 kinds of common web encoding and transformation algorithms

Source: Internet
Author: User
Tags base64

#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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.