Don't like ads? PRO users don't see any ads ;-)
Guest

PA4 on Haskell

By: a guest on Apr 14th, 2012  |  syntax: Haskell  |  size: 0.74 KB  |  hits: 27  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. {-# LANGUAGE ViewPatterns #-}
  2.  
  3. import Data.Bits (xor)
  4. import Data.Char
  5. import Data.Hex (hex, unhex)
  6. import Text.Regex
  7. import Data.Maybe (mapMaybe)
  8. import Data.List
  9.  
  10. unHex :: String -> String
  11. unHex = head . unhex
  12.  
  13. listXor k m = zipWith xor (cycle k) m
  14. stringXor (map ord -> k) (map ord -> m) = map chr $ listXor k m
  15.  
  16. validPad = matchRegex (mkRegex "GET /([0-9a-f]+) HTTP/1.1\" 404")
  17.  
  18. process valids = concat $ zipWith stringXor guesses ciphers where
  19.   guesses = tail $ map (stringXor "\16" . take 16) lastValids
  20.   ciphers = init $ map (drop 16) lastValids
  21.   lastValids = map (valids!!) [16-1,32-1..length valids - 1]
  22.  
  23. main = do
  24.   f <- readFile "proj4-log.txt"
  25.   putStrLn $ process $ map unHex $ concat  $ mapMaybe validPad $ lines f