Post

Replies

Boosts

Views

Activity

Generate 64 bytes using ECDSA SHA 256 using pem file in XCode
// here is the code snippet func generateSignatureWithPEMFile(data: Data, privateKeyPEM: String) -> Data? { let privateKeyBIO = BIO_new(BIO_s_mem()) let privateKeyPEMData = privateKeyPEM.data(using: .utf8) _ = privateKeyPEMData?.withUnsafeBytes { buffer in BIO_write(privateKeyBIO, buffer.baseAddress, Int32(buffer.count)) } let privateKey = PEM_read_bio_PrivateKey(privateKeyBIO, nil, nil, nil) let signature = UnsafeMutablePointer.allocate(capacity: 64) var signatureLength: UInt32 = 64 let ctx = EVP_MD_CTX_new() EVP_DigestInit(ctx, EVP_sha256()) _ = data.withUnsafeBytes { buffer in EVP_DigestUpdate(ctx, buffer.baseAddress, buffer.count) } EVP_SignFinal(ctx, signature, &signatureLength, privateKey) EVP_MD_CTX_free(ctx) EVP_PKEY_free(privateKey) BIO_free(privateKeyBIO) return Data(bytes: signature, count: Int(signatureLength)) } // usage let dataToSign = Data(hex: key)! let stringDataToSign = Data(stringToSign.utf8) if let signature = generateSignatureWithPEMFile(data: dataToSign, privateKeyPEM: privateKeyPEM) { let signatureHex = signature.map { String(format: "%02x", $0) }.joined() print("Signature (Hex): (signatureHex)") peripheralsConnected!.writeValue(signature, for: characteristic, type: .withResponse) } else { print("Failed to generate the signature.") } but i am getting 70 bytes or 71 bytes or 72 bytes in the debug console @eskimo pls help me !! i am stuck on this point from many days, any help will be appreciated, thanks in advance!!
0
0
406
Oct ’23