I'm learning some OpenSSL RSA usage. I noticed that there are two different ways of generating and verifying file signatures. One by using openssl-dgst(1) and the other using openssl-pkeyutl(1) and they both seem to verify, accept private and public certificates, output signature files, accept algorithms, but they are not interchangeable. A signature generated by openssl-pkeyutl will not work in verification as a signature with openssl-dgst, and the opposite seems to be true as well. Even though both signature files have similar data structure formats.
Note: this is regarding RSA certificates specifically. I am not completely sure if the same possibility exists with X.509 certificates.
Question: Which is the more proper usage? openssl-pkeyutl or openssl-dgst?
Method 1: openssl dgst
# directory/
# test.txt - File to create a signature for
# cert.pem - Private/Public RSA Key, encrypted with hmacWithSHA256 via PKCS#8
# cert.pub.pem - Public Key of cert.pem extracted with `openssl rsa -pubout...`
# test.sig - File signature created by OpenSSL
# Create test.sig
$ openssl dgst -sha256 -sign cert.pem -out test.sig test.txt
Enter pass phrase for cert.pem: test
# Verify with the private key
$ openssl dgst -sha256 -verify cert.pub.pem -signature test.sig test.txt
Verified OK
# Verify with the public key
$ openssl dgst -sha256 -prverify cert.pem -signature test.sig test.txt
Enter pass phrase for cert.pem: test
Verified OK
Method 2: openssl pkeyutl
# directory/
# test.txt - File to create a signature for
# cert.pem - Private/Public RSA Key, encrypted with hmacWithSHA256 via PKCS#8
# cert.pub.pem - Public Key of cert.pem extracted with `openssl rsa -pubout...`
# test.sig - File signature created by OpenSSL
# Create test.sig
$ openssl pkeyutl -sign -in test.txt -out test.sig -inkey cert.pem
Enter pass phrase for cert.pem: test
# Verify with the private key
$ openssl pkeyutl -verify -sigfile test.sig -in test.txt -inkey cert.pub.pem -pubin
Signature Verified Successfully
# Verify with the public key
$ openssl pkeyutl -verify -sigfile test.sig -in test.txt -inkey cert.pem
Enter pass phrase for cert.pem: test
Signature Verified Successfully