Example of Message digest algorithm implemented by Python and go language

Source: Internet
Author: User
Tags sha1
Commonly used message digest algorithm has MD5 and SHA, these algorithms in the Python and go library have, need time to call under OK, here summarizes the implementation of Python and go.

Example of a Python message digest

The code is as follows:
Copy the Code code as follows:


#! /usr/bin/python
'''
File:testHash.py
Author:mike
E-mail:mike_zhang@live.com
'''
Import Hashlib
src = 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:
Copy the Code code 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 (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},
"SHA224": Func (msg []byte) hash. Hash{var h Hash. Hash = sha256. New224 (); 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:")
var MSG1 string
Fmt. SCANF ("%s", &MSG1)
For _,funcname: = Range funcnamelist{
Fmt. Printf ("%s \t:\t%x\n", Funcname,funcmap[funcname] ([]byte (MSG1)). Sum ())
}
}


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 light implementation of a function, I would like to go to do 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.