Sha256: 789b2a111808047057c885581725f3365719cad03e03cf1551e5887bf10f09a1
Contents?: true
Size: 1.66 KB
Versions: 245
Compression:
Stored size: 1.66 KB
Contents
module CryptoSquare ( normalizedPlaintext , plaintextSegments , encoded , ciphertext ) where import Prelude import Data.Array as A import Data.Array (filter, fromFoldable, replicate, toUnfoldable, (:)) import Data.Char.Unicode (isAlphaNum) import Data.Foldable (maximum) import Data.Int (ceil, toNumber) import Data.List (transpose) import Data.Maybe (fromMaybe) import Data.String (drop, fromCharArray, joinWith, length, take, toCharArray, toLower) import Math (sqrt) normalizedPlaintext :: String -> String normalizedPlaintext = toCharArray >>> filter isAlphaNum >>> fromCharArray >>> toLower plaintextSegments :: String -> Array String plaintextSegments str = toSquare norm where norm = normalizedPlaintext str cols = sqrt (length norm # toNumber) # ceil toSquare "" = [] toSquare s = take cols s : toSquare (drop cols s) transposeArray :: forall a. Array (Array a) -> Array (Array a) transposeArray = toUnfoldable >>> map toUnfoldable >>> transpose >>> map fromFoldable >>> fromFoldable encoded :: String -> String encoded = plaintextSegments >>> map toCharArray >>> transposeArray >>> map fromCharArray >>> joinWith "" spaces :: Int -> Array Char spaces n = replicate n ' ' equalPad :: Array (Array Char) -> Array (Array Char) equalPad arr = map pad arr where width = fromMaybe 0 (maximum $ map A.length arr) pad el = el <> spaces (width - A.length el) ciphertext :: String -> String ciphertext = plaintextSegments >>> map toCharArray >>> transposeArray >>> equalPad >>> map fromCharArray >>> joinWith " "
Version data entries
245 entries across 245 versions & 1 rubygems