diff --git a/enigma.go b/enigma.go index 0e289f3..4391863 100644 --- a/enigma.go +++ b/enigma.go @@ -82,7 +82,7 @@ type RotorConfig struct { func NewEnigma(rotorConfiguration []RotorConfig, refID string, plugs []string) *Enigma { rotors := make(Rotors, len(rotorConfiguration)) for i, configuration := range rotorConfiguration { - rotors[i] = HistoricRotors.GetByID(configuration.ID) + rotors[i] = *HistoricRotors.GetByID(configuration.ID) rotors[i].Offset = CharToIndex(configuration.Start) rotors[i].Ring = configuration.Ring - 1 } @@ -118,13 +118,13 @@ func (e *Enigma) EncodeChar(letter byte) byte { letterIndex = e.Plugboard[letterIndex] for i := len(e.Rotors) - 1; i >= 0; i-- { - e.Rotors[i].Step(&letterIndex, false) + letterIndex = e.Rotors[i].Step(letterIndex, false) } letterIndex = e.Reflector.Sequence[letterIndex] for i := 0; i < len(e.Rotors); i++ { - e.Rotors[i].Step(&letterIndex, true) + letterIndex = e.Rotors[i].Step(letterIndex, true) } letterIndex = e.Plugboard[letterIndex] diff --git a/presets.go b/presets.go index 208b216..9557c11 100644 --- a/presets.go +++ b/presets.go @@ -4,24 +4,24 @@ package enigma // notches. "Beta" and "Gamma" are additional rotors used in M4 // at the leftmost position. var HistoricRotors = Rotors{ - NewRotor("EKMFLGDQVZNTOWYHXUSPAIBRCJ", "I", "Q"), - NewRotor("AJDKSIRUXBLHWTMCQGZNPYFVOE", "II", "E"), - NewRotor("BDFHJLCPRTXVZNYEIWGAKMUSQO", "III", "V"), - NewRotor("ESOVPZJAYQUIRHXLNFTGKDCMWB", "IV", "J"), - NewRotor("VZBRGITYUPSDNHLXAWMJQOFECK", "V", "Z"), - NewRotor("JPGVOUMFYQBENHZRDKASXLICTW", "VI", "ZM"), - NewRotor("NZJHGRCXMYSWBOUFAIVLPEKQDT", "VII", "ZM"), - NewRotor("FKQHTLXOCBJSPDZRAMEWNIUYGV", "VIII", "ZM"), - NewRotor("LEYJVCNIXWPBQMDRTAKZGFUHOS", "Beta", ""), - NewRotor("FSOKANUERHMBTIYCWLQPZXVGJD", "Gamma", ""), + *NewRotor("EKMFLGDQVZNTOWYHXUSPAIBRCJ", "I", "Q"), + *NewRotor("AJDKSIRUXBLHWTMCQGZNPYFVOE", "II", "E"), + *NewRotor("BDFHJLCPRTXVZNYEIWGAKMUSQO", "III", "V"), + *NewRotor("ESOVPZJAYQUIRHXLNFTGKDCMWB", "IV", "J"), + *NewRotor("VZBRGITYUPSDNHLXAWMJQOFECK", "V", "Z"), + *NewRotor("JPGVOUMFYQBENHZRDKASXLICTW", "VI", "ZM"), + *NewRotor("NZJHGRCXMYSWBOUFAIVLPEKQDT", "VII", "ZM"), + *NewRotor("FKQHTLXOCBJSPDZRAMEWNIUYGV", "VIII", "ZM"), + *NewRotor("LEYJVCNIXWPBQMDRTAKZGFUHOS", "Beta", ""), + *NewRotor("FSOKANUERHMBTIYCWLQPZXVGJD", "Gamma", ""), } // HistoricReflectors in the list are pre-loaded with historically accurate data // from Enigma machines. Use "B-Thin" and "C-Thin" with M4 (4 rotors). var HistoricReflectors = Reflectors{ - NewReflector("EJMZALYXVBWFCRQUONTSPIKHGD", "A"), - NewReflector("YRUHQSLDPXNGOKMIEBFZCWVJAT", "B"), - NewReflector("FVPJIAOYEDRZXWGCTKUQSBNMHL", "C"), - NewReflector("ENKQAUYWJICOPBLMDXZVFTHRGS", "B-thin"), - NewReflector("RDOBJNTKVEHMLFCWZAXGYIPSUQ", "C-thin"), + *NewReflector("EJMZALYXVBWFCRQUONTSPIKHGD", "A"), + *NewReflector("YRUHQSLDPXNGOKMIEBFZCWVJAT", "B"), + *NewReflector("FVPJIAOYEDRZXWGCTKUQSBNMHL", "C"), + *NewReflector("ENKQAUYWJICOPBLMDXZVFTHRGS", "B-thin"), + *NewReflector("RDOBJNTKVEHMLFCWZAXGYIPSUQ", "C-thin"), } diff --git a/reflector.go b/reflector.go index f4c52cb..ae0b680 100644 --- a/reflector.go +++ b/reflector.go @@ -19,14 +19,14 @@ func NewReflector(mapping string, id string) *Reflector { } // Reflectors is a simple list of reflector pointers. -type Reflectors []*Reflector +type Reflectors []Reflector // GetByID takes a "name" of the reflector (e.g. "B") and returns the // Reflector pointer. func (refs *Reflectors) GetByID(id string) *Reflector { for _, ref := range *refs { if ref.ID == id { - return ref + return &ref } } return nil diff --git a/rotor.go b/rotor.go index c5afa22..dc377b0 100644 --- a/rotor.go +++ b/rotor.go @@ -50,27 +50,26 @@ func (r *Rotor) ShouldTurnOver() bool { // Step through the rotor, performing the letter substitution depending // on the offset and direction. -func (r *Rotor) Step(letter *int, invert bool) { - l := *letter - l = (l - r.Ring + r.Offset + 26) % 26 +func (r *Rotor) Step(letter int, invert bool) int { + letter = (letter - r.Ring + r.Offset + 26) % 26 if invert { - l = r.ReverseSeq[l] + letter = r.ReverseSeq[letter] } else { - l = r.StraightSeq[l] + letter = r.StraightSeq[letter] } - l = (l + r.Ring - r.Offset + 26) % 26 - *letter = l + letter = (letter + r.Ring - r.Offset + 26) % 26 + return letter } // Rotors is a simple list of rotor pointers. -type Rotors []*Rotor +type Rotors []Rotor // GetByID takes a "name" of the rotor (e.g. "III") and returns the // Rotor pointer. func (rs *Rotors) GetByID(id string) *Rotor { for _, rotor := range *rs { if rotor.ID == id { - return rotor + return &rotor } } return nil