5

The guidance is to to use #import "CommonCrypto/CommonCrypto.h" in the bridging header. This is from the question at: SHA256 in swift.
However, when I use the answers given by Andi and Graham Xcode still complains about "use of unresolved identifier CC_SHA256_DIGEST_LENGTH..."

I am thinking I've made one of two mistakes: Either (a) I am missing something in not having wired up the header and import correctly. i.e. I did not setup the bridging header correctly. I'd love clear steps on how to include the library and create the bridging header correctly. Or (b)The library is not included by default and I actually need to download it and store locally before I can use it. I'd love instructions on that.

Thanks.

Metis
  • 51
  • 1
  • 2
  • Fow Swif 3 hashing See examples: [HMAC with MD5, SHA1, SHA224, SHA256, SHA384, SHA512](https://stackoverflow.com/documentation/swift/7885/cryptographic-hashing/25615/hmac-with-md5-sha1-sha224-sha256-sha384-sha512-swift-3#t=201706071930379421472) in the Documentation section. – zaph Jul 23 '17 at 03:24

2 Answers2

4

Good news!: Swift 4(Xcode 10) has made CommonCrypto to be available for import by default!

This may not be helpful for you at Swift 3 but still, it's just FYI

Lokesh SN
  • 1,583
  • 7
  • 23
  • While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - [From Review](/review/low-quality-posts/21089630) – UdayKiran Pulipati Oct 10 '18 at 08:41
  • 1
    Thanks @pudaykiran I've changed it – Lokesh SN Oct 10 '18 at 09:20
2

better way in 8 steps

1) -------------------------------------------

go to xcode file inspector and select your project file and add a new target.

xcode file inspector

2) -------------------------------------------

select an aggregate from cross-platform section.

aggregate

3) -------------------------------------------

after you name it appropriately, select it from targets and go to build phases section.

build phases

4) -------------------------------------------

there select the plus button and create new run script phase with following code. it will generate appropriate module for each platform just before building and you will be able to import CommonCrypto even for simulator.

add script script code

mkdir -p "${SRCROOT}/Frameworks/CommonCrypto"
cat <<EOF > "${SRCROOT}/Frameworks/CommonCrypto/module.modulemap"
module CommonCrypto [system] {
    header "${SDKROOT}/usr/include/CommonCrypto/CommonCrypto.h"
    export *
}
EOF

5) -------------------------------------------

after this step go to your project target and actually link this aggregate to your build process

linkup

6) -------------------------------------------

select the aggregate

aggregate selection

7) -------------------------------------------

now still in the project target go to build settings and find "header search paths" and insert this path to be traversed for the newly generated module

${SRCROOT}/Frameworks/CommonCrypto

header search paths

8) -------------------------------------------

now all you need to do is just

import CommonCrypto

somewhere and start using it.

hope it helps

ha100
  • 1,563
  • 1
  • 21
  • 28
  • 1
    Thanks @ha100. Sorry for the late response... just returning to the project. I am not seeing anything like "he Objective-C Bridging Header section in Build Settings." I did add the bridging header file using the naming convention you indicate. I'm using Xcode 8.3. Is there is difference between versions for this? – Metis Jul 31 '17 at 20:40
  • I am looking for a workable solution in Swift 4. I've looked everywhere and have not been successful using this solution. Any ideas? – llamacorn Jun 05 '18 at 21:07
  • @ha100 I am stucked on step 7. I couldn't find "header search paths" in my project target. any clue? – Qadir Hussain Aug 02 '18 at 07:57