Error[8]: Undefined offset: 37, File: /www/wwwroot/outofmemory.cn/tmp/plugin_ss_superseo_model_superseo.php, Line: 121
File: /www/wwwroot/outofmemory.cn/tmp/plugin_ss_superseo_model_superseo.php, Line: 473, decode(

在Linux中使用MD5实现用户验证的解决方法

使用openssl中的MD5函数,该函数返回16字节的unsigned char类型的数据,每个字节的范围都在0~255间,把

它格式化为十六进制就是32位md5编码。注:一个字节为8位,正好可以表示2位的十六进制。

使用登录客户端的用户名从Redis数据库中得到salt值和加密后的密码,然后把登录客户端的密码经过salt加密后,与

Redis数据库中的密码进行比较。相同则验证通过,否则验证失败。

Redis数据库中密码的存储格式为password:salt

用户验证算法如下:

  int user_authenticate(char *username, char *password)

  {

    char *salt_pw, *salt, *pw;

    char buf[40];

    char tmp[3]={'

    unsigned char md[16];

'}, md5_str[33]={'

    int i;

'};

    //get_salt_pw调用Redis数据库获得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 < 16; 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;

  }

[+++] [+++]

其中要注意strtok函数的使用,以及16字节的unsigned char转换为32位十六进制数的过程。

)
File: /www/wwwroot/outofmemory.cn/tmp/route_read.php, Line: 126, InsideLink()
File: /www/wwwroot/outofmemory.cn/tmp/index.inc.php, Line: 166, include(/www/wwwroot/outofmemory.cn/tmp/route_read.php)
File: /www/wwwroot/outofmemory.cn/index.php, Line: 30, include(/www/wwwroot/outofmemory.cn/tmp/index.inc.php)
Error[8]: Undefined offset: 38, File: /www/wwwroot/outofmemory.cn/tmp/plugin_ss_superseo_model_superseo.php, Line: 121
File: /www/wwwroot/outofmemory.cn/tmp/plugin_ss_superseo_model_superseo.php, Line: 473, decode(

在Linux中使用MD5实现用户验证的解决方法

使用openssl中的MD5函数,该函数返回16字节的unsigned char类型的数据,每个字节的范围都在0~255间,把

它格式化为十六进制就是32位md5编码。注:一个字节为8位,正好可以表示2位的十六进制。

使用登录客户端的用户名从Redis数据库中得到salt值和加密后的密码,然后把登录客户端的密码经过salt加密后,与

Redis数据库中的密码进行比较。相同则验证通过,否则验证失败。

Redis数据库中密码的存储格式为password:salt

用户验证算法如下:

  int user_authenticate(char *username, char *password)

  {

    char *salt_pw, *salt, *pw;

    char buf[40];

    char tmp[3]={'

    unsigned char md[16];

'}, md5_str[33]={'

    int i;

'};

    //get_salt_pw调用Redis数据库获得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 < 16; 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;

  }

[+++]

其中要注意strtok函数的使用,以及16字节的unsigned char转换为32位十六进制数的过程。

)
File: /www/wwwroot/outofmemory.cn/tmp/route_read.php, Line: 126, InsideLink()
File: /www/wwwroot/outofmemory.cn/tmp/index.inc.php, Line: 166, include(/www/wwwroot/outofmemory.cn/tmp/route_read.php)
File: /www/wwwroot/outofmemory.cn/index.php, Line: 30, include(/www/wwwroot/outofmemory.cn/tmp/index.inc.php)
在Linux中使用MD5实现用户验证的解决方法_软件运维_内存溢出

在Linux中使用MD5实现用户验证的解决方法

在Linux中使用MD5实现用户验证的解决方法,第1张

在Linux中使用MD5实现用户验证的解决方法

使用openssl中的MD5函数,该函数返回16字节的unsigned char类型的数据,每个字节的范围都在0~255间,把

它格式化为十六进制就是32位md5编码。注:一个字节为8位,正好可以表示2位的十六进制。

使用登录客户端的用户名从Redis数据库中得到salt值和加密后的密码,然后把登录客户端的密码经过salt加密后,与

Redis数据库中的密码进行比较。相同则验证通过,否则验证失败。

Redis数据库中密码的存储格式为password:salt

用户验证算法如下:

  int user_authenticate(char *username, char *password)

  {

    char *salt_pw, *salt, *pw;

    char buf[40];

    char tmp[3]={'

    unsigned char md[16];

'}, md5_str[33]={'

    int i;

'};

    //get_salt_pw调用Redis数据库获得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 < 16; 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;

  }

其中要注意strtok函数的使用,以及16字节的unsigned char转换为32位十六进制数的过程。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/yw/897389.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-15
下一篇 2022-05-15

发表评论

登录后才能评论

评论列表(0条)

保存