码迷,mamicode.com
首页 > 编程语言 > 详细

swift MD5 加密方法

时间:2014-11-20 11:39:13      阅读:237      评论:0      收藏:0      [点我收藏+]

标签:des   io   ar   os   sp   for   文件   on   bs   

引入OC类库

md5.h:

 

#import <UIKit/UIKit.h>

 

@interface Md5Controller : UIViewController

 

@end

 

md5.m:

#import "Md5Controller.h"

 

@interface Md5Controller ()

 

@end

 

@implementation Md5Controller

 

- (void)viewDidLoad {

    [super viewDidLoad];

    // Do any additional setup after loading the view.

}

 

- (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}

 

/*

#pragma mark - Navigation

 

// In a storyboard-based application, you will often want to do a little preparation before navigation

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {

    // Get the new view controller using [segue destinationViewController].

    // Pass the selected object to the new view controller.

}

*/

 

@end

桥接文件:

 

#import <CommonCrypto/CommonCrypto.h>

 

 

可以直接加在文件里面:

 

extension String {

    func md5() -> String! {

        let str = self.cStringUsingEncoding(NSUTF8StringEncoding)

        let strLen = CUnsignedInt(self.lengthOfBytesUsingEncoding(NSUTF8StringEncoding))

        let digestLen = Int(CC_MD5_DIGEST_LENGTH)

        let result = UnsafeMutablePointer<CUnsignedChar>.alloc(digestLen)

        

        CC_MD5(str!, strLen, result)

        

        var hash = NSMutableString()

        for i in 0..<digestLen {

            hash.appendFormat("%02x", result[i])

        }

        

        result.destroy()

        

        return String(format: hash)

    }

}

 

打印测试:

 

        println("hello".md5())

 

swift MD5 加密方法

标签:des   io   ar   os   sp   for   文件   on   bs   

原文地址:http://www.cnblogs.com/already/p/4110019.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!