// enums
// usage
i am able to get 64 byte signature data which is required by client to send over server but my peripheral firmware is disconnecting when writing signature which i don't want and if i want to connect again then i have to press reset button so far.
Any help will be appreciated.. thanks in advance..
@quin pls help
Post
Replies
Boosts
Views
Activity
// 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!!