HsOpenSSL-0.6: examples/HelloWorld.hs
import Control.Monad
import Data.List
import Data.Maybe
import OpenSSL
import OpenSSL.EVP.Cipher
import OpenSSL.EVP.Open
import OpenSSL.EVP.PKey
import OpenSSL.EVP.Seal
import OpenSSL.PEM
import OpenSSL.RSA
import Text.Printf
main = withOpenSSL $
do putStrLn "cipher: DES-CBC"
des <- liftM fromJust $ getCipherByName "DES-CBC"
putStrLn "generating RSA keypair..."
rsa <- generateRSAKey 512 65537 Nothing
let plainText = "Hello, world!"
putStrLn ("plain text to encrypt: " ++ plainText)
putStrLn ""
putStrLn "encrypting..."
(encrypted, [encKey], iv) <- seal des [fromPublicKey rsa] plainText
putStrLn ("encrypted symmetric key: " ++ binToHex encKey)
putStrLn ("IV: " ++ binToHex iv)
putStrLn ("encrypted message: " ++ binToHex encrypted)
putStrLn ""
putStrLn "decrypting..."
let decrypted = open des encKey iv rsa encrypted
putStrLn ("decrypted message: " ++ decrypted)
binToHex :: String -> String
binToHex bin = concat $ intersperse ":" $ map (printf "%02x" . fromEnum) bin