I
I
Ivan Kolesnik2016-11-09 18:32:37
Hashing
Ivan Kolesnik, 2016-11-09 18:32:37

Get md5 in swift?

You need to get the MD5 strings. View extension written

import Foundation
import CommonCrypto // ERROR : NO SUCH MODULE 'CommonCrypto'

extension Data {
    func hexString() -> String {
        let string = self.map{Int($0).hexString()}.joined()
        return string
    }
    
    func MD5() -> Data {
        var result = Data(count: Int(CC_MD5_DIGEST_LENGTH))
        _ = result.withUnsafeMutableBytes {resultPtr in
            self.withUnsafeBytes {(bytes: UnsafePointer<UInt8>) in
                CC_MD5(bytes, CC_LONG(count), resultPtr)
            }
        }
        return result
    }
    
}

extension String {
    var md5 : String {
        return self.data(using: .utf8)!.MD5().hexString()
    }
}

Of course, imports should have been added to BridgingHeader. Done: And connect the header file itself:
#import <CommonCrypto/CommonCrypto.h>
Objective-C Bridging Header - Parh/To/Header/File.h

At the output I get an error:
NO SUCH MODULE 'CommonCrypto'
How to fight? Thank you.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Rou1997, 2016-11-09
@dart_kinselok

import не нуженwhen there is #importan Objective-C header file.
More about CommonCrypto: iosdeveloperzone.com/2014/10/03/using-commoncrypto...
Couldn't you have guessed to remove it yourself? Yes, and you could go to this link yourself .

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question