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.