Example of message digest algorithm (Python and go)

Source: Internet
Author: User
Tags sha1

Common Message SummaryAlgorithmYesMD5AndShaThese algorithms arePythonAndGoIn the library,CallOKNow, here is a summaryPythonAndGo.

I,PythonMessage Digest example

CodeAs follows:

#! /Usr/bin/Python  '''  File: testhash. py  Author: Mike  E-mail: Mike_Zhang@live.com  ''' ImportHashlib  Src =Raw_input("Input string :")  Funcnamelist = ["MD5","Sha1","Shares","Sha256","Sha384","Sha512"]  Funcmap = { "MD5":LambdaCNT: hashlib. MD5 (CNT). hexdigest (),  "Sha1":LambdaCNT: hashlib. sha1 (CNT). hexdigest (),  "Shares":LambdaCNT: hashlib. shaloud (CNT). hexdigest (), "Sha256":LambdaCNT: hashlib. sha256 (CNT). hexdigest (),  "Sha384":LambdaCNT: hashlib. sha384 (CNT). hexdigest (),  "Sha512":LambdaCNT: hashlib. sha512 (CNT). hexdigest ()  } ForFuncnameInFuncnamelist:  PrintFuncname,"\ T:\ T", Funcmap [funcname] (SRC) 

Running effect:

II,GoLanguage message digest example

The Code is as follows:

 /*  File: hashtest. Go  Author: Mike  E-mail: Mike_Zhang@live.com */  PackageMain  Import(  "FMT"  "Crypto/MD5" "Crypto/sha1"  "Crypto/sha256"  "Crypto/sha512"  "Hash" )  FuncMain (){          Funcnamelist: = []  String  {  "MD5"  ,  "Sha1"  ,  "Shares" ,  "Sha256"  ,  "Sha384"  ,  "Sha512"  }    funcmap: =     Map    [    string   ]     func    (MSG []     byte   ) hash. hash {   " MD5 " :    func    (MSG []     byte   ) hash. hash {    var    H hash. hash = md5.new (); H. write (MSG);     return    H },    " sha1 " :    func    (MSG []     byte   ) hash. hash {    var    H hash. hash = sha1.new (); H. write (MSG);     return    H },    " shares' " :    func    (MSG []     byte   ) hash. hash {    var    H hash. hash = sha256.newiterator (); H. write (MSG);     return    H },    " sha256 " :    func    (MSG []     byte   ) hash. hash {    var    H hash. hash = sha256.new (); H. write (MSG);     return    H },    " sha384 " :    func    (MSG []     byte   ) hash. hash {    var    H hash. hash = sha512.new384 (); H. write (MSG);     return    H },    " sha512 " :    func    (MSG []     byte   ) hash. hash {    var    H hash. hash = sha512.new (); H. write (MSG);     return    H }, }  FMT. printf ("Input string :")  VaRMsg1String FMT. scanf ("% S", & Msg1)  For_, Funcname: =RangeFuncnamelist {    FMT. printf (  " % S    \ t   :    \ t    % x    \ n   "  , funcname, funcmap [funcname] ([]     byte    (msg1 )). sum () }  } 

Running effect:

Haha, did you find the above two groups?ProgramThe code structure is the same.PythonTo learnGoLanguage: First UsePythonIt is very lightweight to implement a function. I will try againGoDo it again. It is summarized here for future use.

Related Article

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.