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))
}