Last active
August 29, 2015 14:15
-
-
Save bdesham/9f789e541e107b5a2563 to your computer and use it in GitHub Desktop.
Obfuscate text for use in web pages
This file contains 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
{- | |
Replaces each character with an HTML (or XML) escape code like " ". This is | |
useful as lightweight protection against email-address harvesting. | |
Example: | |
ghci> obfuscate "[email protected]" | |
"a@example.co" | |
Compare to the Clojure version: https://gist.github.com/bdesham/3327022 | |
-} | |
module Obfuscate (obfuscate) where | |
import Data.Char (ord) | |
import Numeric (showHex) | |
numberAsHexString :: (Show a, Integral a) => a -> String | |
numberAsHexString n = showHex n "" | |
-- Just for kicks, we use hex numbers for some input characters and decimal | |
-- numbers for others | |
obfuscateChar :: Char -> String | |
obfuscateChar c | |
| num `mod` 2 == 0 = "&#x" ++ numberAsHexString num ++ ";" | |
| num `mod` 2 == 1 = "&#" ++ show num ++ ";" | |
where num = ord c | |
obfuscate :: String -> String | |
obfuscate = concat . (map obfuscateChar) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment