Golang Md5+salt

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


Write network programming, you need to save the user name and password, to prevent misappropriation or misuse. The current approach to security is: Md5+salt.



MD5 is simply a one-way irreversible hash. If the password is hashed directly, then the hacker can get a hash value by obtaining this password, and then through the hash value dictionary (for example, MD5 password cracking site), to obtain a user's password.



Adding salt can solve this problem to some extent. The so-called salt method is adding "seasoning". The basic idea is this: when the user first provides the password (usually at the time of registration), the system automatically sprinkle some "seasoning" into this password, and then hash. When the user logs in, the system provides the user with the same "seasoning" code, then hash, and then compare the hash value, determined whether the password is correct.






When the user registers,






User input "account" and "password" (and other user information);



The system generates a "salt value" for the user;



The system connects the "salt value" and "User password" together;



Hashes the concatenated values to obtain a "hash value";



Place "Hash value 1" and "salt value" in the database respectively.






When a user logs on,






User input "account" and "password";



The system uses the user name to find the corresponding "hash value" and "Salt value";



The system connects the "salt value" and "User entered password" together;



Hash the concatenated values to get a "hash value of 2" (note that the value is calculated immediately);






Compare "hash value 1" and "hash value 2" for equality, which means the password is correct, otherwise the password is incorrect.







Golang provides a MD5 package that can be called directly. The following program demonstrates the Md5+salt encryption process that uses the current time as a salt.




Package Main


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


Func Main () {
Salt: = time. Now (). Unix ()
M5: = MD5. New ()
M5. Write ([]byte ("Mi Ma"))
M5. Write ([]byte (String (salt)))
ST: = M5. Sum (Nil)
Fmt. Println (St, Hex. Encodetostring (ST))


}



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.