From 6d07d856ce61005edcbcd8ba4ee40e6ecdde3b93 Mon Sep 17 00:00:00 2001 From: PAndaContron Date: Thu, 28 Oct 2021 16:40:12 -0400 Subject: [PATCH 1/2] XOR cipher --- algorithms/ciphers/Xor.hs | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 algorithms/ciphers/Xor.hs diff --git a/algorithms/ciphers/Xor.hs b/algorithms/ciphers/Xor.hs new file mode 100644 index 0000000..1b9f860 --- /dev/null +++ b/algorithms/ciphers/Xor.hs @@ -0,0 +1,21 @@ +module Xor + ( encodeB + , decodeB + , encode + , decode + ) where + +import Data.Char +import Data.Bits + +encodeB :: Bits a => [a] -> [a] -> [a] +encodeB key = zipWith xor (cycle key) + +decodeB :: Bits a => [a] -> [a] -> [a] +decodeB = encodeB + +encode :: String -> String -> String +encode key txt = map chr $ encodeB (map ord key) (map ord txt) + +decode :: String -> String -> String +decode = encode From 2c338186d8b17ce81f1b831157d180c88bad119a Mon Sep 17 00:00:00 2001 From: PAndaContron Date: Thu, 28 Oct 2021 16:43:09 -0400 Subject: [PATCH 2/2] Comments for XOR --- algorithms/ciphers/Xor.hs | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/algorithms/ciphers/Xor.hs b/algorithms/ciphers/Xor.hs index 1b9f860..e8c592b 100644 --- a/algorithms/ciphers/Xor.hs +++ b/algorithms/ciphers/Xor.hs @@ -8,14 +8,21 @@ module Xor import Data.Char import Data.Bits +-- Raw encoding function for lists of numbers +-- First argument is the key, second is the data +-- The key will be repeated to the length of the data encodeB :: Bits a => [a] -> [a] -> [a] encodeB key = zipWith xor (cycle key) +-- Decoding is the same as encoding decodeB :: Bits a => [a] -> [a] -> [a] decodeB = encodeB +-- To encode a String, we first map each character to a number, +-- then map the numbers back to characters after encoding encode :: String -> String -> String encode key txt = map chr $ encodeB (map ord key) (map ord txt) +-- Decoding is the same as encoding decode :: String -> String -> String decode = encode