ory/hydra · error

ErrBcryptPasswordLengthReached

ErrBcryptPasswordLengthReached

Error message

passwords are limited to a maximum length of 72 characters

What it means

ErrBcryptPasswordLengthReached is a sentinel error from ory/x hasherx returned by validateBcryptPasswordLength when a password exceeds 72 bytes, the bcrypt algorithm's hard input limit (bcrypt only uses the first 72 bytes). The hasher rejects the password up front instead of silently truncating it, so callers don't get a false sense of security.

Source

Thrown at oryx/hasherx/hasher_bcrypt.go:15

package hasherx

import (
	"context"

	"github.com/pkg/errors"
	"go.opentelemetry.io/otel"
	"go.opentelemetry.io/otel/attribute"
	"golang.org/x/crypto/bcrypt"

	"github.com/ory/x/otelx"
)

// ErrBcryptPasswordLengthReached is returned when the password is longer than 72 bytes.
var ErrBcryptPasswordLengthReached = errors.Errorf("passwords are limited to a maximum length of 72 characters")

type (
	// Bcrypt is a hasher that uses the bcrypt algorithm.
	Bcrypt struct {
		c BCryptConfigurator
	}
	// BCryptConfig is the configuration for the bcrypt hasher.
	BCryptConfig struct {
		Cost uint32 `json:"cost"`
	}
	// BCryptConfigurator is the interface that must be implemented by a configuration provider for the bcrypt hasher.
	BCryptConfigurator interface {
		HasherBcryptConfig(ctx context.Context) *BCryptConfig
	}
)

func NewHasherBcrypt(c BCryptConfigurator) *Bcrypt {
	return &Bcrypt{c: c}

View on GitHub (pinned to 4174065ffb)

Solutions

  1. Truncate or reject passwords longer than 72 bytes in your application's signup/password-change flow before hashing
  2. Enforce a maxLength=72 validation on password inputs in the UI and API
  3. Pre-hash long inputs with SHA-256 and base64-encode the digest before bcrypt if you must support arbitrary-length secrets
  4. Fall back to a hasher without the 72-byte limit (e.g. argon2id) if long passphrases are a requirement

Example fix

// before
hasher.GenerateHash(ctx, password) // panics/errors when len(password) > 72
// after
if len(password) > 72 {
    return fmt.Errorf("password must be at most 72 characters")
}
hash, err := hasher.GenerateHash(ctx, password)
Defensive patterns

Strategy: validation

Validate before calling

func validatePassword(pw string) error {
    if len(pw) > 72 {
        return fmt.Errorf("password must be at most 72 characters, got %d", len(pw))
    }
    return nil
}

Try / catch

if err := hasher.GenerateHash(ctx, pw); errors.Is(err, hasherx.ErrBcryptPasswordLengthReached) {
    // prompt user to choose a shorter password
}

Prevention

When it happens

Trigger: Calling a Bcrypt hasher's GenerateHash/ComparePassword (which calls validateBcryptPasswordLength at hasher_bcrypt.go:60) with len(password) > 72.

Common situations: Users pasting long passphrases or generated secrets (SSH keys, JWTs, base64 blobs) as passwords; apps that don't enforce a max length on signup; switching from a hasher without a limit (e.g. argon2) to bcrypt.

Related errors


AI-assisted analysis of ory/hydra@4174065ffb (2026-09-03). Data as JSON: /api/errors/08f691a60fd4e04f. Report an issue: GitHub.