Senthilkumar
Senthilkumar

Reputation: 2481

SecKeyRawSign and SecKeyRawVerify in swift3

I'm having a string. I want to SecKeyRawSign and SecKeyRawVerify in swift3. I'm using 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
    }    
}

Edited: I'm passing returned data from signString method to verifyString method. I'm not able to get, what string I signed earlier.

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 ""
        }


    }

But I can able to verify, If I pass the same string in the below method.

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
    }



}

Upvotes: 1

Views: 1538

Answers (1)

Vini App
Vini App

Reputation: 7485

Please check :

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
    }    
}

Upvotes: 3

Related Questions