skip to Main Content

Hello I have an Objective-C project and I want to start using Apple’s CryptoKit framework. Is this possible?

2

Answers


  1. #include<CommonCrypto/CommonCrypto.h>
    

    Worked for me in straight C and, since Objective C is a strict superset, it should work.

    I couldn’t find any direct documentation, but the header files themselves specify the parameters.

    This is not CryptoKit, but unless you have an edge case it will work and is Apple supplied as opposed to openssl or libressl. Since OP mentioned it, I thought that was a reason for giving commoncrypto as an alternative. The actual implementation of the algorithms doesn’t differ in any significant way for most cases.

    Login or Signup to reply.
  2. Late to answer, but IMHO it is easiest to create a Swift Static Library or a Swift Framework w/ Apple’s CryptoKit (dependency) and link it to your obj-c project. Then of course you’d import the Swift Static Lib/Framework headers into your Obj-C Class.

    Framework in Xcode

    Be sure to prefix your Swift functions with @objc open for both your class and functions.

    I’d encourage you to use delegates, maybe some singleton pattern in your Swift CryptoKit Wrapper.

    You can link your Swift Framework into your obj-c classes with a readonly property:

    @property (nonatomic, readonly) CryptoKitWrapper *wrapper;
    

    Since in the Swift Wrapper Class you have them set as open/obj-c prefix the funcs are exposed.

    As a pseudo example, here is a Swift Func, and obj-c calling it.

    Obj-c:

    - (NSData *) hashSHA256: (NSData *) data
    {
        return [_wrapper SHA256hashWithData:data];
    }
    

    Swift class/func:

    @objc open class CryptoKitWrapper: NSObject  {
        public static let shared = CryptoKitWrapper()
        public var delegate:CryptoKitWrapperProtocol?
        
        @objc open func SHA256hash(data: Data) -> Data {
            let digest = SHA256.hash(data: data)
            CryptoKitWrapper.shared.delegate?.didCallSHA256Hash()
            return Data(digest);
        }
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search