mahcha

package module
v0.0.101 Latest Latest
Warning

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

Go to latest
Published: Aug 4, 2025 License: MIT Imports: 17 Imported by: 0

README

mahcha

Build Status

mahcha cipher algorythm

Documentation

Index

Constants

View Source
const (
	Low Profile = iota
	Medium
	High
	Max
	Custom

	Noise ProfileType = iota
	Mask
	Round
	FeistelRound
	MinSkip          = 5731
	MaxSkip          = 207825
	BlockSize        = 16                              // the size of a Block
	HeaderLength     = 80                              // The Header length
	HeaderInitLength = 8                               // The HeaderInitBlock length
	HeaderDataLength = HeaderLength - HeaderInitLength // The HeaderDataBlock length
	KeyLength        = 32                              // The MahChaKey length
	NonceLength      = 12                              // The MahChaNonce length
	KinMin           = uint32(1220000000)              // The Key Initializer min value
	KinMax           = uint32(4294967295)              // The Key Initializer max value
)

Variables

View Source
var UseSIMD = haveSSE2

useSIMD peut être désactivé manuellement pour forcer le fallback Go

Functions

func AddPadding

func AddPadding(data []byte, size int) []byte

Add padding of specified size to data if needed.

func BlockToBytes

func BlockToBytes(block Block) []byte

Convert specified block to []byte.

func BytesToUint16 added in v0.0.95

func BytesToUint16(data [2]byte) uint16

Convert specified []byte data as uint64 (using little endian).

func BytesToUint32 added in v0.0.95

func BytesToUint32(data [4]byte) uint32

Convert specified []byte data as uint64 (using little endian).

func BytesToUint64

func BytesToUint64(data []byte) uint64

Convert specified []byte data as uint64 (using little endian).

func FloatToTime added in v0.0.75

func FloatToTime(f float64) time.Time

func GetBytesAsHex added in v0.0.7

func GetBytesAsHex(key []byte) []byte

func GetBytesFromHex added in v0.0.7

func GetBytesFromHex(key []byte) []byte

func GetFileSize added in v0.0.7

func GetFileSize(path string) (int64, error)

func Int64To8Bytes added in v0.0.7

func Int64To8Bytes(val int64) [8]byte

Convert specified uint64 to [8]byte (using little endian).

func RandomBytes

func RandomBytes(size int) []byte

Generate random []byte of specified size using crypto/rand.

func SecondsSince added in v0.0.75

func SecondsSince(t, start time.Time) float64

func Sha256Sum added in v0.0.7

func Sha256Sum(path string) [32]byte

func ShrinkPadding

func ShrinkPadding(data []byte, size int) []byte

Shrink padding on data at specified size.

func To2Bytes added in v0.0.5

func To2Bytes(b []byte) [2]byte

Convert specified []byte to [2]byte. Emit a warning if b length is less than 2.

func To4Bytes added in v0.0.5

func To4Bytes(b []byte) [4]byte

Convert specified []byte to [4]byte. Emit a warning if b length is less than 4.

func To8Bytes added in v0.0.5

func To8Bytes(b []byte) [8]byte

Convert specified []byte to [8]byte. Emit a warning if b length is less than 8.

func Uint16To2Bytes added in v0.0.5

func Uint16To2Bytes(val uint16) [2]byte

Convert specified uint32 to [4]byte (using little endian).

func Uint16ToBytes added in v0.0.5

func Uint16ToBytes(val uint16) []byte

Convert specified uint16 to []byte (using little endian).

func Uint32To4Bytes added in v0.0.5

func Uint32To4Bytes(val uint32) [4]byte

Convert specified uint32 to [4]byte (using little endian).

func Uint32ToBytes added in v0.0.5

func Uint32ToBytes(val uint32) []byte

Convert specified uint32 to []byte (using little endian).

func Uint64To8Bytes added in v0.0.5

func Uint64To8Bytes(val uint64) [8]byte

Convert specified uint64 to [8]byte (using little endian).

func Uint64ToBytes

func Uint64ToBytes(val uint64) []byte

Convert specified uint64 to []byte (using little endian).

func Xor8Bytes added in v0.0.59

func Xor8Bytes(a, b [8]byte) [8]byte

Types

type Block

type Block [16]byte

Block tyoe definition

func NewBlock

func NewBlock(data []byte) Block

Return a new Block with specified data. It panics if size of data is not BlockSize

func (*Block) FromBytes

func (b *Block) FromBytes(data []byte)

Fill the block with specified []byte data.

func (Block) ToBytes

func (b Block) ToBytes() []byte

Convert data to bytes using little endian.

type Blocks

type Blocks []Block

func NewBlocks

func NewBlocks(data []byte) (Blocks, int)

Return new Blocks of []byte data and original size of data.

func (*Blocks) Append

func (b *Blocks) Append(block Block)

Append specified block.

func (*Blocks) AppendBytes

func (b *Blocks) AppendBytes(data []byte)

Append specified []byte data convert as Block using little endian. It panics if len(data) don't fit BlockSize.

func (*Blocks) FromBytes

func (b *Blocks) FromBytes(data []byte) int

Fill Block with given []byte data and return original size.

func (Blocks) ToBytes

func (b Blocks) ToBytes(size int) []byte

Convert data to []byte (little endian) with specified size.

type ChaCha20Source

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

Struct to represent a ChaCha20 source. This source has 2 internal counters state wich simulate a 64 bits counter. This means it can generates 2^64 blocks of 64 random bytes, so approximatively 1,048,576 Go (1 Po) of random data for a same key and nonce.

func NewChaCha20Source

func NewChaCha20Source() (*ChaCha20Source, error)

Return a new ChaCha20Source initialized with a new key and a new nonce generated by crypto/rand.

func NewChaCha20SourceFrom

func NewChaCha20SourceFrom(key Key, nonce Nonce) (*ChaCha20Source, error)

Return a new ChaCha20Source initialized with specified key and nonce.

func (*ChaCha20Source) Uint64

func (s *ChaCha20Source) Uint64() uint64

Generate a random uint64. If internal counterLo overflow, it switch to the counterHi wich simulate a 64 bits counter.

type Duration added in v0.0.75

type Duration struct {
	Start float64
	End   float64
}

func (*Duration) Elapsed added in v0.0.75

func (d *Duration) Elapsed() float64

func (*Duration) EndTime added in v0.0.75

func (d *Duration) EndTime() time.Time

func (*Duration) SetEnd added in v0.0.75

func (d *Duration) SetEnd()

func (*Duration) StartTime added in v0.0.75

func (d *Duration) StartTime() time.Time

type Feistel

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

Convenient Struct to represent a Feistel Cipher.

func NewFeistel

func NewFeistel(masterKey FeistelMasterKey, round int) *Feistel

Return a new Feistel with specified masterKey and round. Internal keys will be derive from the masterKey using an Hkdf. Increasing round enhance diffusion.

func (*Feistel) Decrypt

func (f *Feistel) Decrypt(cipher Block) Block

Decrypt specified cipher block.

func (*Feistel) DecryptMeasure added in v0.0.76

func (f *Feistel) DecryptMeasure(cipher Block) Block

Decrypt specified cipher block and measure each specific operations.

func (*Feistel) Encrypt

func (f *Feistel) Encrypt(block Block) Block

Encrypt specified block.

func (*Feistel) EncryptMeasure added in v0.0.76

func (f *Feistel) EncryptMeasure(block Block) Block

Encrypt specified block and measure each specific operations.

type FeistelKey

type FeistelKey []byte // FeistelKey type definition

type FeistelMasterKey

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

Struct to represent a FeistelMasterKey, with salt, info and an hash function.

func NewFeistelMasterKey

func NewFeistelMasterKey(rand *Randomizer, info []byte) FeistelMasterKey

Return a new FeistelMasterKey with key and salt generated by specified Randomizer. The info parameter can be nil.

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

Struct to represent an Header. It's equivalent to an HeaderBlock, but with convenient types.

func NewHeaderFromFile added in v0.0.5

func NewHeaderFromFile(path string, profile ProfileType, split int) Header

Return a Header for specified file paht, profile type and split number.

func (Header) Kin added in v0.0.5

func (h Header) Kin() uint32

Get the Header kin.

func (Header) Pos added in v0.0.5

func (h Header) Pos() uint32

Get the pos of Header Data Block

func (Header) Profile added in v0.0.5

func (h Header) Profile(profile ProfileType) ProfileStore

Get related ProfileStore for specified profile.

func (Header) ProfilesToSeq added in v0.0.5

func (h Header) ProfilesToSeq() [16]byte

Convert Profiles to seq as [16]byte

func (Header) Reserved added in v0.0.15

func (h Header) Reserved() [8]byte

Get the split number

func (Header) Split added in v0.0.5

func (h Header) Split() int

Get the split number

func (Header) ToBlock added in v0.0.5

func (h Header) ToBlock() HeaderBlock

return an HeaderBlock

func (Header) ToDataBlock added in v0.0.5

func (h Header) ToDataBlock() HeaderDataBlock

return an Header Data Block

func (Header) ToInitBlock added in v0.0.5

func (h Header) ToInitBlock() HeaderInitBlock

return an HeaderInitBlock.

func (Header) ValidSignature added in v0.0.5

func (h Header) ValidSignature() bool

Check if signature is valid.

func (Header) Version added in v0.0.5

func (h Header) Version() Version

Get the Header used Version.

type HeaderBlock added in v0.0.5

type HeaderBlock struct {
	HeaderInitBlock
	HeaderDataBlock
}

Struct to represent a Block Header wich embed Header Init Block and Header Data Block.

func NewHeaderBlockFromContent added in v0.0.5

func NewHeaderBlockFromContent(content HeaderContent) HeaderBlock

Return a new Header Block from specified HeaderContent.

func (HeaderBlock) ToContent added in v0.0.7

func (hb HeaderBlock) ToContent() HeaderContent

Convert HeaderBlock to HeaderContent

func (HeaderBlock) ToHeader added in v0.0.95

func (hb HeaderBlock) ToHeader() Header

Convert HeaderBlock to HeaderContent

type HeaderContent added in v0.0.5

type HeaderContent [HeaderLength]byte // HeaderContent type definition

type HeaderDataBlock added in v0.0.5

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

Struct to represent a Data Block Header wich contains related information as []byte to encode/decode a file.

func (HeaderDataBlock) ToBytes added in v0.0.7

func (hdb HeaderDataBlock) ToBytes() []byte

Convert HeaderDataBlock to []byte

type HeaderInitBlock added in v0.0.5

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

Struct to represent an Init Block Header wich contains signature & pos of Data Block Header as []byte.

func (HeaderInitBlock) ToBytes added in v0.0.7

func (hib HeaderInitBlock) ToBytes() []byte

Convert HeaderInitBlock to []byte

type Key

type Key [KeyLength]byte // Key type definition

func NewKey

func NewKey() Key

Generate a new Key from a secure random generator.

type MahChaKey

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

Struct to represent a MahChaKey.

func NewMahChaKey

func NewMahChaKey() *MahChaKey

Return a new MahChaKey with secured key and nonce generated by crypto/rand source generator. The MahChaKey will use internaly a Randomizer sourced by a ChaCha20Source initialized with key and nonce.

func NewMahChaKeyFrom

func NewMahChaKeyFrom(key Key, nonce Nonce) *MahChaKey

Return a MahChaKey from specified key and nonce. The MahChaKey will use internaly a Randomizer sourced by a ChaCha20Source initialized with key and nonce.

func (*MahChaKey) DecryptHeader added in v0.0.11

func (k *MahChaKey) DecryptHeader(cipher []byte) Header

func (*MahChaKey) EncryptHeader added in v0.0.7

func (k *MahChaKey) EncryptHeader(header Header) []byte

func (*MahChaKey) GetKin added in v0.0.7

func (k *MahChaKey) GetKin() uint32

Returns a new Kin number required for a new encoding

func (*MahChaKey) GetRandomizer added in v0.0.16

func (k *MahChaKey) GetRandomizer() *Randomizer

Should be removed

func (*MahChaKey) InitEncode added in v0.0.8

func (k *MahChaKey) InitEncode(path string, profile ProfileType, split int) Header

Initiate encoding.

func (*MahChaKey) ResetRandomizer

func (k *MahChaKey) ResetRandomizer()

Reset the internal random generator.

type Measure added in v0.0.75

type Measure struct {
	Tasks            []*MeasureTask
	LabelWidth       int
	DefaultPrecision int
}

func NewMeasure added in v0.0.75

func NewMeasure() *Measure

func (*Measure) NewCustomTask added in v0.0.75

func (m *Measure) NewCustomTask(msg string, precision int) *MeasureTask

func (*Measure) NewTask added in v0.0.75

func (m *Measure) NewTask(msg string) *MeasureTask

func (*Measure) Report added in v0.0.75

func (m *Measure) Report() string

type MeasureTask added in v0.0.75

type MeasureTask struct {
	Msg       string
	Precision int
	Duration  Duration
}

func NewMeasureTask added in v0.0.75

func NewMeasureTask(msg string, precision int, start time.Time) *MeasureTask

func (*MeasureTask) Report added in v0.0.75

func (mt *MeasureTask) Report(width int) string

func (*MeasureTask) SetEnd added in v0.0.75

func (mt *MeasureTask) SetEnd()

type Nonce

type Nonce [NonceLength]byte // Nonce type definition

func NewNonce

func NewNonce() Nonce

Generate a new Nonce from a secure random generator.

type Profile added in v0.0.5

type Profile = int // Profile Type definition as enum

type ProfileStore added in v0.0.5

type ProfileStore struct {
	Type    ProfileType
	Profile Profile
	// contains filtered or unexported fields
}

func GetNoiseProfileStore added in v0.0.5

func GetNoiseProfileStore(profile ProfileType) ProfileStore

func SeqToProfiles added in v0.0.95

func SeqToProfiles(seq [16]byte, profile Profile) [4]ProfileStore

type ProfileType added in v0.0.5

type ProfileType = int // ProfileType Type definition as enum

type Randomizer

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

Struct to represent a Randomizer using a ChaCha20 random source generator

func NewRandomizer

func NewRandomizer(src *ChaCha20Source) *Randomizer

Initiate Randomizer with specified ChaCha20 source. If src is nil, it generates a new one.

func (*Randomizer) Bytes

func (r *Randomizer) Bytes(size int) []byte

Generate random []byte. It impacts the internal ChaCha20 source generator state.

func (*Randomizer) CustomMask

func (r *Randomizer) CustomMask(min int, max int) []byte

Generate a custom mask with a random size from specified min to max. It impacts the internal ChaCha20 source generator state.

func (*Randomizer) GetMap

func (r *Randomizer) GetMap(size int) []int

Generate a shuffled map of specified size. It impacts the internal ChaCha20 source generator state.

func (*Randomizer) GetNoiseMap

func (r *Randomizer) GetNoiseMap(size int, min int, max int) []int

Generate a noise map of speicified size, with random noise size between min and max. It impacts the internal ChaCha20 source generator state.

func (*Randomizer) GetRand

func (r *Randomizer) GetRand() *v2.Rand

Return *v2.Rand generator sourced with current *ChaCha20.

func (*Randomizer) Noise

func (r *Randomizer) Noise(size int) []byte

Generate noise as []byte of specified size. Note that it does not impacts the internal ChaCha20 generator state.

type SBox

type SBox [256]byte

type Block2 [16]byte

type Version added in v0.0.5

type Version struct {
	Major uint8
	Minor uint8
}

Struct to represent a Version.

var (
	Signature      [4]byte = [4]byte{0xce, 0xb6, 0xd9, 0x80} // MahCha File Signature
	CurrentVersion Version = Version{1, 0}                   // MahCha current version

)

func NewVersion added in v0.0.5

func NewVersion(major int, minor int) Version

Return a new Version.

func (Version) Is added in v0.0.5

func (v Version) Is(major int, minor int) bool

Check if version match.

func (Version) IsAtLeast added in v0.0.5

func (v Version) IsAtLeast(major int, minor int) bool

Check if version is at least.

func (Version) IsAtLeastMajor added in v0.0.5

func (v Version) IsAtLeastMajor(major int) bool

Check if version major is at least.

func (Version) IsAtLeastMinor added in v0.0.5

func (v Version) IsAtLeastMinor(minor int) bool

check if version minor is at least.

func (Version) IsMajor added in v0.0.5

func (v Version) IsMajor(major int) bool

Check if version major match.

func (Version) IsMinor added in v0.0.5

func (v Version) IsMinor(minor int) bool

Check if version minor match.

func (Version) To2Bytes added in v0.0.5

func (v Version) To2Bytes() [2]byte

Convert version to [2]byte

func (Version) ToBytes added in v0.0.5

func (v Version) ToBytes() []byte

Convert version to []byte

func (Version) ToString added in v0.0.7

func (v Version) ToString() string

Convert version to string

Jump to

Keyboard shortcuts

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