Golang's MD5 encryption

Source: Internet
Author: User
Tags md5 encryption
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

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.