base64 和 md5

概览

1.base64和md5都可以通过apr-util库完成
2.base64编码通过apr_base64_encode()实现
3.md5编码通过apr_md5()实现

正文

1.安装apr库

apt-get install libaprutil1

2.base64编码

apr base64官方文档

#include <stdio.h>
#include <string.h>
#include<apr-1.0/apr_base64.h>
#include<apr-1.0/apr_md5.h>
#include<iostream>
using namespace std;

int main(){
    const unsigned char *str="hello";
    unsigned char decode_str[1000];
    int len = apr_base64_encode(decode_str,str, strlen(str));
    cout<<decode_str<<endl;
return 0;
}

编译

g++ test2.cpp -laprutil-1 -g -O0 -lpthread -lcrypt -fpermissive  -I/usr/include/apr-1.0

md5编码

apr-md5官方文档

#include <stdio.h>
#include <string.h>
#include<apr-1.0/apr_base64.h>
#include<apr-1.0/apr_md5.h>
#include<iostream>

using namespace std;

int main(){
    const char *str="hello";
    unsigned char digest[128], digest_str[128];
    int len = apr_md5(digest, str, strlen(str));
    int x, i;
    unsigned int b;
    //散列出来的哈希值每个占8位,在实际过程中,不方便查看和对比(乱码),所以实际使用过程中,总是将md5值
    //一个8位的hash拆成两个4位,并映射到字符 0~9、a~z中
    for (x = i = 0; x < 16; x++) {
        b = (digest[x] >> 4) & 15;
        digest_str[i++] = b + (b > 9 ? 'a' - 10 : '0');
        b = digest[x] & 15;
        digest_str[i++] = b + (b > 9 ? 'a' - 10 : '0');
    }
    digest_str[i] = '\0';
    cout << digest_str <<endl;
    return 0;
}

编译

g++ test2.cpp -laprutil-1 -g -O0 -lpthread -lcrypt -fpermissive  -I/usr/include/apr-1.0

ref

apr base64官方文档
apr-md5官方文档

猜你喜欢

转载自www.cnblogs.com/ishen/p/12737229.html