brute

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: 4 Imported by: 0

Documentation

Overview

Package brute is the toolkit's generic keyspace brute-force engine: pluggable Cipher, KeySpace, and Scorer interfaces driving an ordered, resumable sweep that keeps the top-scoring candidates. It ships the classical building blocks (XOR, Caesar) and scorers (printable, English frequency, known-plaintext crib), plus the smart single-byte and repeating-key XOR solvers that decompose the otherwise-astronomical repeating-key keyspace into per-column single-byte sweeps.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ByteKeySpace

type ByteKeySpace struct {
	Len int
	// contains filtered or unexported fields
}

ByteKeySpace enumerates all keys of a fixed byte length as a big-endian odometer. Practical for 1–3 bytes; wider keys should use the smart per-column solvers below.

func (*ByteKeySpace) Next

func (k *ByteKeySpace) Next() ([]byte, bool)

func (*ByteKeySpace) Position

func (k *ByteKeySpace) Position() uint64

func (*ByteKeySpace) Seek

func (k *ByteKeySpace) Seek(p uint64)

func (*ByteKeySpace) Size

func (k *ByteKeySpace) Size() uint64

type Caesar

type Caesar struct{}

Caesar subtracts key[0] from each byte (mod 256).

func (Caesar) Decrypt

func (Caesar) Decrypt(key, ct []byte) []byte

type Candidate

type Candidate struct {
	Key       []byte
	Plaintext []byte
	Score     float64
	Position  uint64
}

Candidate is one scored decryption.

func Run

func Run(ctx context.Context, c Cipher, ks KeySpace, sc Scorer, ct []byte, opt Options) []Candidate

Run sweeps ks, decrypting ct with c and scoring with sc, returning the TopN highest-scoring candidates in descending score order. It honors ctx cancellation between keys.

func SolveCaesar

func SolveCaesar(ct []byte, sc Scorer) Candidate

SolveCaesar finds the single-byte additive (Caesar) shift best explaining ct.

func SolveRepeatingXOR

func SolveRepeatingXOR(ct []byte, keyLen int, sc Scorer) Candidate

SolveRepeatingXOR recovers a repeating-key XOR key of the given length by solving each key column independently as a single-byte XOR — the standard decomposition that turns 256^keyLen into keyLen×256.

func SolveSingleByteXOR

func SolveSingleByteXOR(ct []byte, sc Scorer) Candidate

SolveSingleByteXOR finds the single-byte XOR key best explaining ct.

func SolveVigenere

func SolveVigenere(ct []byte, keyLen int, sc Scorer) Candidate

SolveVigenere recovers a repeating additive (Vigenère) key of the given length by solving each column independently as a Caesar shift.

type Cipher

type Cipher interface {
	Decrypt(key, ct []byte) []byte
}

Cipher decrypts ciphertext under a key. Implementations are pure.

type Crib

type Crib struct {
	Crib []byte
	Base Scorer
}

Crib rewards plaintexts containing a known substring (known-plaintext). It composes a base scorer with a large bonus when the crib appears.

func (Crib) Score

func (c Crib) Score(pt []byte) float64

type English

type English struct{}

English scores a plaintext by average per-byte log-likelihood under an English letter-frequency model (case-folded). Non-letters that are printable get a small floor; non-printable bytes are penalized heavily, so gibberish keys score far below readable text.

func (English) Score

func (English) Score(pt []byte) float64

type KeySpace

type KeySpace interface {
	Next() (key []byte, ok bool)
	Position() uint64
	Seek(pos uint64)
	Size() uint64 // 0 means unknown/unbounded
}

KeySpace is an ordered, seekable enumerator over candidate keys. Ordered + seekable is what makes a sweep resumable from a checkpointed position.

type Options

type Options struct {
	TopN     int                    // keep this many best candidates (default 5)
	OnTick   func(pos, size uint64) // optional progress callback
	TickEach uint64                 // call OnTick every N keys (default 65536)
}

Options configure a Run.

type Printable

type Printable struct{}

Printable scores the fraction of bytes that are printable ASCII or common whitespace. Cheap and language-agnostic — a good first filter.

func (Printable) Score

func (Printable) Score(pt []byte) float64

type Scorer

type Scorer interface {
	Score(pt []byte) float64
}

Scorer ranks a decrypted plaintext; higher is more plausible.

type Vigenere

type Vigenere struct{}

Vigenere subtracts a repeating additive key (mod 256) — the additive counterpart of repeating-key XOR.

func (Vigenere) Decrypt

func (Vigenere) Decrypt(key, ct []byte) []byte

type XOR

type XOR struct{}

XOR repeats key over the data (single-byte key = single-byte XOR).

func (XOR) Decrypt

func (XOR) Decrypt(key, ct []byte) []byte

Jump to

Keyboard shortcuts

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