linux md5函式使用

2021-06-05 06:03:01 字數 1363 閱讀 4551

測試環境:ubuntu 8.04

簡單介紹md5函式的使用。並用md5函式寫乙個簡單的密碼比較示例。

md5.h在ubuntu中的位置:/usr/include/openssl/md5.h,此檔案定義了md5的結構體,宣告了api函式。

密碼認證示例參考lighttpd原始碼的http認證模組。採用的密碼為password,它的md5加密後的密文,可以採用第三方工具檢視。這裡我已經用第三方工具得到了它的密文,為: 5f4dcc3b5aa765d61d8327deb882cf99

/*結構體*/

typedef struct md5state_st md5_ctx;

/*api*/

int md5_init(md5_ctx *c); //初始化乙個md5_ctx結構體

int md5_update(md5_ctx *c, const void *data, size_t len); //對data資料進行計算

int md5_final(unsigned char *md, md5_ctx *c); //將計算結果拷貝到md

#include #include #include #include #define hashlen 16

typedef unsigned char hash[hashlen];

#define hashhexlen 32

typedef char hashhex[hashhexlen+1];

static const char hex_chars = "0123456789abcdef";

char int2hex(char c)

void cvthex(hash bin, hashhex hex)

hex[hashhexlen] = '\0';}/*

* 密碼比較,相等返回0,不等返回-1

*/static int password_compare(char *password, const char *pw)

else

return 0;

}int main(int argc, char **argv)

else printf("password equal\n");

return 0;

}

編譯:gcc -o md5 md5.c -lssl

執行:./md5

md5pw = 5f4dcc3b5aa765d61d8327deb882cf99

pw = 5f4dcc3b5aa765d61d8327deb882cf99

password equal

linux md5sum 命令詳解

1 命令詳解 md5sum help usage md5sum option file print or check md5 128 bit checksums.with no file,or when file is read standard input.b,binary read in bin...

Linux md5sum校驗檔案完整性

使用場景 遠端備份大檔案,防止網路異常斷開,檔案備份不完整,使用md5校驗其完整性。1.獲取檔案md5值 root kvm 123 gitlab md5sum 1564248991 2019 07 29 11.11.0 ee gitlab backup.tar bb02bd6e4da8e612274...

5 使用區域性函式值

5 使用區域性函式值 本章介紹 認識值的角色 用差別聯合表示值 使用泛型別和型別推斷 這一章討論值 values 它是乙個術語,用於很多不同的程式語言,因此,我們應該首先定義我們的意思。當我們討論函式程式設計的概念時,將函式程式描述有輸入,並返回結果的計算。簡單來說,值既可以用作 輸入,也可以接收作...