【问题标题】:SecKeyRawSign and SecKeyRawVerify in swift3swift3 中的 SecKeyRawSign 和 SecKeyRawVerify
【发布时间】:2017-09-22 05:48:10
【问题描述】:

我有一个字符串。我想在 swift3 中使用 SecKeyRawSign 和 SecKeyRawVerify。我正在使用 Xcode 8.3.3

   func signString(string: String, privateKey: SecKey) -> NSData? {
    var digest = Data(count: Int(CC_SHA256_DIGEST_LENGTH))
    let stringData: Data = string.data(using: String.Encoding.utf8)!

    _ = digest.withUnsafeMutableBytes { (digestBytes) in
        stringData.withUnsafeBytes { (stringBytes) in
            CC_SHA256(stringBytes, CC_LONG(stringData.count), digestBytes)
        }
    }

    let signedData: NSMutableData = NSMutableData(length: SecKeyGetBlockSize(privateKey))!
    var signedDataLength: Int = signedData.length

    let err: OSStatus = SecKeyRawSign(
        privateKey,
        SecPadding.PKCS1SHA256,
        [UInt8](digest),
        digest.count,
        signedData.mutableBytes.assumingMemoryBound(to: UInt8.self),
        &signedDataLength
    )
    switch err {
    case noErr:
        return signedData
    default:
        return nil
    }    
}

编辑: 我将从 signString 方法返回的数据传递给 verifyString 方法。我无法获得我之前签署的字符串。

func verifyString(signeddata: NSData,  publicKey: SecKey) -> String {

        var digest = Data(count: Int(CC_SHA256_DIGEST_LENGTH))
        let rawSignedData: Data =  signeddata as Data
        _ = digest.withUnsafeMutableBytes { (digestBytes) in
            rawSignedData.withUnsafeBytes { (stringBytes) in
                CC_SHA256(stringBytes, CC_LONG(rawSignedData.count), digestBytes)
            }
        }


        let unsignedData: NSMutableData = NSMutableData(length: SecKeyGetBlockSize(publicKey))!
        let unsignedDataLength: Int = unsignedData.length

        let err: OSStatus = SecKeyRawVerify(
            publicKey,
            SecPadding.PKCS1SHA256,
            [UInt8](digest),
            digest.count,
            unsignedData.mutableBytes.assumingMemoryBound(to: UInt8.self),
            unsignedDataLength
        )
        switch err {
        case noErr:
             let backToString2 = String(data: unsignedData as Data, encoding: String.Encoding.utf8) as String!
            return backToString2!
        default:
            return ""
        }


    }

但我可以验证,如果我在下面的方法中传递相同的字符串。

func verifyString(string: String, signature: NSData, publicKey: SecKey) -> Bool {
    var digest = Data(count: Int(CC_SHA256_DIGEST_LENGTH))
    let stringData: Data = string.data(using: String.Encoding.utf8)!

    _ = digest.withUnsafeMutableBytes { (digestBytes) in
        stringData.withUnsafeBytes { (stringBytes) in
            CC_SHA256(stringBytes, CC_LONG(stringData.count), digestBytes)
        }
    }


    let mutdata = NSMutableData(data: signature as Data)

    let err: OSStatus = SecKeyRawVerify(
        publicKey,
        SecPadding.PKCS1SHA256,
        [UInt8](digest),
        digest.count,
        mutdata.mutableBytes.assumingMemoryBound(to: UInt8.self),
        signature.length
    )
    switch err {
    case noErr:
        return true
    default:
        return false
    }



}

【问题讨论】:

  • 我试过这个,但没有成功。 var digest2 = Data(digest as Data) // var keyData = Data(count: 64) let result = digest2.withUnsafeMutableBytes {mutableBytes in SecRandomCopyBytes(kSecRandomDefault, digest2.count, mutableBytes) } CC_SHA256(stringData.bytes, CC_LONG(stringData.长度),UnsafeMutablePointer(结果))

标签: ios iphone swift3 unsafemutablepointer


【解决方案1】:

请检查:

func signString(string: String, privateKey: SecKey) -> NSData? {
    var digest = Data(count: Int(CC_SHA256_DIGEST_LENGTH))
    let stringData: Data = string.data(using: String.Encoding.utf8)!

    _ = digest.withUnsafeMutableBytes { (digestBytes) in
            stringData.withUnsafeBytes { (stringBytes) in
                CC_SHA256(stringBytes, CC_LONG(stringData.count), digestBytes)
            }
        }

    let signedData: NSMutableData = NSMutableData(length: SecKeyGetBlockSize(privateKey))!
    var signedDataLength: Int = signedData.length

    let err: OSStatus = SecKeyRawSign(
        privateKey,
        SecPadding.PKCS1SHA256,
        [UInt8](digest),
        digest.count,
        signedData.mutableBytes.assumingMemoryBound(to: UInt8.self),
        &signedDataLength
    )
    switch err {
        case noErr:
            return signedData
        default:
            return nil
    }    
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-27
    • 2017-10-25
    • 1970-01-01
    相关资源
    最近更新 更多