RSAPair.hs 878 B

123456789101112131415161718192021222324252627
  1. module Main where
  2. import Codec.Crypto.RSA
  3. import Crypto.Random
  4. import Crypto.Random.DRBG
  5. import Data.Binary
  6. import qualified Data.ByteString.Lazy as BS
  7. import Numeric (showHex)
  8. import System.Environment (getArgs)
  9. import System.IO (hPutStrLn, stderr)
  10. toHex :: (Binary a) => a -> String
  11. toHex = concat . map (flip showHex "") . BS.unpack . encode
  12. main :: IO ()
  13. main = do
  14. args <- getArgs
  15. let size = case args of (s:_) -> read s
  16. _ -> 1024
  17. hPutStrLn stderr ("Generating key pair of size " ++ show size)
  18. (pub, priv) <- genPair size
  19. putStrLn ("pub: " ++ toHex pub)
  20. putStrLn ("priv: " ++ toHex priv)
  21. genPair :: Int -> IO (PublicKey, PrivateKey)
  22. genPair size = go `fmap` (newGenIO :: IO HashDRBG)
  23. where go g = let (pub, priv, _) = generateKeyPair g size in (pub, priv)