This is a creation in Article, where the information may have evolved or changed.
Look at the implementation code first:
Package Main
Import (
"Crypto/md5"
"Encoding/hex"
"FMT"
)
Func Main () {
H: = MD5. New ()
H.write ([]byte ("123456"))//The string to be encrypted is 123456
CIPHERSTR: = H.sum (nil)
Fmt. Println (CIPHERSTR)
Fmt. Printf ("%s\n", Hex. Encodetostring (CIPHERSTR))//Output encryption result
}
Code Input Effect:
Description
Golang's cryptographic libraries are placed in the crypto directory, where the MD5 library is in the CRYPTO/MD5 package, which mainly provides the new and sum functions.
This computes the MD5 directly to a string of strings. which through MD5. New () Initializes a MD5 object, which is actually a hash. The hash object. The function prototypes are:
New returns a new hash. Hash computing the MD5 checksum.
Func New () hash. Hash {
D: = new (Digest)
D.reset ()
Return D
}
The object implements a hash. Hash sum interface: calculates the checksum. Its function prototypes are:
Hash is the common interface implemented by all hash functions.
Type Hash Interface {
Sum appends the current hash to B and returns the resulting slice.
It does the underlying hash state.
Sum (b []byte) []byte
...
}
The Sum function is the hash. The content stored inside the hash object is checksum computed and then appended to data to form a new byte slice. So the usual way to do this is to set data to nil.
The method returns a size byte array, which is a 128bit 16-byte byte array for MD5.
Resources:
Golang calculation MD5
http://gotaly.blog.51cto.com/8861157/1403942