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
相关推荐
2401_858286113 小时前
OS82.【Linux】设计线程池
java·linux·运维·服务器·开发语言·算法·线程池
Epiphany.5564 小时前
找到所有好字符串(记忆化搜索+KMP)(里面含KMP模板,用来入门)
算法
深圳市快瞳科技有限公司6 小时前
个体识别、行为解读、健康管理:多模态宠物AI大模型的场景化落地
人工智能·算法·计算机视觉·大模型·多模态·宠物·宠物ai识别
liulilittle6 小时前
归一化:激活函数
人工智能·算法·机器学习·llm
Mem0rin8 小时前
前缀和:一维与二维
数据结构·算法
小趴蔡ha8 小时前
12 K-Means 聚类入门:没有标签如何发现数据分组
算法·kmeans·聚类
-dzk-8 小时前
【二叉树】LC 101.对称二叉树
算法·二叉树
白狐_7988 小时前
408数据结构第6章:图——核心考点、算法对比与AOE网真题
数据结构·算法
变量未定义~8 小时前
DFS之剪枝与优化-小猫爬山、 数独、 木棒
算法
Navigator_Z8 小时前
LeetCode //C - 1187. Make Array Strictly Increasing
c语言·算法·leetcode