【问题标题】:Will CommonCrypto work in my app on an iPhone device?CommonCrypto 可以在我的 iPhone 设备上的应用程序中工作吗?
【发布时间】:2011-09-15 05:19:32
【问题描述】:

this question 非常相似,我希望计算一个字符串的 MD5 哈希值以用于 API。

根据this thread on Apple Discussions,这不适用于设备:

CommonCrypto 框架不在 iPhone 上。不幸的是, iPhone Simulator 针对 Mac OS X 框架进行编译,因此可以运行 模拟器......但你不会让它为设备编译。

这样的代码还能在设备上运行吗?

#import <CommonCrypto/CommonDigest.h>

#define CC_MD5_DIGEST_LENGTH 16   /* digest length in bytes */

- (NSString *)md5:(NSString *)str { 
    const char *cStr = [str UTF8String];
    unsigned char result[CC_MD5_DIGEST_LENGTH]; 
    CC_MD5(cStr, strlen(cStr), result); 
    return [NSString stringWithFormat: @"%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X",                     
                result[0], result[1], result[2], result[3],
                result[4], result[5], result[6], result[7],
                result[8], result[9], result[10], result[11],
                result[12], result[13], result[14], result[15]];        
}

在设备上生成 MD5 哈希的最新和最佳方法是什么? 如何在 XCode 4 中添加 CommonCrypto/CommonDigtest.h?

谢谢,

【问题讨论】:

    标签: iphone objective-c ios4 hash


    【解决方案1】:

    是的,您可以在 iOS 中使用 CommonCrypto。我在 AppStore 中有一个使用 CommonCrypto 的应用。

    更多信息,使帖子更完整:

    只需导入诸如

    之类的标头
    #import <CommonCrypto/CommonDigest.h>
    #import <CommonCrypto/CommonHMAC.h>
    #import <CommonCrypto/CommonCryptor.h>
    

    和框架:Security.framework

    这是一个将 SHA1 与 NSData 结合使用的示例,只需将其替换为 MD5:

    + (NSData *)doSha1:(NSData *)dataIn
    {
        NSMutableData *macOut = [NSMutableData dataWithLength:CC_SHA1_DIGEST_LENGTH];
    
        CC_SHA1( dataIn.bytes,
                 dataIn.length,
                 macOut.mutableBytes);
    
        return macOut;
    }
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-12-13
    • 1970-01-01
    • 1970-01-01
    • 2015-08-23
    • 1970-01-01
    • 1970-01-01
    • 2018-10-06
    • 1970-01-01
    相关资源
    最近更新 更多