Example of a message digest algorithm implemented by Python and go language _python

Source: Internet
Author: User
Tags hash md5 sha1

The commonly used message digest algorithms are MD5 and SHA, which are available in both Python and go libraries, and are OK when called, which summarizes the implementation of Python and went.

Example of a Python message digest

The code is as follows:

Copy 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)

Operation Effect:

Two, go Language message Digest sample

The code is as follows:

Copy 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 ())
}
}


Operation Effect:

Haha, is not found in the above two groups of code structure of the same ah, in fact, I just want to use Python to learn the go language: first with Python is very lightweight to achieve a function, I will consider using go to do again. Here is a summary, convenient to use later.

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.