【发布时间】:2019-04-11 18:53:11
【问题描述】:
我找到的唯一答案是在this,我对此并不满意。
我正在添加一个标准 MD5 转换器作为字符串扩展:
/* ###################################################################################################################################### */
/**
From here: https://stackoverflow.com/q/24123518/879365
I am not making this public, because it requires the common crypto in the bridging header.
*/
fileprivate extension String {
/* ################################################################## */
/**
- returns: the String, as an MD5 hash.
*/
var md5: String {
let str = self.cString(using: String.Encoding.utf8)
let strLen = CUnsignedInt(self.lengthOfBytes(using: String.Encoding.utf8))
let digestLen = Int(CC_MD5_DIGEST_LENGTH)
let result = UnsafeMutablePointer<CUnsignedChar>.allocate(capacity: digestLen)
CC_MD5(str!, strLen, result)
let hash = NSMutableString()
for i in 0..<digestLen {
hash.appendFormat("%02x", result[i])
}
result.deallocate()
return hash as String
}
}
它要求我将以下内容添加到我的桥接头中:
#import <CommonCrypto/CommonCrypto.h>
由于我想将此添加到一套可重复使用的工具中,我想看看是否有一种方法可以在编译时检测是否使用了通用加密库。
有没有办法将其设置为条件编译?
如果不是,这没什么大不了的;只是意味着我需要将其设置为单独的源文件。
【问题讨论】:
标签: swift string commoncrypto