Cryptopp 是一个c++写的功能完善的密码学工具,类似于openssl

官网:https://www.cryptopp.com

以下主要演示Cryptopp 在iOS上的RSA加密解密签名与验证签名

1. 编译cryptopp为iOS上使用的静态库

   我整理好了一份 cryptopp5.6.2版本的打包脚本随后在下面DEMO中一起发布,需要可自行下载

   编译其他版本的,简单修改脚本就行

   终端运行脚本

   sh build-cryptopp.sh 

   就会生成如下目录结构,

   Cryptopp iOS 使用 RSA加密解密和签名验证签名

  生成的静态库是通用版的,真机模拟器合并的,比较大200多M,实测打包之后几百K,

  打包之后libcryptopp.a 和 include 的头文件,在ios上使用

2. 将上述打包的静态库以及 头文件导入DEMO工程CryptoppDemo

    将测试用的viewController.m 修改为 ViewController.mm

   主要为了实现 oc ,c++的混编

3. 使用 md5

    当然还支持其他hash算法   

-(NSData*)getMD5Value:(NSData*)data {
    CryptoPP::MD5 md5;
    byte digest[ CryptoPP::MD5::DIGESTSIZE ];
    
    md5.CalculateDigest(digest, (const byte*)[data bytes], [data length]);
    
    NSData * hashVale = [NSData dataWithBytes:digest length:sizeof digest];
    return hashVale;
}



    //MD5
    NSString *testStr = @"hello world md5";
    NSData *testDAT = [testStr dataUsingEncoding:NSUTF8StringEncoding];
    NSData *md5Dat = [self getMD5Value:testDAT];
    NSLog(@"%lu",md5Dat.length);
    NSMutableString *s = [NSMutableString string];
    unsigned char * hashValue = (byte *)[md5Dat bytes];
    
    int i;
    for (i = 0; i < [md5Dat length]; i++) {
        [s appendFormat:@"%02x", hashValue[i]];
    }
    NSLog(@"%@",s);
View Code

相关文章: