packages feed

crypto-pubkey-0.2.2: Tests/T.hs

{-# LANGUAGE BangPatterns #-}
module Main where

import PregenKeys
import Control.Applicative
import qualified Crypto.Hash.SHA1 as SHA1
import qualified Crypto.PubKey.RSA as RSA
import qualified Crypto.PubKey.RSA.OAEP as RSAOAEP
import qualified Crypto.PubKey.RSA.PKCS15 as RSAPKCS15
import qualified Data.ByteString as B

import Crypto.Random

priv = rsaPrivatekey
pub  = rsaPublickey
oaepParams = RSAOAEP.defaultOAEPParams SHA1.hash

doEncrypt = False

nbEncrypt = 944242
nbDecrypt = 64325

main = do
    system <- cprgCreate <$> createEntropyPool :: IO SystemRNG
    if doEncrypt
        then do
            putStrLn $ show $ justEncrypt system nbEncrypt B.empty
        else do
            let t = either (error . show) id $ fst $ RSAPKCS15.encrypt system pub msg
    --let t = either (error . show) id $ RSAOAEP.encryptWithSeed (B.replicate 20 0) oaepParams pub msg
 
            putStrLn $ show $ decryptEncrypt nbDecrypt (t, msg)
  where msg = B.replicate 10 4
        decryptEncrypt 0 (!bEnc, !b) = b
        decryptEncrypt n (!bEnc, !b) =
            let bDec = either (error . show) id $ RSAPKCS15.decrypt Nothing priv bEnc in
            --let bDec = either (error . show) id $ RSAOAEP.decrypt Nothing oaepParams priv bEnc in
            decryptEncrypt (n-1) (bEnc, bDec)

        justEncrypt _ 0 (!bEnc) = bEnc
        justEncrypt rng n (!bEnc) =
            let bEnc2 = either (error . show) id $ fst $ RSAPKCS15.encrypt rng pub msg in
            justEncrypt rng (n-1) bEnc2