common

package
v0.0.0-...-43498e6 Latest Latest
Warning

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

Go to latest
Published: Apr 17, 2023 License: BSD-3-Clause Imports: 18 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var EnglishUpperFreqs [256]float64

EnglishUpperFreqs contains relative frequencies of 'A' through 'Z' in English words. All other bytes are 0.

Functions

func A

func A(n int) []byte

A returns a buffer containing the byte 'A' repeated n times.

func AlphaBytes

func AlphaBytes(orig []byte) []byte

AlphaBytes returns a buffer containing only ASCII letters from orig.

func B

func B(n int) []byte

B returns a buffer containing the byte 'B' repeated n times.

func BlockSizeECB

func BlockSizeECB(f EncryptFunc) int

BlockSizeECB infers the block size used by f, an ECB function.

func BlockString

func BlockString(b []byte, bs int) string

BlockString returns a hex representation of b segmented into blocks.

func ByteFreqs

func ByteFreqs(counts [256]int) [256]float64

ByteFreqs returns a table of normalized frequencies in [0.0, 1.0] of bytes with the supplied counts.

func CountBytes

func CountBytes(buf []byte) [256]int

CountBytes returns an array containing the number of times each byte occurs in buf.

func DecryptAES

func DecryptAES(enc, key, iv []byte) []byte

DecryptAES decrypts b using AES-128 with the supplied key. If iv is non-nil CBC mode is used; otherwise ECB is used.

func DiffByteFreqs

func DiffByteFreqs(a, b [256]float64) float64

DiffByteFreqs characterizes byte frequency differences between a and b. The frequency distributions should each be normalized to sum to 1 (see ByteFreqs).

func EncryptAES

func EncryptAES(b, key, iv []byte) []byte

EncryptAES encrypts b using AES-128 with the supplied key. If iv is non-nil CBC mode is used; otherwise ECB is used.

func FirstModBlock

func FirstModBlock(f EncryptFunc, bs int) int

FirstModBlock returns the index of the first modifiable block for f, an ECB or CBC function with a fixed key and fixed prefix.

func FixedLen

func FixedLen(f EncryptFunc, bs int) int

FixedLen returns the combined length of a fixed prefix and suffix used by f, an ECB or CBC function with a fixed key.

func HMACSHA1

func HMACSHA1(msg, key []byte) []byte

HMACSHA1 implements HMAC as described at https://en.wikipedia.org/wiki/HMAC#Implementation using SHA-1 as its hash function.

func Hamming

func Hamming(a, b []byte) int

Hamming returns the Hamming distance (i.e. number of differing bits) between a and b, which must be of the same length.

func MDPadding

func MDPadding(mlen int, bo binary.ByteOrder) []byte

MDPadding computes SHA-1, MD4, etc. padding for a message of length mlen *bytes*. bo specifies the byte order used when appending the length (SHA-1 uses big-endian, while MD4 uses little-endian).

Pseudocode from https://en.wikipedia.org/wiki/SHA-1#SHA-1_pseudocode:

append the bit '1' to the message e.g. by adding 0x80 if message length is a multiple of 8 bits.
append 0 ≤ k < 512 bits '0', such that the resulting message length in bits
  is congruent to −64 ≡ 448 (mod 512)
append ml, the original message length, as a 64-bit big-endian integer.
Thus, the total length is a multiple of 512 bits.

func NextSuffixByteECB

func NextSuffixByteECB(f EncryptFunc, bs int, known []byte) byte

NextSuffixByteECB attacks f to find the next byte in a fixed suffix. The bytes decoded so far should be passed in known. f should not use a fixed prefix.

func PadPKCS7

func PadPKCS7(b []byte, bs int) []byte

PadPKCS7 returns a new buffer containing b padded to the supplied block size using PKCS#7 padding.

func PrefixLen

func PrefixLen(f EncryptFunc, bs int) int

PrefixLen returns the length of the fixed prefix used by f, an ECB or CBC function with a fixed key.

func RandBytes

func RandBytes(n int) []byte

RandBytes returns a slice of n cryptographically-secure random bytes.

func RandInt

func RandInt(max int) int

RandInt returns a cryptographically-secure random integer in the range [0, n).

func RandInt64

func RandInt64(max int64) int64

RandInt64 returns a cryptographically-secure random integer in the range [0, n).

func RandWord

func RandWord() (word string, maxLen int)

RandWord returns a randomly-chosen word from /usr/share/dict/words. The maximum length of all words is also returned.

func ReadBase64

func ReadBase64(p string) []byte

ReadBase64 reads base64 data from the file at p. It panics on error.

func ReadBase64Lines

func ReadBase64Lines(p string) [][]byte

ReadHexLines reads and decodes base64 lines from p.

func ReadHexLines

func ReadHexLines(p string) [][]byte

ReadHexLines reads and decodes hex lines from p.

func SingleByteXOR

func SingleByteXOR(enc []byte) byte

SingleByteXOR tries to find the byte that's most likely to have been used for single-byte XOR encryption of English text.

func SuffixLen

func SuffixLen(f EncryptFunc, bs int) int

SuffixLen returns the length of the fixed suffix used by f, an ECB or CBC function with a fixed key.

func Unhex

func Unhex(s string) []byte

Unhex decodes the supplied hexadecimal string, panicking on error.

func UnpadPKCS7

func UnpadPKCS7(b []byte) ([]byte, error)

UnpadPKCS7 undoes padding added by PadPKCS7.

func UpperBytes

func UpperBytes(orig []byte) []byte

UpperBytes returns a buffer where lowercase ASCII letters in orig are uppercased. All other bytes are unchanged.

func XOR

func XOR(a, b []byte) []byte

XOR xors a with b. If b is shorter than a, it is repeated.

Types

type CTR

type CTR struct {
	// contains filtered or unexported fields
}

CTR implements AES in CTR mode.

func NewCTR

func NewCTR(key []byte, nonce uint64) *CTR

func (*CTR) Process

func (c *CTR) Process(r io.Reader, w io.Writer) error

Process reads from r until EOF and writes encrypted or unencrypted data to w.

func (*CTR) Reset

func (c *CTR) Reset()

Reset resets c's block counter to 0.

type EncryptFunc

type EncryptFunc func(b []byte) []byte

EncryptFunc encrypts the supplied buffer. An additional prefix and/or suffix may be applied. The same prefix, suffix, and key are used every time.

type MT

type MT struct {
	// contains filtered or unexported fields
}

MT is a PRNG implementing the Mersenne Twister algorithm. See https://en.wikipedia.org/wiki/Mersenne_Twister for details and pseudocode.

func NewMT19937

func NewMT19937(seed uint64) *MT

NewMT19937 returns a new MT using the Mersenne prime 2^19937−1.

func (*MT) Extract

func (m *MT) Extract() uint64

Extract returns the next number.

func (*MT) Params

func (m *MT) Params() MTParams

Params returns a copy of the constant parameters used by the algorithm.

func (*MT) SetState

func (m *MT) SetState(st []uint64)

SetState replaces m's internal state with st. m's index is also reset to 0.

type MTParams

type MTParams struct {
	W     int    // word size (in number of bits)
	N     int    // degree of recurrence
	M     int    // middle word, an offset used in the recurrence relation defining the series x, 1 ≤ m < n
	A     uint64 // coefficients of the rational normal form twist matrix
	B, C  uint64 // TGFSR(R) tempering bitmasks
	D     uint64 // additional Mersenne Twister tempering bitmask
	R     int    // separation point of one word, or the number of bits of the lower bitmask, 0 ≤ r ≤ w - 1
	S, T  int    // TGFSR(R) tempering bit shifts
	U, L  int    // additional Mersenne Twister tempering bit shifts
	F     uint64 // "another parameter to the generator, though not part of the algorithm proper"
	WMask uint64 // mask for bottom w bits
}

Parameter values are listed at https://en.wikipedia.org/wiki/Mersenne_Twister.

type Score

type Score struct {
	Chars    int     // letters, digits, spaces
	FreqDiff float64 // letter frequency difference (see DiffByteFreqs)
}

func EnglishScore

func EnglishScore(b []byte) Score

EnglishScore generates an ad-hoc score for the likelihood that b contains English text.

func (*Score) Better

func (s *Score) Better(o *Score) bool

Jump to

Keyboard shortcuts

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