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.