Swift:
func md5StringFromString(string :String) ->String{
? ? ? ? letstr = string.cString(using:String.Encoding.utf8)
? ? ? ? letstrLen =CUnsignedInt(string.lengthOfBytes(using:String.Encoding.utf8))
? ? ? ? letdigestLen =Int(CC_MD5_DIGEST_LENGTH)
? ? ? ? letresult =UnsafeMutablePointer.allocate(capacity: digestLen)
? ? ? ? CC_MD5(str!, strLen, result)
? ? ? ? lethash =NSMutableString()
? ? ? ? foriin0..< digestLen {
? ? ? ? ? ? hash.appendFormat("%02x", result[i])
? ? ? ? }
? ? ? ? result.deinitialize()
? ? ? ? returnString(format: hashasString)
? ? }
OC:
- (NSString*) md5Str
{
? ? const char *myPasswd = [self UTF8String];
? ? unsigned char md5c[16];
? ? CC_MD5(myPasswd, (CC_LONG)strlen(myPasswd), md5c);
? ? NSMutableString *md5Str = [NSMutableString string];
? ? for (int i = 0; i < 16 ; i++) {
? ? ? ? [md5Str appendFormat:@"%02x",md5c[i]];
? ? }
? ? return? [md5Str copy];
}