Golang md5 encryption

Database password is encrypted storage commonly, use md5 encrypted, here is a kind of irreversible encryption md5 encryption algorithm, the characteristics of the irreversible encryption algorithm is don’t need to use in the process of encryption key, input plaintext encrypted algorithm directly by the system after processing into a cipher, the encrypted data cannot be decrypted, only to input plaintext, And again after the same irreversible encryption algorithm processing, the same encrypted ciphertext is obtained and re-identified by the system, the real decryption.

When a user logs in, the front end passes the original password. In the background, the original password is encrypted by MD5 and compared with the database. When the user name and password are equal, the user information is returned.

func GetStringMd5(s string) string {
    md5 := md5.New()
    md5.Write([]byte(s))
    md5Str := hex.EncodeToString(md5.Sum(nil))
    return md5Str
}

The GetStringMd5 function is the MD5 encryption method, which uses the standard library crypto/ MD5.

func Login(username string, password string) (user model.User, err error) {
    err = Db.QueryRow("select id,username,realname from user where username = ? and password= ? ", username, GetStringMd5(password)).Scan(&user.Id, &user.Username, &user.Realname)
    if err != nil {
    logger.Log.Error(err)
    }
    return
}

Queries the User object assigned by the User based on the User name and MD5-encrypted password. If there is no query in this will return normally, so when using the need to first determine the user. Username is not equal to “”.

Leave a Reply

Your email address will not be published. Required fields are marked *

en_USEnglish