stats

package
v0.7.0 Latest Latest
Warning

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

Go to latest
Published: Jul 14, 2026 License: Apache-2.0 Imports: 3 Imported by: 0

Documentation

Overview

Package stats holds the cipher-agnostic statistical primitives the toolkit uses to triage a captured payload: is it plaintext, a weak classical cipher, an obfuscation, or strong encryption? Plus the repeating-key-XOR key-length estimators (Friedman/Hamming) and a crib-dragging helper for many-time-pad / keystream-reuse analysis.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Autocorrelation added in v0.5.8

func Autocorrelation(data []byte, maxLag int) []float64

Autocorrelation returns, for each lag 1..maxLag, the coincidence rate: the fraction of positions i where data[i] == data[i+lag]. For a uniform random byte stream this sits near 1/256 ≈ 0.0039 at every lag; a repeating-key cipher, a periodic scrambler, or a reused keystream produces sharp peaks at multiples of its period. This is the byte-level analogue of CrypTool's autocorrelation view, and the prerequisite measurement before choosing a repeating-XOR key length or a rolling-descramble frame size.

func ByteFrequency

func ByteFrequency(data []byte) [256]int

ByteFrequency returns the count of each byte value.

func ChiSquareUniform

func ChiSquareUniform(data []byte) float64

ChiSquareUniform returns the chi-square statistic of the byte distribution against a uniform 256-value model. Near (256-1)=255 for uniform/random data; large for structured data.

func HammingDistance

func HammingDistance(a, b []byte) int

HammingDistance returns the bit-difference count between equal-length byte slices. It panics if the lengths differ — callers slice first.

func IndexOfCoincidence

func IndexOfCoincidence(data []byte) float64

IndexOfCoincidence returns the probability that two bytes drawn at random (without replacement) are equal. English text over the 26-letter alphabet sits near 0.066; uniform random over 256 values near 1/256.

func ShannonEntropy

func ShannonEntropy(data []byte) float64

ShannonEntropy returns the per-byte Shannon entropy in bits (0..8). High values (~8) indicate compression or strong encryption; low values indicate structure (plaintext, weak ciphers, obfuscation).

Types

type DragHit

type DragHit struct {
	Offset    int
	Revealed  []byte
	Printable bool
}

DragHit is one crib-drag position and the plaintext fragment it reveals in the other message when two ciphertexts share a keystream (c1 ^ c2 ^ crib).

func CribDrag

func CribDrag(a, b, crib []byte) []DragHit

CribDrag slides crib across a^b (where a and b are two ciphertexts that reused the same keystream, so a^b = p_a ^ p_b) and reports, at each offset, the bytes the crib reveals in the partner plaintext. Offsets that reveal all-printable fragments are flagged — those are likely real.

type KeyLenScore

type KeyLenScore struct {
	Length int
	Score  float64
}

KeyLenScore pairs a candidate repeating-XOR key length with its normalized average Hamming distance (bits/byte); the true length tends to minimize it.

func GuessXORKeyLength

func GuessXORKeyLength(ct []byte, maxLen int) []KeyLenScore

GuessXORKeyLength ranks candidate key lengths for a repeating-key XOR ciphertext by averaging the normalized Hamming distance between adjacent key-length blocks (the Friedman/Kasiski idea). Lower score = more likely. Results are sorted best-first.

type NGram added in v0.5.8

type NGram struct {
	Bytes []byte
	Count int
}

NGram is one byte n-gram and how often it occurs.

func TopNGrams added in v0.5.8

func TopNGrams(data []byte, n, topN int) []NGram

TopNGrams returns the most frequent length-n byte sequences, highest count first, capped at topN. It is the histogram CrypTool exposes for spotting repeated blocks (ECB-style structure, repeated headers, padding).

type PeriodCandidate added in v0.5.8

type PeriodCandidate struct {
	Lag         int
	Coincidence float64
	// ZScore is how many baseline standard deviations the coincidence sits
	// above the mean coincidence across all lags — a scale-free strength.
	ZScore float64
}

PeriodCandidate is one lag whose coincidence rate stands out, the likely period (or a multiple of it) of a repeating structure.

func DetectPeriod added in v0.5.8

func DetectPeriod(data []byte, maxLag, topN int) []PeriodCandidate

DetectPeriod ranks the lags of Autocorrelation by how far their coincidence rate exceeds the across-lag baseline, returning the strongest candidates first. An empty result means no lag stood out (no detectable period within maxLag). topN <= 0 returns all peaks above the threshold.

Jump to

Keyboard shortcuts

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