Swift MD5 Encryption

Source: Internet
Author: User

Many times we will use md5 encryption, here is swift 3.0 the implementation method:

The first new bridge file xx-Bridging-Header , the method is many, here is not introduced.

Then introduce the encryption library in the bridging file

#import <CommonCrypto/CommonDigest.h>

Create a new Swift extension class file String+extension

extension String {///MD5 Encryption///    ///-returns:32 Bit capitalFunc ss_md5 ()String {Let str= Self.cstring (using:. UTF8) Let StrLen= Cunsignedint (Self.lengthofbytes (using:. UTF8)) Let Digestlen=Int (cc_md5_digest_length)Let result = Unsafemutablepointer<cunsignedchar>.allocate (capacity:digestlen) cc_md5 (str!, StrLen, result) let hash=nsmutablestring () forIinch 0.. <Digestlen {Hash.appendformat ("%02x", Result[i])} Result.deinitialize ()returnString (Format:hash asString)}}

How to use:

Let Md5str = Str.ss_md5 ()

Swift MD5 Encryption

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.