Post

Swift进行md5加密之CryptoSwift与CommonCrypto相差10倍

一、使用CommonCrypto进行MD5加密的代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import Foundation
import CommonCrypto

extension String {
    var md5: String? {
        guard let data = self.data(using: .utf8) else {
            return nil
        }
        var digest = [UInt8](repeating: 0, count: Int(CC_MD5_DIGEST_LENGTH))
        #if swift(>=5.0)
        _ = data.withUnsafeBytes { (bytes: UnsafeRawBufferPointer) in
            return CC_MD5(bytes.baseAddress, CC_LONG(data.count), &digest)
        }
        #else
        _ = self.withUnsafeBytes { bytes in
            return CC_MD5(bytes, CC_LONG(data.count), &digest)
        }
        #endif
        
        return digest.map { String(format: "%02x", $0) }.joined()
    }
}
extension Data {
    
    var md5: String {
        
        var digest = [UInt8](repeating: 0, count: Int(CC_MD5_DIGEST_LENGTH))
        #if swift(>=5.0)
        _ = self.withUnsafeBytes { (bytes: UnsafeRawBufferPointer) in
            return CC_MD5(bytes.baseAddress, CC_LONG(self.count), &digest)
        }
        #else
        _ = self.withUnsafeBytes { bytes in
            return CC_MD5(bytes, CC_LONG(self.count), &digest)
        }
        #endif
        
        return digest.map { String(format: "%02x", $0) }.joined()
    }
}

二、使用CryptoSwift进行md5加密的代码
1
2
3
4
5
6
7
8
9
10
11
12
extension String {
    var md5X1: String? {
        return self.md5()
    }
    var md5X2: String? {
        guard let data = self.data(using: .utf8) else {
            return nil
        }
        let md5Data = data.bytes.md5()
        return md5Data.toHexString()
    }
}

测试效果代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
class TestMd5VC: UIViewController {
    override func viewDidLoad() {
        super.viewDidLoad()
        self.view.backgroundColor = .white
        
        DispatchQueue.global().async { [weak self] in
            guard let self = self else {
                return
            }
            let (dis1) = self.testmd51(type: 1)
            let (dis2) = self.testmd51(type: 2)
            let (dis0) = self.testmd53()
            print("CryptoSwift与CommonCrypto效率:加密效率:\(dis1/dis0) \(dis2/dis0)")
        }
    }
    
    func testmd51(type: Int) -> (Double) {
        let startDate1 = Date().timeIntervalSince1970
        let originStr = "阿里斯顿减肥啦上岛咖啡进啦手打飞机萨达"
        let repeatCount = 999
        if type == 1 {
            for idx in 0..<repeatCount {
                let md5Str = originStr.md5X1
            }
        } else if type == 2 {
            for idx in 0..<repeatCount {
                let md5Str = originStr.md5X2
            }
        }
        let endDate1 = Date().timeIntervalSince1970
        let distance1 = endDate1-startDate1
        print("CryptoSwift\(type) md5 加密\(repeatCount)次 耗时:\(distance1)")
        return (distance1)
    }
    func testmd53() -> (Double) {
        let startDate1 = Date().timeIntervalSince1970
        let originStr = "阿里斯顿减肥啦上岛咖啡进啦手打飞机萨达"
        let repeatCount = 999
        for idx in 0..<repeatCount {
            let md5Str = originStr.md5
//            print("CommonCrypto idx: \(idx) md5Str: \(md5Str ?? "")")
        }
        let endDate1 = Date().timeIntervalSince1970
        let distance1 = endDate1-startDate1
        print("CommonCrypto md5 加密\(repeatCount)次 耗时:\(distance1)")
        return (distance1)
    }
    /**
    CryptoSwift1 md5 加密999次 耗时:0.12769389152526855
    CryptoSwift2 md5 加密999次 耗时:0.10972404479980469
    CommonCrypto md5 加密999次 耗时:0.011641979217529297
    CryptoSwift与CommonCrypto效率:加密效率:10.96840057341798 9.424861765308211
    */
}


This post is licensed under CC BY 4.0 by the author.