Example of the message digest algorithm implemented by Python and GO, pythongo

Source: Internet
Author: User

Example of the message digest algorithm implemented by Python and GO, pythongo

Common message digest algorithms include MD5 and SHA. These algorithms are available in the python and go libraries and can be called as needed. Here we will summarize the implementation of python and go.

I. python message digest example

The Code is as follows:
Copy codeThe Code is 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", "SHA256", "SHA384", "SHA512"]
FuncMap = {
"MD5": lambda cnt: hashlib. md5 (cnt). hexdigest (),
"SHA1": lambda cnt: hashlib. sha1 (cnt). hexdigest (),
"Shavel": lambda cnt: hashlib. shaloud (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)

Running effect:

Ii. Example of a go message digest

The Code is as follows:
Copy codeThe 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", "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.new6.(); 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 ())
}
}

Running effect:

Haha, did you find that the code structure of the above two sets of programs is the same? Actually, I want to use python to learn the go language: first, python is very lightweight to implement a function, I will try again using go. It is summarized here for future 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.