linux資料庫中使用MD5加密

來源:互聯網
上載者:User

標籤:不能   -shared   結果   文章   打包   target   select   rcm   user   

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\t", value[i]); 23     } 24     printf("\n"); 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\n"); 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\t",result[j+i*col]);101         }102         printf("\n");103     }104     105     if(row > 0) /*資料匹配成功*/106         printf("checked OK\n");107     else  /*資料匹配失敗*/108         printf("fail\n");109     sqlite3_free_table(result);//釋放結果110     #endif111     112 113     sqlite3_close(ppdb);114     return 0;115 }
View Code

我們先要建立一個名字叫passwd資料庫,如果不懂建立可以看看我的這篇文章:linux資料庫環境搭建

sqlite3 passwd

接著我們編譯器

gcc -o mysqlite3 mysqlite3.c -lsqlite3 -lmd5

 

運行結果如下:

 

linux資料庫中使用MD5加密

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.