How do you test code that uses signing and verification?

Testing code that uses signing and verification can be crucial for ensuring the authenticity and integrity of the data. In this example, we will demonstrate how to implement a simple signing and verification process using PHP. You can test the code by signing a message and then verifying the signature using the public key.

<?php // Generate a new private (and public) key pair $privkey = openssl_pkey_new(); // Extract the private key from $privkey to $privateKey openssl_pkey_export($privkey, $privateKey); // Get the public key $keyDetails = openssl_pkey_get_details($privkey); $publicKey = $keyDetails['key']; // The message to be signed $message = "Hello, World!"; // Sign the message openssl_sign($message, $signature, $privateKey, OPENSSL_ALGO_SHA256); // Verify the signature $verified = openssl_verify($message, $signature, $publicKey, OPENSSL_ALGO_SHA256); if ($verified === 1) { echo "Signature is valid."; } elseif ($verified === 0) { echo "Signature is invalid."; } else { echo "Error verifying signature."; } ?>

signing verification PHP cryptography secure messaging