Skip to content

Cryptography Cheat Sheet

Commands for hashing, encryption, certificates, and TLS inspection.

Terminal window
# SHA-256 hash of a string
echo -n "hello" | shasum -a 256
# SHA-256 hash of a file
shasum -a 256 myfile.txt
# Verify a checksum file
shasum -a 256 -c checksums.sha256
# MD5 (legacy, don't use for security)
md5 myfile.txt # macOS
md5sum myfile.txt # Linux
# Generate a hash with openssl
openssl dgst -sha256 myfile.txt
Terminal window
# Random hex string (32 bytes = 256 bits)
openssl rand -hex 32
# Random base64 string
openssl rand -base64 32
# Random bytes to file
openssl rand 32 > key.bin
import secrets, os
secrets.token_hex(32) # Hex string (64 chars)
secrets.token_urlsafe(32) # URL-safe base64
os.urandom(32) # Raw bytes
Terminal window
# Encrypt a file (AES-256-CBC, password-based)
openssl enc -aes-256-cbc -pbkdf2 -in plain.txt -out encrypted.enc
# Decrypt
openssl enc -aes-256-cbc -pbkdf2 -d -in encrypted.enc -out plain.txt
# Encrypt with a key file
openssl enc -aes-256-cbc -pbkdf2 -in plain.txt -out encrypted.enc -kfile key.bin
# Inspect encrypted file
xxd encrypted.enc | head -5
from cryptography.fernet import Fernet
key = Fernet.generate_key() # Save this
f = Fernet(key)
token = f.encrypt(b"secret") # Encrypt
f.decrypt(token) # Decrypt
from cryptography.hazmat.primitives.ciphers.aead import AESGCM
import os
key = AESGCM.generate_key(bit_length=256)
gcm = AESGCM(key)
nonce = os.urandom(12) # Never reuse with same key
ct = gcm.encrypt(nonce, b"data", b"metadata")
pt = gcm.decrypt(nonce, ct, b"metadata")
Terminal window
# Generate private key (4096-bit)
openssl genpkey -algorithm RSA -pkeyopt rsa_keygen_bits:4096 -out rsa.key
# Extract public key
openssl pkey -in rsa.key -pubout -out rsa.pub
# Inspect key details
openssl pkey -in rsa.key -text -noout
Terminal window
# Generate private key
openssl genpkey -algorithm ED25519 -out ed25519.key
# Extract public key
openssl pkey -in ed25519.key -pubout -out ed25519.pub
Terminal window
# Generate Ed25519 SSH key
ssh-keygen -t ed25519 -C "you@example.com"
# Generate RSA SSH key (if Ed25519 not supported)
ssh-keygen -t rsa -b 4096 -C "you@example.com"
# View public key fingerprint
ssh-keygen -lf ~/.ssh/id_ed25519.pub
Terminal window
# Sign a file
openssl pkeyutl -sign -inkey ed25519.key -in document.txt -out document.sig
# Verify a signature
openssl pkeyutl -verify -pubin -inkey ed25519.pub \
-in document.txt -sigfile document.sig
# Sign with RSA + SHA-256 (for larger files)
openssl dgst -sha256 -sign rsa.key -out file.sig file.txt
# Verify RSA signature
openssl dgst -sha256 -verify rsa.pub -signature file.sig file.txt
Terminal window
# View a certificate file
openssl x509 -in cert.pem -text -noout
# View remote server certificate
echo | openssl s_client -connect example.com:443 2>/dev/null | \
openssl x509 -text -noout
# Check expiration date
openssl x509 -in cert.pem -noout -dates
# View certificate chain
echo | openssl s_client -connect example.com:443 -showcerts 2>/dev/null | \
grep -E "s:|i:"
# Get certificate fingerprint
openssl x509 -in cert.pem -fingerprint -sha256 -noout
Terminal window
# Self-signed certificate (EC key, 1 year)
openssl req -x509 -newkey ec -pkeyopt ec_paramgen_curve:prime256v1 \
-keyout server.key -out server.crt -days 365 -nodes \
-subj "/CN=localhost"
# Create a CSR (Certificate Signing Request)
openssl req -newkey ec -pkeyopt ec_paramgen_curve:prime256v1 \
-keyout server.key -out server.csr -nodes -subj "/CN=myapp.local"
# Sign a CSR with your CA
openssl x509 -req -in server.csr -CA ca.crt -CAkey ca.key \
-CAcreateserial -out server.crt -days 365
# Verify certificate against CA
openssl verify -CAfile ca.crt server.crt
Terminal window
# Test TLS connection
openssl s_client -connect example.com:443 </dev/null 2>/dev/null | \
grep -E "Protocol|Cipher|Verify"
# Force TLS version
openssl s_client -connect example.com:443 -tls1_3 </dev/null
openssl s_client -connect example.com:443 -tls1_2 </dev/null
# View TLS details with curl
curl -vI https://example.com 2>&1 | grep -E "TLS|SSL|subject|issuer"
# Check certificate expiration with curl
curl -vI https://example.com 2>&1 | grep "expire"
# Test with specific SNI (Server Name Indication)
openssl s_client -connect cdn.example.com:443 -servername example.com
# PBKDF2 (stdlib)
import hashlib, os
salt = os.urandom(16)
key = hashlib.pbkdf2_hmac("sha256", b"password", salt, 100_000)
# bcrypt (pip install bcrypt)
import bcrypt
hashed = bcrypt.hashpw(b"password", bcrypt.gensalt())
bcrypt.checkpw(b"password", hashed) # True
Terminal window
# HMAC-SHA256 of a string
echo -n "message" | openssl dgst -sha256 -hmac "secret-key"
# HMAC-SHA256 of a file
openssl dgst -sha256 -hmac "secret-key" myfile.txt
import hmac, hashlib
sig = hmac.new(b"secret", b"message", hashlib.sha256).hexdigest()
# Constant-time comparison (always use for secrets)
hmac.compare_digest(sig, expected_sig)
Terminal window
# Encode
echo -n "hello" | base64
openssl base64 -in file.bin -out file.b64
# Decode
echo "aGVsbG8=" | base64 -d
openssl base64 -d -in file.b64 -out file.bin
TaskCommand
Hash a fileshasum -a 256 file
Generate random keyopenssl rand -hex 32
Generate secure tokenpython3 -c "import secrets; print(secrets.token_urlsafe(32))"
Encrypt a fileopenssl enc -aes-256-cbc -pbkdf2 -in f -out f.enc
Generate Ed25519 keyopenssl genpkey -algorithm ED25519 -out key.pem
Generate SSH keyssh-keygen -t ed25519
Sign a fileopenssl pkeyutl -sign -inkey key -in f -out f.sig
Inspect remote certopenssl s_client -connect host:443
Check cert expirationopenssl x509 -in cert.pem -noout -dates
Self-signed certopenssl req -x509 -newkey ec ... -subj "/CN=localhost"
Test TLS versionopenssl s_client -connect host:443 -tls1_3
HMAC a messageecho -n msg | openssl dgst -sha256 -hmac key
PurposeUseAvoid
File integritySHA-256MD5, SHA-1
Password storageargon2id, bcryptSHA-256, plain text
Symmetric encryptionAES-256-GCMAES-ECB, DES
SignaturesEd25519RSA-1024, DSA
Key exchangeX25519, ECDH P-256RSA-2048
TLSTLS 1.3TLS 1.0, TLS 1.1
Random tokenssecrets modulerandom module
Message authHMAC-SHA256Raw hash