Recently in learning iOS development, from a simple login, gradually resolved a network access, access control values and other issues, encountered the problem of information encryption.
As an entry point for iOS, there are several issues that need to be addressed by encrypting information:
1, iOS MD5 encryption there is no fixed function, how to use this function.
After checking the data, there are built-in functions in the OBJECT-C
2, how to introduce the function of object-c
First add the header file, on the Xode 7 project right-new file-"ios-" source-"headfile-> next set name, can be arbitrarily named, add the following code in HEAD.H:
#import <CommonCrypto/CommonDigest.h>
3, how to introduce the head file into the project
In Code7, click on the project root directory, select "Build Settings", select "All", search Swift will find "Swift Compiler–code Generation", in "objective-c bridging Header "Input" Projectname/projectname-bridging-header.h "is the head file just added.
After the head introduction is complete, expand the MD5 function and add the following code to any swift:
Stringmd5.swiftextension String { var md5:string {let cString = self.cstringusingencoding ( nsutf8stringencoding) Let length = Cunsignedint ( self.lengthofbytesusingencoding (nsutf8stringencoding) ) Let result = Unsafemutablepointer<cunsignedchar>.alloc ( Int (cc_md5_digest_length) ) cc_md5 ( cstring!, length, result) return String (format: "%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%0 2x%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]) }}
The above code found on the Internet "http://www.cocoachina.com/bbs/read.php?tid-290038.html"
You can use MD5 encryption.
print(poststring. MD5);
iOS Development questions: How do I introduce a head file in Swift?