0

I'm going to make a security App, so sign certificate is necessary.

Follow the Apple document Certificate, Key, and Trust Services Reference the only way to Sign a Data is to use SecKeyRawSign and verify data with SecKeyRawVerify

func SecKeyRawSign(_ key: SecKey, _ padding: SecPadding, _ dataToSign: UnsafePointer, _ dataToSignLen: Int, _ sig: UnsafeMutablePointer, _ sigLen: UnsafeMutablePointer) -> OSStatus

func SecKeyRawVerify(_ key: SecKey, _ padding: SecPadding, _ signedData: UnsafePointer, _ signedDataLen: Int, _ sig: UnsafePointer, _ sigLen: Int) -> OSStatus

According to the API from Apple, I already can generate key pair.

The below is my function called getSignature()

func getSignatureBytes(plainText: String) {
    var result = [UInt8](count: Int(CC_SHA1_DIGEST_LENGTH), repeatedValue: 0)
    var resultLength = result.count

    let SignData = [UInt8](plainText.utf8)
    let SignLength = SignData.count

    let status = SecKeyRawSign(privateKey!, SecPadding.PKCS1SHA1, SignData, SignLength, &result, &resultLength)
    let status2 = SecKeyRawVerify(publicKey!, SecPadding.PKCS1SHA1, result, Int(CC_SHA1_DIGEST_LENGTH), &result, resultLength)
}

After I finished the function, I make some UnitTest

func test_EC() {
    let b = TestEC()
    b.GenerateKeyPair()
    print(b.GetKeyTypeInKeyChain("tagPrivate"))
    b.getSignatureBytes("sdljaorjgpoa")
}

Here is the console return. At first, I generate key with RSA1024bits, it seems work!

So, I print the SecKeyRawSign OSStatus, return -50 Meaning about OSStatus , it is errSecParam.

enter image description here

Hope to get some suggestions!!!

Community
  • 1
  • 1
HungCLo
  • 442
  • 2
  • 6
  • 21

0 Answers0