Skip to content

Signing Algorithms

CyberGo JWT supports 4 categories with 12 signing algorithms, covering scenarios from monolithic apps to microservice architectures.

Algorithm Overview

TypeAlgorithmsKey TypeUse Case
HMACHS256 / HS384 / HS512Symmetric keySingle apps, simple services
RSARS256 / RS384 / RS512Public/Private keyMicroservices, multi-service validation
RSA-PSSPS256 / PS384 / PS512Public/Private keyMicroservices (recommended over RSA)
ECDSAES256 / ES384 / ES512Public/Private keyHigh-performance microservices

HMAC (Symmetric Key)

HMAC uses the same key for signing and verification — the simplest approach.

Key Requirements

  • Minimum 32 bytes
  • Library auto-detects weak keys (repeated characters, sequential patterns, etc.)

Usage

go
cfg := jwt.DefaultConfig()
cfg.SecretKey = "hmac-key-that-has-at-least-32-bytes!"
cfg.SigningMethod = jwt.SigningMethodHS256 // default, can be omitted

Algorithm Selection

ConstantAlgorithmDescription
SigningMethodHS256HMAC-SHA256Recommended, balanced performance and security
SigningMethodHS384HMAC-SHA384Higher security
SigningMethodHS512HMAC-SHA512Highest security

Recommendation

HS256 is sufficient for most scenarios. Keys should be generated using cryptographically secure random methods, at least 32 bytes.

RSA (Asymmetric Key)

RSA uses a private key for signing and a public key for verification. Suitable when the verifying party doesn't need the private key.

Usage

go
cfg := jwt.DefaultConfig()
cfg.SigningMethod = jwt.SigningMethodRS256
cfg.SigningKey = rsaPrivateKey        // *rsa.PrivateKey
cfg.VerificationKey = rsaPublicKey    // *rsa.PublicKey (optional)

Verification Key

VerificationKey is optional. When not set, the library uses SigningKey for verification (internally extracts the public key from the private key).

Key Generation

go
// Generate 2048-bit RSA key pair
privateKey, err := rsa.GenerateKey(rand.Reader, 2048)
if err != nil {
    log.Fatal(err)
}
publicKey := &privateKey.PublicKey

Algorithm Selection

ConstantAlgorithmDescription
SigningMethodRS256RSA-SHA256Recommended
SigningMethodRS384RSA-SHA384Higher security
SigningMethodRS512RSA-SHA512Highest security

RSA-PSS is an improved RSA signing scheme using Probabilistic Signature Scheme (PSS) padding, offering better security than PKCS#1 v1.5. Uses the same keys as RSA.

Usage

go
cfg := jwt.DefaultConfig()
cfg.SigningMethod = jwt.SigningMethodPS256
cfg.SigningKey = rsaPrivateKey        // *rsa.PrivateKey (same key as RSA)
cfg.VerificationKey = rsaPublicKey    // *rsa.PublicKey (optional)

Recommended Replacement

RSA-PSS is more secure than RSA PKCS#1 v1.5. New projects should prefer RSA-PSS algorithms. Keys are identical to RSA — no additional key generation needed.

Algorithm Selection

ConstantAlgorithmDescription
SigningMethodPS256RSA-PSS-SHA256Recommended
SigningMethodPS384RSA-PSS-SHA384Higher security
SigningMethodPS512RSA-PSS-SHA512Highest security

ECDSA (Elliptic Curve)

ECDSA is also asymmetric but with shorter keys and better performance.

Usage

go
cfg := jwt.DefaultConfig()
cfg.SigningMethod = jwt.SigningMethodES256
cfg.SigningKey = ecdsaPrivateKey      // *ecdsa.PrivateKey
cfg.VerificationKey = ecdsaPublicKey  // *ecdsa.PublicKey (optional)

Key Generation

go
// Generate P-256 curve key pair
privateKey, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
if err != nil {
    log.Fatal(err)
}
publicKey := &privateKey.PublicKey

Algorithm Selection

ConstantAlgorithmCurveDescription
SigningMethodES256ECDSA-SHA256P-256Recommended
SigningMethodES384ECDSA-SHA384P-384Higher security
SigningMethodES512ECDSA-SHA512P-521Highest security

How to Choose

text
Monolithic app ────────→ HMAC
Microservices (same trust domain) → HMAC
Microservices (cross-service verification) → RSA, RSA-PSS, or ECDSA
Security priority ─────→ RSA-PSS (replaces RSA)
High performance ──────→ ECDSA
Short key length ──────→ ECDSA
FactorHMACRSARSA-PSSECDSA
Sign speedFastSlowerSlowerFast
Verify speedFastFastFastFast
Key length32+ bytes2048+ bits2048+ bits256+ bits
Signature lengthFixedLong (~256 bytes)Long (~256 bytes)Short (~64 bytes)
Architecture couplingTightLooseLooseLoose
SecurityHighHighHigherHigh

Security Notes

Prohibited

  • Never hardcode keys in source code
  • Never use weak keys (pure digits, repeated characters, etc.)
  • Never use the none algorithm (automatically rejected by this library)
  • HMAC keys must not be shorter than 32 bytes

Best Practices

  • Use environment variables or key management services
  • Rotate signing keys regularly
  • Use RSA or ECDSA in production
  • RSA keys should be 2048 bits or larger

Next Steps