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
相关推荐
88号技师几秒前
2025年11月一区SCI-壁虎优化算法Gekko Japonicus Algorithm-附Matlab免费代码
开发语言·算法·数学建模·matlab·优化算法
浅念-9 分钟前
LeetCode 双指针题型 C++ 解题整理
开发语言·数据结构·c++·笔记·算法·leetcode·职场和发展
Mr_Xuhhh24 分钟前
LeetCode hot 100(C++版本)
c++·leetcode·哈希算法
故事和你9127 分钟前
洛谷-入门6-函数与结构体
开发语言·数据结构·c++·算法·动态规划
青瓷程序设计34 分钟前
【基于 YOLO的咖啡豆果实成熟度检测系统】+ Python+算法模型+目标检测+2026原创
python·算法·yolo
程序员Shawn34 分钟前
【机器学习 | 第七篇】- 聚类算法
算法·机器学习·聚类
地平线开发者41 分钟前
征程 6X watchdog 问题分析
算法·自动驾驶
像素猎人43 分钟前
蓝桥杯OJ716【限定第一步和最后一步爬台阶的经典例题】【动态规划】
c++·算法·动态规划
Q741_1471 小时前
每日一题 力扣 3474. 字典序最小的生成字符串 贪心 字符串 C++ 题解
c++·算法·leetcode·贪心
人道领域1 小时前
LeetCode【刷题日记】:螺旋矩阵逆向全过程,边界缩进优化
算法·leetcode·矩阵