Hi, I need help. I have been searching for a solution for past few days to no avail. As per the subject, I need to sign a string with private key and SHA256.
1) I had created a Bridging Header file and in it, i include the code
#import <CommonCrypto/CommonHMAC.h>
#import <CommonCrypto/CommonDigest.h>
2) Have added the path in the Build setting : Project-name/Project-name-Bridging-header.h
3) I used below code on Xcode 8 with Swift3 and I encountered below error messages base on below function :signString
//- Error Msg 1 : use of unresolved identifier 'CC_SHA256_DIGEST_LENGTH'
//- Error Msg 2: Use of unresolved identifier 'CC_SHA256
//- Error Msg 3: Use of unresolved identifier 'CC_LONG'
func signString(string: String, privateKey: SecKey) -> NSData? {
//-1: Error Msg : use of unresolved identifier 'CC_SHA256_DIGEST_LENGTH'
let digest = NSMutableData(length: Int(CC_SHA256_DIGEST_LENGTH))!
let stringData: NSData = string.data(using: String.Encoding.utf8)! NSData
//-2: Error Msg : Use of unresolved identifier 'CC_SHA256
//-3: Error Msg : Use of unresolved identifier 'CC_LONG'
CC_SHA256(stringData.bytes, CC_LONG(stringData.length), UnsafeMutablePointer<UInt8>(digest.mutableBytes))
let signedData: NSMutableData = NSMutableData(length: SecKeyGetBlockSize(privateKey))!
var signedDataLength: Int = signedData.length
let err: OSStatus = SecKeyRawSign(
privateKey,
SecPadding.PKCS1SHA256,
UnsafePointer<UInt8>(digest.bytes),
digest.length,
UnsafeMutablePointer<UInt8>(signedData.mutableBytes),
&signedDataLength
)
switch err {
case noErr:
return signedData
default:
return nil
}
}
Can you help me to correct the code below. your help is much appreciated.
Thanks