Skip to content

Instantly share code, notes, and snippets.

@weihanglo
Last active December 5, 2020 02:01
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save weihanglo/8fbe189b099c98c54f7509eee0f0e0c3 to your computer and use it in GitHub Desktop.
Save weihanglo/8fbe189b099c98c54f7509eee0f0e0c3 to your computer and use it in GitHub Desktop.
OpenSSL CheatSheet
# Create X509 self-signed certificate.
openssl req \
-newkey rsa:2048 -nodes -keyout cert.key \
-x509 -days 365 -out cert.pem
# Convert from PEM format to DER.
openssl pkcs12 \
-inkey cert.key -in cert.pem \
-export -out cert.pfx
# Deserialize public key.
openssl x509 -pubkey -noout -in cert.pem > cert.pub
# Deserialize private key from PKCS#12/PFX.
openssl pkcs12 -in cert.pfx -nocerts
# Create secret data
echo "hello openssl" >> secret.txt
# "Encrypt" with public key. "Decrypt" with private key.
openssl rsautl -encrypt \
-in secret.txt -out secret.enc \
-pubin -inkey cert.pub
openssl rsautl -decrypt \
-in secret.enc -inkey cert.key
# "Sign" with private key. "Verify" with public key.
openssl rsautl -sign \
-in secret.txt -out secret.sign \
-inkey cert.key
openssl rsautl -verify \
-in secret.sign -pubin -inkey cert.pub
# hash digest
openssl dgst -md5 -binary -out md5.hash secret.txt # binary
openssl dgst -md5 -binary secret.txt | xxd -p > md5.hash # hex
# HMAC
openssl dgst -sha256 -binary -hmac "rust" -out sha256.hmac secret.txt # binary
openssl dgst -sha256 -binary -hmac "rust" secret.txt | xxd -p > sha256.hmac # hex
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment