HASH 哈希算法之MD5 算法

1. 哈希算法,用C++ 写的

复制代码
#include <iostream>
#include <iomanip>
#include <cstring>
#include <openssl/md5.h>
#include <stdio.h>

using namespace std;

int main()
{
    string str = "hello world";
	
    unsigned char digest[MD5_DIGEST_LENGTH];
	
    memset(digest, 0, sizeof(digest));
	
    MD5((const unsigned char*) str.c_str(), str.length(), digest);
	
    string md5_str = "";
	
    char buf[3] = {0};
	
    for (int i = 0; i < MD5_DIGEST_LENGTH; i++)
	{
        sprintf(buf, "%02x", digest[i]);
        md5_str += buf;
    }
    cout << md5_str << endl;
	
    return 0;
}

2. 编译

复制代码
alfred@ubuntu:~/c_learning/d_cplusplus_learning$ g++ hash_md5.cpp -o hash_md5 -lssl -lcrypto

3. 修改不同的输入,会产生一个对应的输出结果

每个str 字符串都有一个唯一的哈希值,可以用来登录服务器的密码,在服务器上保存你算出来的哈希值,而不是保存你真正的密码,这样就算你把服务器给黑了,你也不知道我真正的密码是多少(貌似可以暴力破解)

复制代码
string str = "hello world";

4. 运行算法,得出哈希值

这是用hello world 计算出来的哈希值

复制代码
alfred@ubuntu:~/c_learning/d_cplusplus_learning$ ./hash_md5 
e10adc3949ba59abbe56e057f20f883e
相关推荐
大柏怎么被偷了29 分钟前
【C++】哈希的应用
算法·哈希算法
血小板要健康34 分钟前
如何计算时间复杂度(上)
java·数据结构·算法
古城小栈40 分钟前
Rust Vec与HashMap全功能解析:定义、使用与进阶技巧
算法·rust
wWYy.1 小时前
详解哈希表
数据结构·算法·散列表
无望__wsk1 小时前
Python第一次作业
开发语言·python·算法
Lips6111 小时前
2026.1.25力扣刷题笔记
笔记·算法·leetcode
源代码•宸1 小时前
Leetcode—746. 使用最小花费爬楼梯【简单】
后端·算法·leetcode·职场和发展·golang·记忆化搜索·动规
南 阳1 小时前
Python从入门到精通day16
开发语言·python·算法
沉默-_-2 小时前
力扣hot100-子串(C++)
c++·学习·算法·leetcode·子串
jiaguangqingpanda2 小时前
Day29-20260125
java·数据结构·算法