import Foundation
import CommonCrypto
extension NSString {
//value 是AnyObject类型是因为有可能所传的值不是String类型,有可能是其他任意的类型。
func nullObject() -> Bool {
//首先判断是否为nil
if (NSNull() == self) {
//对象是nil,直接认为是空串
return true
}else{
let x = self
if x == "" || x == "(null)" || 0 == x.length {
return true
}
if x == "null" {
return true
}
if x == "<null>" {
return true
}
return false
}
}
func jwMD5() -> NSString {
let value = self;
print(value)
if value.nullObject() {
return ""
}
let cStr = value.cString(using: String.Encoding.utf8.rawValue)
let buffer = UnsafeMutablePointer<UInt8>.allocate(capacity: 16)
CC_MD5(cStr!,(CC_LONG)(strlen(cStr!)), buffer)
let md5String = NSMutableString();
for i in 0 ..< 16{
md5String.appendFormat("%02x", buffer[i])
}
free(buffer)
return md5String as NSString
}
}
//使用