Last active
July 19, 2017 19:04
-
-
Save KentaKudo/27902e47c9ae24a1592cae42c83f57dd to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public struct Enigma { | |
private var rotorBox: RotorBox | |
private let plugboard: Cipher | |
public typealias Key = (Token, Token, Token) | |
public init(rotor0: @escaping Cipher, rotor1: @escaping Cipher, rotor2: @escaping Cipher, plugboard: @escaping Cipher, key: Key) { | |
self.rotorBox = RotorBox( | |
rotor0: RotorBox.Rotor(rotor0, position: key.0), | |
rotor1: RotorBox.Rotor(rotor1, position: key.1), | |
rotor2: RotorBox.Rotor(rotor2, position: key.2) | |
) | |
self.plugboard = plugboard | |
} | |
// 一文字暗号化(回転により内部状態が変わるのでmutating) | |
public mutating func cipher(_ target: Token) -> Token { | |
return plugboard(rotorBox.cipher(plugboard(target))) | |
} | |
// 文字列の暗号化 | |
public mutating func cipher(_ target: String) -> String { | |
var output = "" | |
for char in target.characters { | |
output += String(cipher(Token(rawValue: char)!).rawValue) | |
} | |
return output | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment