• linux数据库中使用MD5加密


    MD5加密算法源码下载:https://pan.baidu.com/s/1nwyN0xV

    下载完成了之后解压,得到两个文件

    环境搭建:

    1、把md5.h文件拷贝到/usr/include/目录下

    sudo cp md5.h /usr/include
    

    2、编译生成.o文件

     gcc -fPIC -o md5.o -c md5.c -lpthread -ldl
    

    3、编译生成.so文件

    gcc -shared -fPIC -o libmd5.so md5.o -lpthread -ldl

    4、把生成的.so文件拷贝都/lib/目录下,方便使用

    sudo cp libmd5.so /lib/
    

    其中我们使用到了MD5中数据加密函数

    void MD5Str(char *input, unsigned char *output);
    参数: input:要加密的数据
          output:加密后的数据

    程序mysqlite3.c如下:

      1 #include <sqlite3.h>
      2 #include <stdio.h>
      3 #include <string.h>
      4 #include "md5.h"
      5 
      6 /*定义此宏时使用回调函数查询否则只是用非回调函数查询*/
      7 //#define CALLBACK 
      8 
      9 /*定义创建表格指令IF not EXISTS:不存在,AUTOINCREMENT:自动增加主键,not NULL:不能为空*/
     10 #define CREATE "create table IF not EXISTS passwd(id integer primary key AUTOINCREMENT,username text not NULL,password text not NULL)"
     11 /*定义查询数据指令*/
     12 #define SELECT "select * from passwd where username='%s' and password='%s'"
     13 /*定义插入数据指令*/
     14 #define INSERT "insert into passwd(username,password) values('%s','%s')"
     15 
     16 /*如果查询到多行数据, 那么这个函数就会调用多次(每一行调用一次)*/
     17 int callback(void *arg, int col, char **value, char **name)
     18 {
     19     int i=0;
     20     for(i=0;i<col; i++)
     21     {
     22         printf("%s	", value[i]);
     23     }
     24     printf("
    ");
     25     return 0;
     26 }
     27 
     28 
     29 int main(void)
     30 {
     31     
     32     //1.打开数据库
     33     sqlite3 *ppdb = NULL;
     34     int ret = sqlite3_open("./passwd", &ppdb); /*我们之前要先创建一个名字叫passwd的数据库*/
     35     if(ret != SQLITE_OK)
     36     {
     37         perror("open fail");
     38         return -1;
     39     }
     40     sqlite3_exec(ppdb,CREATE,NULL,NULL,NULL); /*创建一个表格*/
     41     char temp[32];
     42     char temp1[32];
     43     char insert[strlen(temp)+strlen(temp1)+200]; /*这里的数组要给大一点,因为等下加密的时候会得到一串很长的数据*/
     44     printf("please input your username:");
     45     scanf("%s",temp);
     46     printf("please input your password:");
     47     scanf("%s",temp1);
     48     MD5Str(temp1,temp1);/*把输入的密码使用md5加密存储在数据库表格中*/
     49     printf("1111
    ");
     50     
     51     
     52     sprintf(insert,INSERT,temp,temp1);/*打包数据,准备插入到表格中*/
     53     
     54     sqlite3_exec(ppdb,insert,NULL,NULL,NULL);
     55     
     56     
     57     char username[32];
     58     char password[33];
     59     printf("input username:");
     60     scanf("%s",username);
     61     printf("input password:");
     62     scanf("%s",password);
     63     
     64     MD5Str(password,password);/*把输入的密码使用md5加密之后与数据库表格中的密码匹对*/
     65     char sql[strlen(SELECT)+strlen(username)+strlen(password)];
     66     
     67     /*打包一个字符串,将SELECT字符串放到sql中,username和password这两个变量放大SELECT中*/
     68     sprintf(sql,SELECT,username,password);
     69 
     70 
     71     #ifdef CALLBACK
     72     //回调查询
     73     char *selectsql = "select * from myname";
     74     ret = sqlite3_exec(ppdb, selectsql, callback, NULL, NULL);
     75     if(ret != SQLITE_OK)
     76     {
     77         perror("create fail");
     78         sqlite3_close(ppdb);
     79         return -1;
     80     }
     81     
     82     //非回调查询
     83     #else
     84     char **result = NULL;
     85     int row = 0;
     86     int col = 0;
     87     char *error  = NULL;
     88     ret = sqlite3_get_table(ppdb,sql,&result,&row,&col,&error);
     89     if(ret != SQLITE_OK)
     90     {
     91         perror("get table fail");
     92         return -1;
     93     }
     94     
     95     int i=0, j=0;
     96     for(i=0;i<row+1;i++)
     97     {
     98         for(j=0;j<col;j++)
     99         {
    100             printf("%s	",result[j+i*col]);
    101         }
    102         printf("
    ");
    103     }
    104     
    105     if(row > 0) /*数据匹配成功*/
    106         printf("checked OK
    ");
    107     else  /*数据匹配失败*/
    108         printf("fail
    ");
    109     sqlite3_free_table(result);//释放结果
    110     #endif
    111     
    112 
    113     sqlite3_close(ppdb);
    114     return 0;
    115 }
    View Code

    我们先要创建一个名字叫passwd数据库,如果不懂创建可以看看我的这篇文章:linux数据库环境搭建

    sqlite3 passwd
    

    接着我们编译程序

    gcc -o mysqlite3 mysqlite3.c -lsqlite3 -lmd5
    

     

    运行结果如下:

  • 相关阅读:
    (数据结构)十分钟搞定时间复杂度(算法的时间复杂度)
    深入学习二叉树
    我对工作的认识
    mysql系列纠错;
    关于普通指针转换为智能指针的一些问题
    vscode The VS Code Server failed to start
    git 分支管理
    git change_id的理解
    git 指令速查
    c++ make_shared()函数理解 (待整理)
  • 原文地址:https://www.cnblogs.com/wurenzhong/p/8321046.html
Copyright © 2020-2023  润新知