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
相关推荐
持续学习的程序员+116 分钟前
强化学习Q-chunking算法
算法
Polaris北33 分钟前
第二十七天打卡
开发语言·c++·算法
风吹乱了我的头发~1 小时前
Day30:2026年2月20日打卡
算法
blackicexs1 小时前
第五周第五天
算法
不吃橘子的橘猫2 小时前
《集成电路设计》复习资料2(设计基础与方法)
学习·算法·fpga开发·集成电路·仿真·半导体
halen3332 小时前
How Masters Tool Fixed My Digital Disaster
算法·均值算法·推荐算法
重生之后端学习2 小时前
78. 子集
java·数据结构·算法·职场和发展·深度优先
摸鱼仙人~2 小时前
0-1背包与完全背包:遍历顺序背后的秘密
人工智能·算法
juleskk2 小时前
2.15 复试训练
开发语言·c++·算法
那起舞的日子2 小时前
斐波那契数列
java·算法