module OpenSSL.EVP.Cipher
( Cipher
, getCipherByName
, getCipherNames
, CryptoMode(..)
, cipher
, cipherBS
, cipherLBS
, cipherStrictLBS
)
where
import qualified Data.ByteString.Char8 as B8
import qualified Data.ByteString.Lazy.Char8 as L8
import Foreign
import Foreign.C
import OpenSSL.Objects
import OpenSSL.EVP.Internal
#if !MIN_VERSION_base(4,8,0)
import Data.Monoid
#endif
foreign import ccall unsafe "EVP_get_cipherbyname"
_get_cipherbyname :: CString -> IO (Ptr EVP_CIPHER)
getCipherByName :: String -> IO (Maybe Cipher)
getCipherByName name
= withCString name $ \ namePtr ->
do ptr <- _get_cipherbyname namePtr
if ptr == nullPtr then
return Nothing
else
return $ Just $ Cipher ptr
getCipherNames :: IO [String]
getCipherNames = getObjNames CipherMethodType True
cipherStrictLBS :: Cipher
-> B8.ByteString
-> B8.ByteString
-> CryptoMode
-> L8.ByteString
-> IO L8.ByteString
cipherStrictLBS c key iv mode input =
do ctx <- cipherInitBS c key iv mode
xs <- cipherUpdateBS ctx `mapM` L8.toChunks input
x <- cipherFinalBS ctx
return $ L8.fromChunks (xs `mappend` [x])
cipher :: Cipher
-> String
-> String
-> CryptoMode
-> String
-> IO String
cipher c key iv mode input
= fmap L8.unpack $ cipherLBS c (B8.pack key) (B8.pack iv) mode (L8.pack input)
cipherBS :: Cipher
-> B8.ByteString
-> B8.ByteString
-> CryptoMode
-> B8.ByteString
-> IO B8.ByteString
cipherBS c key iv mode input
= do ctx <- cipherInitBS c key iv mode
cipherStrictly ctx input
cipherLBS :: Cipher
-> B8.ByteString
-> B8.ByteString
-> CryptoMode
-> L8.ByteString
-> IO L8.ByteString
cipherLBS c key iv mode input
= do ctx <- cipherInitBS c key iv mode
cipherLazily ctx input