cipher

package
v1.3.2 Latest Latest
Warning

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

Go to latest
Published: Jan 22, 2026 License: MIT Imports: 9 Imported by: 0

Documentation

Overview

----------------------------------------------------------------- * L o r d O f S c r i p t s (tm) * Copyright (C)2025 Dídimo Grimaldo T. * goCaesarDisk GUI * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - * Implements basic Caesar cipher encoding/decoding using Unicode * (foreign) alphabets (not just ASCII). *-----------------------------------------------------------------

----------------------------------------------------------------- * L o r d O f S c r i p t s (tm) * Copyright (C)2025 Dídimo Grimaldo T. * goCaesarDisk GUI * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - * A Caesar Message PDU (Protocol Data Unit) consists of the encrypted * payload string, prepended with {TIMESTAMP}{CHECKSUM} where the * Timestamp is the standard YYYYMMDDTHHMMSS and the Checksum is the * XXHash64 checksum over the entire Payload. *-----------------------------------------------------------------

----------------------------------------------------------------- * L o r d O f S c r i p t s (tm) * Copyright (C)2025 Dídimo Grimaldo T. * APP_NAME * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - * *-----------------------------------------------------------------

----------------------------------------------------------------- * L o r d O f S c r i p t s (tm) * Copyright (C)2025 Dídimo Grimaldo T. * goCaesarDisk * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - * A key sequencer for the plain Caesar cipher in which only ONE key * is used. * Version: 1 * Class: Caesar (substitution cipher) * Mode : plain Caesar * Type : Monoalphabetic cipher (1) *-----------------------------------------------------------------

----------------------------------------------------------------- * L o r d O f S c r i p t s (tm) * Copyright (C)2025 Dídimo Grimaldo T. * goCaesarDisk * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - * Caesar key sequencer for Didimus mode. It uses the main Caesar * key for even-positioned characters that are encodeable, and the * alternate key made by adding the Offset to the Main key for all * odd-positioned characters that are encodeable. The odd/even condition * is not determined by the position in the input text, but by their * position after SKIPPING non-encodeable characters. The effective * key shift is always normalized to the current alphabet. * Version: 1 * Class: Caesar (substitution cipher) * Mode : Didimus * Type : Bi-alphabetic cipher (2) *-----------------------------------------------------------------

----------------------------------------------------------------- * L o r d O f S c r i p t s (tm) * Copyright (C)2025 Dídimo Grimaldo T. * goCaesarDisk * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - * Caesar key sequencer for Fibonacci mode. It uses the main Caesar * key and for each subsequent encodeable character in the input, it * uses a new key made by adding the main key to the current Fibonacci * term. The algorithm uses a 10-term Fibonacci series. The effective * key shift is always normalized to the current alphabet. * Version: 1 * Class: Caesar (substitution cipher) * Mode : Fibonacci * Type : Polyalphabetic cipher (up to 9) *-----------------------------------------------------------------

----------------------------------------------------------------- * L o r d O f S c r i p t s (tm) * Copyright (C)2025 Dídimo Grimaldo T. * APP_NAME * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - * *-----------------------------------------------------------------

----------------------------------------------------------------- * L o r d O f S c r i p t s (tm) * Copyright (C)2025 Dídimo Grimaldo T. * goCaesarDisk * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - * Caesar key sequencer for Primus mode. It uses the main Caesar * key and for subsequent encodeable characters in the input, it uses * a shift composed of the Main key/shift plus the current term in the * list of 10 primes (2..31). After the last prime is used, it resets * and starts again. The effective * key shift is always normalized to the current alphabet. * Version: 1 * Class: Caesar (substitution cipher) * Mode : Primus * Type : Poly-alphabetic cipher (up to 12) *-----------------------------------------------------------------

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func KeyAdjuster added in v1.3.2

func KeyAdjuster(N, shift int) int

For an alphabet length N adjust the provided shift key value so that it falls within the valid range of keys 0..N-1 by taking the modulo operation.

func OffsetAdjuster added in v1.3.2

func OffsetAdjuster(N, shift, offset int) int

For alphabet length N the valid range of shift is 0..N-1. It returns the alternate key which is computed from the main shift plus the offset modulo N. Note, it does not return an adjusted offset but the alternate key!

func PrimusMaximus added in v1.3.2

func PrimusMaximus() int

The maximum number of Prime-number terms available in the algorithm.

func RotateStringLeft

func RotateStringLeft(s string, shift int) string

func RotateStringRight

func RotateStringRight(s string, shift int) string

func VerifyCaesarMessage

func VerifyCaesarMessage(hasher hash.IDigest, packet string) (string, error)

Verify that the packaged Caesar message has not been corrupted. on success returns the payload (actual cipher message) and nil.

Types

type Caesar

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

func NewCaesarCipher

func NewCaesarCipher(config *CaesarParameters) *Caesar

(ctor) new instance of plain Caesar cipher. Caesar (plain) is a monoalphabetic substitution cipher. Rot13 is nothing but Caesar with (main) key=13. Note: the config.Offset is not used.

func NewCaesarCipherFromSequencer added in v1.3.2

func NewCaesarCipherFromSequencer(seq IKeySequencer) *Caesar

(ctor) to create a Caesar substitution cipher handler from a preset key sequencer that must have previously been validated.

func NewDidimusCipher added in v1.3.2

func NewDidimusCipher(config *CaesarParameters) *Caesar

(ctor) new instance of Didimus cipher. Didimus is a variant of Caesar that uses an offset over the main Caesar key. Being a bi-alphabetic substitution cipher, it alternates encoding using the main key for even characters and the alternate key (main key + offset % ALPHA_LEN) for odd characters. Note: the config.Offset is always used.

func NewFibonacciCipher added in v1.3.2

func NewFibonacciCipher(config *CaesarParameters) *Caesar

(ctor) Fibonacci is a polyalphabetic substitution cipher based on Caesar, but it uses a 10-term Fibonacci series to generate alternate keys that are used for every other character. Note: the config.Offset is not used.

func NewPrimusCipher added in v1.3.2

func NewPrimusCipher(config *CaesarParameters) *Caesar

(ctor) Primus is a polyalphabetic substitution cipher based on Caesar. It is similar to Fibonacci with two differences. It uses (up to) the first 10 Prime numbers to generate the alternate keys instead of a Fibonacci series. The Offset (0..10) is used to determine how many prime numbers are used prior to rewinding. In every iteration the first is the main Caesar key, and the rest uses the main key plus the prime number modulo length of the alphabet. Note: the config.Offset is used.

func (*Caesar) Decode

func (c *Caesar) Decode(ciphered string) string

implements ITranscoder for decoding/decrypting a message using the selected Caesar mode/variant (Caesar, Didimus, Fibonacci, Primus)

func (*Caesar) DecodeMessage

func (c *Caesar) DecodeMessage(cipheredMessage string) (string, error)

decode a message that is in "standard" format

func (*Caesar) Encode

func (c *Caesar) Encode(plain string) string

implements ITranscoder for encoding/encrypting a message using the selected Caesar mode/variant (Caesar, Didimus, Fibonacci, Primus)

func (*Caesar) EncodeMessage

func (c *Caesar) EncodeMessage(plain string) (string, error)

encode a message and package it in a "standard" form. The standard PDU format is {TIMESTAMP}{CHECKSUM}{PAYLOAD} where Payload is the encrypted message string, Checksum is the checksum over the payload, and Timestamp is the the form YYYYMMDDTHHMMSS

func (*Caesar) String added in v1.3.1

func (c *Caesar) String() string

implements fmt.Stringer and returns algorithm parameters

type CaesarMessage

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

func NewCaesarMessage

func NewCaesarMessage(digest hash.IDigest) *CaesarMessage

(ctor) A ciphered Caesar message that will be formatted with date/time and a user-chosen hash or checksum

func (*CaesarMessage) AddMessage

func (m *CaesarMessage) AddMessage(ciphered string)

used when packaging a new Caesar message by adding new cipher data.

func (*CaesarMessage) String

func (m *CaesarMessage) String() string

implements fmt.Stringer returning the encapsulated Caesar cipher message prepended with the date/time YYYYMMDDTHHMMSS and the message hash

type CaesarParameters

type CaesarParameters struct {
	Alphabet *caesardisk.AlphabetModel
	KeyValue int
	Offset   int // not used for plain Caesar, just Didimus & Fibonacci
	// contains filtered or unexported fields
}

func NewCaesarParameters

func NewCaesarParameters(alphabet *caesardisk.AlphabetModel) *CaesarParameters

(ctor) a new instance of Caesar cipher parameters for the chosen alphabet.

func (*CaesarParameters) GetAlpha added in v1.3.2

func (c *CaesarParameters) GetAlpha() *caesardisk.AlphabetModel

func (*CaesarParameters) GetAltKey added in v1.3.2

func (c *CaesarParameters) GetAltKey() (rune, int)

get the actual alternate key (derived from key, offset & alphabet) in its letter and key shift forms.

func (*CaesarParameters) GetKey added in v1.3.2

func (c *CaesarParameters) GetKey() (rune, int)

func (*CaesarParameters) SetAlpha added in v1.3.2

func (c *CaesarParameters) SetAlpha(alpha *caesardisk.AlphabetModel) (int, int)

The the source encoding alphabet. If there was no previous alpha, key & offset are reset. If the new alpha is shorter than the previous and the key shift is out or range, it gets adjusted. It returns the newly computed main key shift and computed alternate key.

func (*CaesarParameters) SetAltKeyOffset added in v1.3.2

func (c *CaesarParameters) SetAltKeyOffset(offset int) int

compute the new alternate key based on the existing main key and the provided offset. Positive and Negative values produce different results. It returns the computed alternate key shift.

func (*CaesarParameters) SetKey added in v1.3.2

func (c *CaesarParameters) SetKey(shift int) int

set the main keyShift and returns the adjusted (actual) effective key shift corrected for alphabet length. Positive and Negative values produce different results!

func (*CaesarParameters) String

func (c *CaesarParameters) String() string

implements fmt.Stringer and returns the current key in character and shift value together with the actual alphabet letters or symbols.

type CaesarSequencer added in v1.3.2

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

A Caesar sequencer sequences the key value through the message encoding & decoding. It implements IKeySequencer. Only ONE Caesar table is needed.

func NewCaesarSequencer added in v1.3.2

func NewCaesarSequencer(par *CaesarParameters) *CaesarSequencer

(ctor) A new instance of a plain Caesar key sequencer. For plain Caesar the same key is used throughout the message. The Caesar cipher is monosyllabic, one key and one transcode.

func (*CaesarSequencer) GetParams added in v1.3.2

func (cs *CaesarSequencer) GetParams() *CaesarParameters

func (*CaesarSequencer) IsOffsetRequired added in v1.3.2

func (cs *CaesarSequencer) IsOffsetRequired() bool

whether the Offset parameter is used in key sequencing

func (*CaesarSequencer) IsPolyalphabetic added in v1.3.2

func (cs *CaesarSequencer) IsPolyalphabetic() bool

Caesar is a monoalphabetic substitution cipher

func (*CaesarSequencer) KeyRange added in v1.3.2

func (cs *CaesarSequencer) KeyRange() (min, max int)

the range of valid key values for Caesar cipher. For plain Caesar the prim parameter is ignored.

func (*CaesarSequencer) NextKey added in v1.3.2

func (cs *CaesarSequencer) NextKey() int

Get the next key to use, should only be called if a message's character is not skipped. Characters are skipped if they are not part of the encoding alphabet, and thus do not participate in the key computation. Note: For plain Caesar we use the same key throughout the message.

func (*CaesarSequencer) String added in v1.3.2

func (cs *CaesarSequencer) String() string

func (*CaesarSequencer) Validate added in v1.3.2

func (cs *CaesarSequencer) Validate() error

type DidimusSequencer added in v1.3.2

type DidimusSequencer struct {
	CaesarSequencer
	// contains filtered or unexported fields
}

Didimus uses a bi-alphabetic sequencer with the same Caesar Key for encodeable even characters, and using a key+offset for the odd encodeable characters. Therefore, it uses 2 Caesar tables.

func NewDidimusSequencer added in v1.3.2

func NewDidimusSequencer(par *CaesarParameters) *DidimusSequencer

(ctor) A new instance of a Didimus key sequencer. Didimus is part of the Caesar cipher family but is bi-syllabic, the normal Caesar key is used for even characters (provided they are in the encoding alphabet), an

func (*DidimusSequencer) GetParams added in v1.3.2

func (ds *DidimusSequencer) GetParams() *CaesarParameters

func (*DidimusSequencer) IsOffsetRequired added in v1.3.2

func (ds *DidimusSequencer) IsOffsetRequired() bool

whether the Offset parameter is used in key sequencing. Didimus uses the Offset to generate an alternate key by adding the Offset to the Main Key's shift.

func (*DidimusSequencer) IsPolyalphabetic added in v1.3.2

func (ds *DidimusSequencer) IsPolyalphabetic() bool

Didimus is a bi-alphabetic substitution cipher

func (*DidimusSequencer) KeyRange added in v1.3.2

func (ds *DidimusSequencer) KeyRange() (min, max int)

The valid range of Didimus keys.

func (*DidimusSequencer) NextKey added in v1.3.2

func (ds *DidimusSequencer) NextKey() int

func (*DidimusSequencer) String added in v1.3.2

func (ds *DidimusSequencer) String() string

func (*DidimusSequencer) Validate added in v1.3.2

func (ds *DidimusSequencer) Validate() error

type FibonacciSequencer added in v1.3.2

type FibonacciSequencer struct {
	CaesarSequencer
	// contains filtered or unexported fields
}

Fibonacci uses a poly-alphabetic sequencer in which it starts with the main Caesar key, and for subsequent encodeable characters it uses key+F(x) where F(x) is the Xth term of a Fibonacci series. Our Fibonacci series has 10 terms.

func NewFibonacciSequencer added in v1.3.2

func NewFibonacciSequencer(par *CaesarParameters) *FibonacciSequencer

(Ctor) a new instance of the Caesar encoder using Fibonacci mode.

func (*FibonacciSequencer) GetParams added in v1.3.2

func (fs *FibonacciSequencer) GetParams() *CaesarParameters

func (*FibonacciSequencer) IsOffsetRequired added in v1.3.2

func (fs *FibonacciSequencer) IsOffsetRequired() bool

whether the Offset parameter is used in key sequencing. Fibonacci v1 does not use Offset.

func (*FibonacciSequencer) IsPolyalphabetic added in v1.3.2

func (fs *FibonacciSequencer) IsPolyalphabetic() bool

Fibonacci is a polyalphabetic substitution cipher

func (*FibonacciSequencer) KeyRange added in v1.3.2

func (fs *FibonacciSequencer) KeyRange() (min, max int)

The valid range of Didimus keys.

func (*FibonacciSequencer) NextKey added in v1.3.2

func (fs *FibonacciSequencer) NextKey() int

func (*FibonacciSequencer) String added in v1.3.2

func (fs *FibonacciSequencer) String() string

func (*FibonacciSequencer) Validate added in v1.3.2

func (fs *FibonacciSequencer) Validate() error

type IKeySequencer added in v1.3.2

type IKeySequencer interface {
	// implements fmt.Stringer and returns the name of the Cipher
	fmt.Stringer
	// Validates the key parameters. Must be called after the
	// constructor.
	Validate() error
	// Query the key value range (min,max) for main or alt key
	KeyRange() (min, max int)
	// Retrieve cipher parameters
	GetParams() *CaesarParameters
	// gets the key to use for the current character. Must only be
	// called if the character to encode/decode is part of the
	// encoding alphabet.
	NextKey() int
	// whether the sequencer is polialphabetic or not
	IsPolyalphabetic() bool
	// whether the Offset parameter is used in key sequencing
	IsOffsetRequired() bool
}

Allows for key schedules in a message. Plain caesar is monosyllabic and thus has no schedule (same key always). Didimus & Fibonacci are polisyllabic and have their own key schedules over a single message.

type IParamProvider added in v1.3.2

type IParamProvider interface {
	// Get the current alphabet.
	GetAlpha() *caesardisk.AlphabetModel
	// Get the main key in character and key shift.
	GetKey() (rune, int)
	// Get the alternate key in character and key shift based on
	// the existing main key shift and offset.
	GetAltKey() (rune, int)
	// The the source encoding alphabet. If there was no previous alpha,
	// key & offset are reset. If the new alpha is shorter than the previous
	// and the key shift is out or range, it gets adjusted. It returns
	// the newly computed main key shift and computed alternate key.
	SetAlpha(*caesardisk.AlphabetModel) (newKeyShift, newAltKey int)
	// set the main keyShift and returns the adjusted (actual) effective
	// key shift corrected for alphabet length. Positive and Negative
	// values produce different results!
	SetKey(keyShift int) int
	// compute the new alternate key based on the existing main key
	// and the provided offset. Positive and Negative values produce
	// different results. It returns the computed alternate key shift.
	SetAltKeyOffset(offset int) int
}

type ITranscoder added in v1.3.1

type ITranscoder interface {
	Encode(string) string
	Decode(string) string
}

An interface for all text/cipher transforms

type PrimusSequencer added in v1.3.2

type PrimusSequencer struct {
	CaesarSequencer
	// contains filtered or unexported fields
}

Primus is like Fibonacci but instead of a Fibonacci sequence, it uses a sequence of the first 11 prime numbers as an offset to the main Caesar key. Although 0 is not a prime, the first in the series is always zero, thus the main Caesar key.

func NewPrimusSequencer added in v1.3.2

func NewPrimusSequencer(par *CaesarParameters) *PrimusSequencer

(Ctor) a new instance of the Primus sequencer for the Caesar encoder. The Offset value is automatically corrected via modulo to the maximum number of prime values. When Offset is zero we select the maximum (11)

func (*PrimusSequencer) GetParams added in v1.3.2

func (ps *PrimusSequencer) GetParams() *CaesarParameters

func (*PrimusSequencer) IsOffsetRequired added in v1.3.2

func (ps *PrimusSequencer) IsOffsetRequired() bool

whether the Offset parameter is used in key sequencing. Primus uses the Offset value to indicate how many prime numbers (past the initial main key) should be used.

func (*PrimusSequencer) IsPolyalphabetic added in v1.3.2

func (ps *PrimusSequencer) IsPolyalphabetic() bool

Primus is a polyalphabetic substitution cipher

func (*PrimusSequencer) KeyRange added in v1.3.2

func (ps *PrimusSequencer) KeyRange() (min, max int)

The valid range of Primus keys.

func (*PrimusSequencer) NextKey added in v1.3.2

func (ps *PrimusSequencer) NextKey() int

func (*PrimusSequencer) String added in v1.3.2

func (ps *PrimusSequencer) String() string

func (*PrimusSequencer) Validate added in v1.3.2

func (ps *PrimusSequencer) Validate() error

Jump to

Keyboard shortcuts

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