使用Swift

时间:2016-06-02 02:45:11

标签: ios swift security

我要制作安全应用程序,因此需要签名证书。

按照Apple文档Certificate, Key, and Trust Services Reference签署数据的唯一方法是使用SecKeyRawSign并使用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

根据Apple的API,我已经可以生成密钥对。

以下是我的函数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)
}

完成该功能后,我制作了一些UnitTest

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

这是控制台返回。 首先,我使用RSA1024bits生成密钥,看起来很有效!

所以,我打印SecKeyRawSign OSStatus,返回-50 Meaning about OSStatus,它是errSecParam。

enter image description here

希望得到一些建议!!!

0 个答案:

没有答案