Use MD5 to encrypt strings in IOS _ios

Source: Internet
Author: User
Tags md5

1, new nsstring category, remember to add the header file required for encryption #import <CommonCrypto/CommonDigest.h>, Nsstring+md5.h:

#import <Foundation/Foundation.h>
#import <CommonCrypto/CommonDigest.h>

@interface nsstring ( MD5)
+ (NSString *) Md5to32bit: (NSString *) str;
@end

2, write the encryption method:

#import "Nsstring+md5.h"

@implementation nsstring (MD5)

+ (NSString *) Md5to32bit: (NSString *) str {
  
  const char *cstr = [str utf8string];
  unsigned char digest[cc_md5_digest_length];
  CC_MD5 (CStr, strlen (CStr), digest);
  nsmutablestring *result = [nsmutablestring stringwithcapacity:cc_md5_digest_length * 2];
  for (int i = 0; i < cc_md5_digest_length i++)
    [result appendformat:@ "%02x", Digest[i]];
  return result;
}

3, import the header file into the current controller of the string character that needs to be encrypted, and reference the method:

#import "ViewController.h"
#import "nsstring+md5.h"

@interface viewcontroller ()

@end

@ Implementation Viewcontroller

-(void) viewdidload {
  [super viewdidload];
 
  NSString *name = @ "Kuture";
  
  Encrypted string
  nsstring *encypt = [NSString md5to32bit:name];
  NSLog (@ \ Name:%@\n encypt:%@ ", name,encypt);
  
}

@end

The results of the operation are:

The above is the entire content of this article, I hope you can enjoy.

Related Article

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.