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
相关推荐
big_rabbit050211 分钟前
[算法][力扣101]对称二叉树
数据结构·算法·leetcode
美好的事情能不能发生在我身上32 分钟前
Hot100中的:贪心专题
java·数据结构·算法
2301_821700531 小时前
C++编译期多态实现
开发语言·c++·算法
xixihaha13241 小时前
C++与FPGA协同设计
开发语言·c++·算法
小小怪7502 小时前
C++中的函数式编程
开发语言·c++·算法
xixixiLucky2 小时前
编程入门算法题---小明爬楼梯求爬n层台阶一共多少种方法
算法
剑锋所指,所向披靡!2 小时前
数据结构之线性表
数据结构·算法
m0_672703314 小时前
上机练习第49天
数据结构·算法
样例过了就是过了4 小时前
LeetCode热题100 N 皇后
数据结构·c++·算法·leetcode·dfs·深度优先遍历
霖大侠4 小时前
CPAL: Cross-Prompting Adapter With LoRAs forRGB+X Semantic Segmentation
人工智能·深度学习·算法·机器学习·transformer