entropy

package
v1.3.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 6, 2026 License: MIT Imports: 4 Imported by: 0

Documentation

Overview

Package entropy implements password entropy calculation.

This file provides pattern-aware entropy calculation. Instead of applying a post-hoc multiplicative reduction to the pool-size estimate, it uses a segment-based model: each detected pattern segment contributes only its intrinsic entropy — the bits needed to describe the pattern choice to an attacker who already knows the pattern class — while uncovered characters contribute the standard character-pool entropy.

Package entropy implements password entropy calculation.

It estimates the entropy of a password in bits based on the character sets used and the password length, giving a measure of how unpredictable the password is.

Entropy is calculated as:

bits = runeCount × log2(poolSize)

where poolSize is the total number of possible characters based on which character sets (lowercase, uppercase, digits, symbols) are present.

Package entropy implements password entropy calculation.

This file provides Markov-chain analysis for character transition probabilities to estimate entropy more accurately by considering how characters typically follow each other in real passwords.

Package entropy implements password entropy calculation.

This file provides mode selection logic for different entropy calculation methods.

Index

Constants

View Source
const (
	PoolLower  = 26
	PoolUpper  = 26
	PoolDigit  = 10
	PoolSymbol = 32
)

Character pool sizes for each set.

Variables

This section is empty.

Functions

func Calculate

func Calculate(password string) float64

Calculate estimates the entropy of a password in bits.

Length is measured in Unicode code points (runes), not bytes, so multi-byte characters are counted correctly.

func CalculateAdvanced added in v1.3.0

func CalculateAdvanced(password string, patternIssues []issue.Issue) float64

CalculateAdvanced calculates entropy using a segment-based model.

The password is partitioned into two kinds of regions:

  1. Pattern segments (identified by the Pattern field of each issue.Issue): contribute only the intrinsic entropy of their pattern class (see intrinsicPatternEntropy).

  2. Free characters (not covered by any detected pattern): contribute the standard character-pool entropy (bits = count × log2(poolSize)).

Repeated-block patterns are counted once regardless of how many times the block repeats in the password; all repetitions are marked as covered but add no additional entropy.

Issues whose Pattern field is empty are silently ignored (e.g. issues from rule or dictionary checkers that are unrelated to structural patterns).

func CalculatePatternAware added in v1.3.0

func CalculatePatternAware(password string, patternIssues []issue.Issue) float64

CalculatePatternAware calculates entropy using pattern-aware adjustments plus Markov-chain analysis for character transition probabilities.

func CalculateWithMode added in v1.3.0

func CalculateWithMode(password, mode string, patternIssues []issue.Issue) float64

CalculateWithMode calculates entropy using the specified mode. If mode is empty or invalid, falls back to simple mode.

Types

type CharsetInfo

type CharsetInfo struct {
	HasLower  bool // at least one lowercase letter
	HasUpper  bool // at least one uppercase letter
	HasDigit  bool // at least one digit
	HasSymbol bool // at least one symbol / punctuation
}

CharsetInfo holds the results of a single-pass character set analysis.

func AnalyzeCharsets

func AnalyzeCharsets(password string) (info CharsetInfo, runeCount int)

AnalyzeCharsets performs a single pass over the password to determine which character set types are present and counts the number of runes. Uses the unicode package for correct handling of non-ASCII letters and digits.

func (CharsetInfo) PoolSize

func (c CharsetInfo) PoolSize() int

PoolSize returns the total number of possible characters based on which sets are present.

func (CharsetInfo) SetCount

func (c CharsetInfo) SetCount() int

SetCount returns how many of the four character set types are present.

type Mode added in v1.3.0

type Mode string

Mode represents the entropy calculation mode.

const (
	// ModeSimple uses the basic character-pool × length formula.
	ModeSimple Mode = "simple"

	// ModeAdvanced reduces entropy for detected patterns.
	ModeAdvanced Mode = "advanced"

	// ModePatternAware includes pattern analysis plus Markov-chain analysis.
	ModePatternAware Mode = "pattern-aware"
)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL