iOS:OC--MD5

  • 1.创建分类(类目)encryptionMD5继承自NSString
创建类目.png
  • NSString+encryptionMD5.h
#import <Foundation/Foundation.h>

@interface NSString (encryptionMD5)
//外部调用,用于字符串加密
+(NSMutableString *)stringMD5:(NSString *)string;
@end
  • NSString+encryptionMD5.m
#import "NSString+encryptionMD5.h"
#import <CommonCrypto/CommonCrypto.h>
@implementation NSString (encryptionMD5)
+(NSMutableString *)stringMD5:(NSString *)string
{
    const char *data = [string UTF8String];
    
    unsigned char result[CC_MD5_DIGEST_LENGTH];

    CC_MD5(data, (CC_LONG)strlen(data), result);
    NSMutableString *mString = [NSMutableString string];
    for (int i = 0; i < CC_MD5_DIGEST_LENGTH; i++) {
        //02:不足两位前面补0,   %02x:十六进制数
        [mString appendFormat:@"%02x",result[i]];
    }
    
    return mString;
}
@end

  • ViewController.h
#import <UIKit/UIKit.h>
//第一步: 导入 iOS DK 自带框架,用于加密(MD5,SHA)
#import <CommonCrypto/CommonCrypto.h>
@interface ViewController : UIViewController

@end
  • ViewController.m
#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    //1. 准备需要加密的字符串
//    NSString *string = @"我爱 我的 同桌";
    //2. 调用加密使用的函数
    //参数1:加密的内容 参数2:要加密内容的长度 3.加密之后的长度
//    const char *data = [string UTF8String];
//    
//    unsigned char result[CC_MD5_DIGEST_LENGTH];
//    
//    CC_MD5(data,(CC_LONG)strlen(data) , result);
//    
//    //3.创建可变字符串保存结果
//    NSMutableString *mString = [NSMutableString string];
//    
//    //4.遍历结果数组进行添加
//    for (int i = 0; i < CC_MD5_DIGEST_LENGTH; i++) {
//        [mString appendFormat:@"%02x",result[i]];
//    }
//    NSLog(@"%@",mString);
    
    
#pragma mark --- 图片进行加密
//    NSString *path = [[NSBundle mainBundle]pathForResource:@"Secret" ofType:@"jpg"];
//    NSData *data = [NSData dataWithContentsOfFile:path];
//    //加密
//    unsigned char result[CC_MD5_DIGEST_LENGTH];
//    CC_MD5((__bridge const void *)(data), (CC_LONG)data.length, result);
//    NSMutableString *mString = [NSMutableString string];
//    for (int i = 0; i < CC_MD5_DIGEST_LENGTH; i++) {
//        [mString appendFormat:@"%02x",result[i]];
//    }
//    NSLog(@"%@",mString);
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 下面是我最近两年学习OC中的一些基础知识,对于学习OC基础知识的人可能有些帮助,拿出来分享一下,还是那句话不喜勿喷...
    小小赵纸农阅读 7,639评论 1 7
  • *面试心声:其实这些题本人都没怎么背,但是在上海 两周半 面了大约10家 收到差不多3个offer,总结起来就是把...
    Dove_iOS阅读 27,437评论 30 472
  • 1.项目经验 2.基础问题 3.指南认识 4.解决思路 ios开发三大块: 1.Oc基础 2.CocoaTouch...
    阳光的大男孩儿阅读 10,492评论 0 13
  • 1. 熟悉Git的基本流程 git clone git add -A git commit -m " " git ...
    9bf19a4010ab阅读 5,659评论 0 2
  • 最近一朋友正准备跳槽,就从各处搜索整理一些基础,便于朋友复习,也便于自己复习查看. 1. 回答person的ret...
    smile丽语阅读 5,818评论 0 7

友情链接更多精彩内容