iOS MD5加密算法

如果项目中涉及到登陆注册等。

此时就需要考虑密码的加密

iOS下的具体实现方式如下

新建一个类 继承


[objc]  view plain copy
  1. #import <Foundation/Foundation.h>  
  2. #import <CommonCrypto/CommonDigest.h>   
  3.   
  4. @interface MD5 : NSObject  
  5.   
  6.   
  7. /** 
  8. *   MD5加密,传入需要加密的字符串,不可逆 
  9. * 
  10. *  @param str 需要加密的字符串 
  11. * 
  12. *  @return 加密后字符串 
  13. */  
  14. + (NSString *)md5:(NSString *)str;  
  15.   
  16. @end  


类的实现

[objc]  view plain copy
  1. #import "MD5.h"  
  2.   
  3. @implementation MD5  
  4.   
  5. + (NSString *)md5:(NSString *)str  
  6. {  
  7.     const charchar *cStr = [str UTF8String];  
  8.     unsigned char result[16];  
  9.     CC_MD5(cStr, strlen(cStr), result); // This is the md5 call  
  10.     NSString * string =  [NSString stringWithFormat:  
  11.             @"%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",  
  12.             result[0], result[1], result[2], result[3],  
  13.             result[4], result[5], result[6], result[7],  
  14.             result[8], result[9], result[10], result[11],  
  15.             result[12], result[13], result[14], result[15]  
  16.             ];  
  17.     return [string substringWithRange:NSMakeRange(816)];  
  18. }  
  19.   
  20.   
  21.   
  22. @end  


具体使用


[objc]  view plain copy
  1. NSString *str=@"准备加密";  
  2. NSString *str1=[MD5 md5:str];  
  3. NSLog(@"str=%@,str1=%@",str,str1);  


输出结果 

TestMD5[659:20205] str=准备加密,str1=54a96da37e422e51



我在网上找一个加密工具测试一下



好了  基本可以使用了

有什么问题  可以加qq聊


苹果开发群 :414319235  欢迎加入  


版权声明:本文为博主原创文章,未经博主允许不得转载。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值