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
相关推荐
北顾笙98011 小时前
day18-数据结构力扣
数据结构·算法·leetcode
阿Y加油吧12 小时前
LeetCode 中等难度 | 回溯法进阶题解:单词搜索 & 分割回文串
算法·leetcode·职场和发展
QH_ShareHub12 小时前
反正态分布算法
算法
王老师青少年编程12 小时前
csp信奥赛c++中的递归和递推研究
c++·算法·递归·递推·csp·信奥赛
Bczheng112 小时前
五.serialize.h中的CDataStream类
算法·哈希算法
小O的算法实验室12 小时前
2025年SEVC,考虑组件共享的装配混合流水车间批量流调度的多策略自适应差分进化算法,深度解析+性能实测
算法·论文复现·智能算法·智能算法改进
汀、人工智能12 小时前
[特殊字符] 第36课:柱状图最大矩形
数据结构·算法·数据库架构·图论·bfs·柱状图最大矩形
List<String> error_P12 小时前
蓝桥杯最后冲刺(三)
算法
样例过了就是过了13 小时前
LeetCode热题100 跳跃游戏
c++·算法·leetcode·贪心算法·动态规划
无限进步_13 小时前
【C++&string】寻找字符串中第一个唯一字符:两种经典解法详解
开发语言·c++·git·算法·github·哈希算法·visual studio