Message digest algorithm examples (python and go)

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.

the common Message digest algorithm has MD5 and the SHA , these algorithms are python and the Go in the library, it takes time to invoke the next OK , summed up here python and the Go the implementation.

Example of aPython message digest

The code is as follows:

#!/usr/bin/python'''File:testHash.py      Author:mike      e-mail:mike_zhang@live.com      ''' Import hashlibsrc = raw_input("input string:")funcnamelist = ["MD5","SHA1","SHA224","SHA256","SHA384" ,"SHA512"]Funcmap = { "MD5" : Lambda cnt:hashlib.md5 (CNT). Hexdigest (), "SHA1" : Lambda cnt:hashlib.sha1 (CNT). Hexdigest (), "SHA224" : Lambda cnt:hashlib.sha224 (CNT). Hexdigest (), "SHA256" : Lambda cnt:hashlib.sha256 (CNT). Hexdigest (), "SHA384" : Lambda cnt:hashlib.sha384 (CNT). Hexdigest (), "SHA512" : Lambda cnt:hashlib.sha512 (CNT). Hexdigest ()}For   funcName  in funcnamelist: Print  funcName,"\ t:\ t", Funcmap[funcname] (SRC)         

Operating effect:

Two,go language message Digest Example

The code is as follows:

/*File:hashTest.go      Author:mike      e-mail:mike_zhang@live.com      */Package   main Import ("FMT"        "CRYPTO/MD5"        "CRYPTO/SHA1"        "crypto/sha256"        "crypto/sha512"        "Hash"        ) func Main () {        funcnamelist: = []string{"MD5","SHA1","SHA224","SHA256","SHA384","SHA512"}  funcmap: =    map    [   string     "   func     (msg []    byte    ) hash. hash{      "MD5"   :  
     
        func   
        byte    ) hash. hash{   var     h hash. Hash = MD5. New (); H.write (msg);    return     H},       "SHA1"   :  
     
        func   
        byte    ) hash. hash{   var     h hash. Hash = SHA1. New (); H.write (msg);    return     H},       "SHA224"   :  
     
        func   
        byte    ) hash. hash{   var     h hash. Hash = sha256. New224 (); H.write (msg);    return     H},       "SHA256"   :  
     
        func   
        byte    ) hash. hash{   var     h hash. Hash = sha256. New (); H.write (msg);    return     H},       "SHA384"   :  
     
        func   
        byte    ) hash. hash{   var     h hash. Hash = sha512. New384 (); H.write (msg);    return     H},       "SHA512"   :  
     
        func   
        byte    ) hash. hash{   var     h hash. Hash = sha512. New (); H.write (msg);    return     H},    }         FMT. Printf ("Input string:")          var  msg1  string          FMT. SCANF ("%s", &MSG1)         For    _,funcname: =  range  funcnamelist{          fmt. Printf (  "%s   \t   :   \t   %x   \n    "  ,funcname,funcmap[funcname] ([]    byte      }        }

Operating effect:

Haha, is not found above two groups of code structure of the program is the same ah, in fact, I just want to use python to learn the go language: first with Python is a very lightweight implementation of a function, I would consider using go Do it again. Summarized here, convenient for later use.

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.