@@ -31,16 +31,46 @@ func validateSecretLength(secret []byte, minLength int, algorithm string) error
3131}
3232
3333// NewHS256Encoder creates a new HMAC-SHA256 JWT encoder/decoder.
34+ //
35+ // Security: The secret should be at least 256 bits (32 bytes) for HS256.
36+ // Weak secrets are vulnerable to brute-force attacks. By default, this function
37+ // enforces minimum secret length according to RFC 7518. Use NewHS256EncoderWithOptions
38+ // with allowWeakSecret=true only for testing purposes.
39+ //
40+ // Example:
41+ //
42+ // secret := []byte("my-32-byte-secret-key-for-hs256")
43+ // encoder := cryptojwt.NewHS256Encoder(secret)
44+ // token, err := encoder.Encode(`{"user":"alice","exp":1735689600}`)
45+ // if err != nil {
46+ // log.Fatal(err)
47+ // }
48+ // fmt.Println(token) // eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...
3449func NewHS256Encoder (secret []byte ) EncoderDecoder {
3550 return NewHS256EncoderWithOptions (secret , false )
3651}
3752
3853// NewHS256EncoderWithOptions creates a new HMAC-SHA256 JWT encoder/decoder with options.
54+ //
55+ // Parameters:
56+ // - secret: The shared secret key (minimum 32 bytes recommended)
57+ // - allowWeakSecret: If true, allows secrets shorter than 32 bytes (TESTING ONLY)
58+ //
59+ // Security: Setting allowWeakSecret=true bypasses RFC 7518 security requirements.
60+ // Only use this for testing with non-production data.
3961func NewHS256EncoderWithOptions (secret []byte , allowWeakSecret bool ) EncoderDecoder {
4062 return NewHS256EncoderWithValidation (secret , allowWeakSecret , ValidationOptions {})
4163}
4264
4365// NewHS256EncoderWithValidation creates a new HMAC-SHA256 JWT encoder/decoder with validation options.
66+ //
67+ // Parameters:
68+ // - secret: The shared secret key (minimum 32 bytes recommended)
69+ // - allowWeakSecret: If true, allows secrets shorter than 32 bytes (TESTING ONLY)
70+ // - validationOpts: Options for validating JWT claims (exp, nbf, iat)
71+ //
72+ // Note: By default, time-based claims (exp, nbf, iat) are NOT validated.
73+ // Set validationOpts.ValidateClaims=true to enable automatic expiration checking.
4474func NewHS256EncoderWithValidation (secret []byte , allowWeakSecret bool , validationOpts ValidationOptions ) EncoderDecoder {
4575 return & hsjwtEncoderDecoder {
4676 method : jwt .SigningMethodHS256 ,
@@ -67,6 +97,10 @@ func NewHS256DecoderWithValidation(secret []byte, allowWeakSecret bool, validati
6797}
6898
6999// NewHS384Encoder creates a new HMAC-SHA384 JWT encoder/decoder.
100+ //
101+ // Security: The secret should be at least 384 bits (48 bytes) for HS384.
102+ // Weak secrets are vulnerable to brute-force attacks. By default, this function
103+ // enforces minimum secret length according to RFC 7518.
70104func NewHS384Encoder (secret []byte ) EncoderDecoder {
71105 return NewHS384EncoderWithOptions (secret , false )
72106}
@@ -103,6 +137,10 @@ func NewHS384DecoderWithValidation(secret []byte, allowWeakSecret bool, validati
103137}
104138
105139// NewHS512Encoder creates a new HMAC-SHA512 JWT encoder/decoder.
140+ //
141+ // Security: The secret should be at least 512 bits (64 bytes) for HS512.
142+ // Weak secrets are vulnerable to brute-force attacks. By default, this function
143+ // enforces minimum secret length according to RFC 7518.
106144func NewHS512Encoder (secret []byte ) EncoderDecoder {
107145 return NewHS512EncoderWithOptions (secret , false )
108146}
@@ -138,6 +176,17 @@ func NewHS512DecoderWithValidation(secret []byte, allowWeakSecret bool, validati
138176 return NewHS512EncoderWithValidation (secret , allowWeakSecret , validationOpts )
139177}
140178
179+ // Decode validates and decodes a JWT token using HMAC algorithm.
180+ //
181+ // Security: This function validates that the token's algorithm matches the expected
182+ // HMAC algorithm to prevent algorithm confusion attacks. Tokens signed with different
183+ // algorithms will be rejected.
184+ //
185+ // Note: By default, time-based claims (exp, nbf, iat) are NOT validated. Use
186+ // NewHS*EncoderWithValidation with ValidationOptions.ValidateClaims=true to enable
187+ // automatic expiration checking.
188+ //
189+ // Returns: JSON string representation of the token's claims, or an error if validation fails.
141190func (j * hsjwtEncoderDecoder ) Decode (token string ) (string , error ) {
142191 if ! j .allowWeakSecret {
143192 if err := j .validateSecret (); err != nil {
@@ -147,6 +196,19 @@ func (j *hsjwtEncoderDecoder) Decode(token string) (string, error) {
147196 return j .decoder .DecodeJWT (j .secret , token )
148197}
149198
199+ // Encode creates and signs a JWT token using HMAC algorithm.
200+ //
201+ // The payload must be a valid JSON string that can be unmarshaled into jwt.MapClaims.
202+ //
203+ // Security: The returned token is signed but NOT encrypted. Do not include sensitive
204+ // data (passwords, API keys, PII) in the payload as it can be decoded by anyone.
205+ // Always transmit JWT tokens over HTTPS.
206+ //
207+ // Example payload:
208+ //
209+ // `{"user_id":"12345","role":"admin","exp":1735689600}`
210+ //
211+ // Returns: Base64-encoded JWT token (header.payload.signature) or an error if signing fails.
150212func (j * hsjwtEncoderDecoder ) Encode (payload string ) (string , error ) {
151213 if ! j .allowWeakSecret {
152214 if err := j .validateSecret (); err != nil {
0 commit comments