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
相关推荐
leiming65 分钟前
c++ string 容器
开发语言·c++·算法
wljun7391 小时前
六、OrcaSlicer 切片之区域
算法·切片软件 orcaslicer
2401_841495641 小时前
【LeetCode刷题】跳跃游戏Ⅱ
数据结构·python·算法·leetcode·数组·贪心策略·跳跃游戏
leaves falling1 小时前
动态规划讲解
算法·动态规划
钓鱼的肝1 小时前
GESP系列(3级)小杨的储蓄
开发语言·数据结构·c++·笔记·算法·gesp
MicroTech20251 小时前
MLGO微算法科技推出人工智能与量子计算融合新成果:基于QLSS与LCHS的量子DPM算法技术
人工智能·科技·算法
AndrewHZ1 小时前
【图像处理基石】[特殊字符]圣诞特辑:10+经典图像处理算法,让你的图片充满节日氛围感!
图像处理·人工智能·opencv·算法·计算机视觉·stable diffusion·节日氛围感
艾醒2 小时前
大模型原理剖析——矩阵吸收优化:LLM推理加速的核心原理与实践
算法
艾醒2 小时前
大模型原理剖析——多头并行 + 潜变量协同:原理、应用与部署优化
算法
KingRumn2 小时前
Linux信号之信号安全
linux·算法