Go string connection and MD5 encryption

Source: Internet
Author: User
Tags md5 encryption
This is a creation in Article, where the information may have evolved or changed.

There are several ways to connect a go string, and the BUF buffer method executes faster, and the method needs to contain a package that is: "Byte"

Go MD5 encryption method, here directly to a string of strings to calculate the MD5. among them, through MD5. New () Initializes a MD5 object, which is actually a hash. The hash object. The function prototype is func New() hash.Hash .

The object implements a hash. Hash sum interface: calculates the checksum. Its function func Sum(data []byte) [Size]byte is a prototype of a 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 use is to set data to nil.

The method returns a size byte array, which is a 128bit 16-byte byte array for MD5. Here, you convert a string into a byte array by []byte (), and you can also convert it through the other interfaces of byte.


Package Main

Import (
"Bytes"
"FMT"
"Crypto/md5"
"Encoding/hex"
)

Func Main () {
str: = "something"
STR2: = "Nanalihai"
BUF: = bytes. Newbufferstring (str)
Buf. Write ([]byte (STR2))
Fmt. Println (BUF. String ())//Output stitching result of two strings

H: = MD5. New ()
H.write ([]byte (BUF. String ()))//BUF is required for encryption. String ()
Fmt. Printf ("%s\n", Hex. Encodetostring (H.sum (nil)))//Output encryption result
}

The result of the operation is:

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.