This is a creation in Article, where the information may have evolved or changed.
Golang Calculation MD5
1. One-way encryption
What is a one-way encryption algorithm. In short, it is a non-decrypted encryption method, also known as irreversible encryption. These academic explanations do not have to worry about it, it is a group of mathematicians, grandpa through a variety of proofs to get a way to encrypt things in this way, the Earth's existing human beings in a limited time can not be decrypted, including the person who encrypts themselves. Since the encryption itself can not be decrypted, then this perverse encryption algorithm has a Mao? What scenarios require this kind of encryption with no return?
Here we first understand the characteristics of one-way encryption: First, the non-decryption has been mentioned above, the purpose is to hide the plaintext. Second one-way encryption algorithm of the other two characteristics make it more grounded gas, the first is any two pieces of plaintext data, encryption after the ciphertext can not be the same; the second is any piece of plaintext data, after encryption, the results must always be unchanged. This allows you to try a one-way encryption algorithm when we need to characterize a unique piece of content, but when you don't want someone else to know the content or simply to hit a unique ID for a single content.
For example, if you want to cache a piece of content, in order to find its content faster, use the K-v storage method. So how do you build key? Of course there are many ways, such as time. By using the "" different plaintext encryption results "feature, we can encrypt the content in one way and then use the encrypted result as key. Another example in the user login scene, afraid of other people through the way to obtain user secrets, we can encrypt the secret into the database in one-way, after the verification of the user entered the secret of one-way encryption to ensure that it in the transmission process "invisible."
So what are the common one-way encryption algorithms? There are mainly: BASE64, MD5, SHA, HMAC, the most common of which is md5,base64. Here we mainly introduce the most commonly used MD5 algorithm. The algorithm can be used to get a 128bit value, either as a key in K-v, or to encrypt the password to ensure that it is "invisible" in transit.
2. The classic Md5api
The API for computing MD5 in the C-series typically consists of several APIs.
- Init: Get a MD5 context
- Append/update: Add clear text, can be added multiple times
- Final: Perform a calculation operation
- Digest/hex_digest: Obtains the result of the encryption, the result is a 128bit memory value or its 32 16 binary notation.
These APIs are also available in the standard library of Python, and provide a way to get clear-text computing MD5 directly from a file
3.Golang of "Crypto/md5"
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. At first glance, the use is not the same as in the past. But after reading the example found that the purpose is the same, is to create a MD5 object, and then update the plaintext, and then encrypt the output, and finally clean the battlefield.
Let's look at an example:
Package MainImport ( "CRYPTO/MD5" "FMT" "Encoding/hex")func Main(){ Md5ctx := MD5.New() Md5ctx.Write([]byte("Test MD5 Encrypto")) Cipherstr := Md5ctx.Sum(Nil) FMT.Print(Cipherstr) FMT.Print("\ n") FMT.Print(Hex.encodetostring(Cipherstr))}
Here the MD5 is calculated directly on a string of strings. which 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 prototype is func Sum (data []byte) [Size]byte
Description of the official manual here I feel a bit of a problem. The official description is: "Sum returns the MD5 checksum of the data."
By flipping through the source code, you can see that he is not verifying the data, but rather 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. Here, a string is converted to a byte array by []byte (), and the other interfaces of byte can be converted, and the Write method is described below. Hash. The stored contents within the hash object are verified and computed. And where is this storage? By Golang the code Md5.go of the standard library, you can find
type digest struct { s [ Span class= "Mi" style= "Color:rgb (0,153,153)" >4 ] uint32 x [ chunk ] byte nx int Len uint64 }
Such a type. Its contents exist in a byte array of 64 bytes.
4. Clear Text interface
hash. The hash inherits the Io.write. It can therefore be used as an input stream for content updates. The Write method has been seen above and is described in detail here. The method is from IO. Write inherits, rather, inherits an interface and then implements it. The function prototype is: write (p []byte) (n int, err error)
the contents of P are read into and calculated and deposited to the top Digest byte memory x goes in. Finally, the checksum is computed by the internal function checksum.
We can therefore consider the Write method to be the Update method in the C system. Sum is considered the final method. From the above we see a memory notation whose structure is 128bit. So how do you get its 32-byte 16-in notation? Here we can encode it using the Encodetostring method in the Encoding/hex package. The function prototype is to func EncodeToString(src []byte) string
direct the return result of sum to SRC. This allows us to make simple correspondence to quickly familiarize ourselves with Golang's MD5 interface operations:
Init() -- vs -- MD5.New()Update() -- vs -- Md5ctx.Write()Final() -- vs -- Md5ctx.Sum(Nil)hex_digest() -- vs -- Hex.encodetostring()
With these interfaces, it is convenient for you to package a file to calculate the MD5 value of the interface.
Original link: http://blog.csdn.net/cz_it/article/details/24733773