Using MD5 in Linux to implement user authentication solutions _linux

Source: Internet
Author: User
Tags md5 redis strtok

Using the MD5 function in OpenSSL, the function returns 16 bytes of unsigned char-type data, each byte in a range of 0~255,

It is formatted as 16 and is a 32-bit MD5 encoding. Note: One byte is 8 bits, which can represent 2-bit hexadecimal.

Use the login client's username to get the salt value and the encrypted password from the Redis database, and then encrypt the login client's password with the

Compare the passwords in the Redis database. The same validation passes, or the validation fails.

The password in the Redis database is stored in the format Password:salt

The user authentication algorithm is as follows:

int User_authenticate (char *username, char *password)

{

Char *SALT_PW, *salt, *PW;

Char buf[40];

Char tmp[3]={' ", md5_str[33]={'"};

unsigned char md[16];

int i;

GET_SALT_PW calls the Redis database for Password:salt

SALT_PW = GET_SALT_PW (db, username);

PW = Strtok (SALT_PW, ":");

if (!PW) {

return 0;

}

Salt = strtok (NULL, ":");

if (!salt) {

return 0;

}

strcpy (buf, password);

strcat (buf, salt);

MD5 ((const unsigned char*) buf, strlen (BUF), MD);

Transform to MD5 string

for (i = 0; i < i++) {

sprintf (tmp, "%02x", Md[i]);

strcat (MD5_STR, TMP);

}

Compare encode password using MD5

if (strcmp (char*) md5_str, pw)) {

return 0;

}

return 1;

}

Note the use of the Strtok function, and the process of converting 16 bytes of unsigned char to 32-bit hexadecimal numbers.

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.