请用cryptto库编写一个计算字符串MD5的函数
时间: 2023-03-13 13:06:59 浏览: 47
可以使用crypto库中的crypto.createHash()来编写一个函数,用于计算字符串的MD5值。具体的实现方法如下: const crypto = require('crypto'); function md5(str) {
let hash = crypto.createHash('md5');
hash.update(str);
return hash.digest('hex');
}
console.log(md5('hello world'));
相关问题
请用cryptto库编写一个字符串AESl加解密的函数
我可以为您提供一个使用crypto库编写的AES加解密函数的示例:const crypto = require('crypto');// 加密函数
function encrypt(key, data) {
const cipher = crypto.createCipher('aes192', key);
let crypted = cipher.update(data, 'utf8', 'hex');
crypted += cipher.final('hex');
return crypted;
}// 解密函数
function decrypt(key, data) {
const decipher = crypto.createDecipher('aes192', key);
let decrypted = decipher.update(data, 'hex', 'utf8');
decrypted += decipher.final('utf8');
return decrypted;
}
请用C语言写一个将字符串加密MD5的函数
#include<stdio.h>
#include<string.h>
#include<openssl/md5.h>// 定义一个函数,用来将字符串加密成MD5
void str2md5(const char *str, char *md5_str)
{
// 1.初始化MD5
MD5_CTX ctx;
MD5_Init(&ctx); // 2.添加要加密的信息
MD5_Update(&ctx, str, strlen(str)); // 3.结束MD5加密
unsigned char md[MD5_DIGEST_LENGTH] = {0};
MD5_Final(md, &ctx); // 4.将加密的结果转换成字符串
for (int i = 0; i < MD5_DIGEST_LENGTH; ++i)
{
// 将加密结果转换成十六进制
snprintf(&md5_str[i*2], 2+1, "%02x", md[i]);
}
}int main()
{
const char *str = "hello world!";
char md5_str[MD5_DIGEST_LENGTH*2+1] = {0}; // 将字符串加密成MD5
str2md5(str, md5_str); printf("%s\n", md5_str); return 0;
}