    -- |
    -- Module      : Crypto.Cipher.Types.Utils
    -- License     : BSD-style
    -- Maintainer  : Vincent Hanquez <vincent@snarc.org>
    -- Stability   : Stable
    -- Portability : Excellent
    --
    -- basic utility for cipher related stuff
    --
    module Crypto.Cipher.Types.Utils where

    import Data.Bits (xor)
    import Data.ByteString (ByteString)
    import qualified Data.ByteString as B

    chunk :: Int -> ByteString -> [ByteString]
    chunk :: Int -> ByteString -> [ByteString]
    chunk Int
    sz ByteString
    bs = ByteString -> [ByteString]
    split ByteString
    bs
      where split :: ByteString -> [ByteString]
    split ByteString
    b | ByteString -> Int
    B.length ByteString
    b Int -> Int -> Bool
    forall a. Ord a => a -> a -> Bool
    <= Int
    sz = [ByteString
    b]
                    | Bool
    otherwise        =
                            let (ByteString
    b1, ByteString
    b2) = Int -> ByteString -> (ByteString, ByteString)
    B.splitAt Int
    sz ByteString
    b
                             in ByteString
    b1 ByteString -> [ByteString] -> [ByteString]
    forall a. a -> [a] -> [a]
    : ByteString -> [ByteString]
    split ByteString
    b2

    bxor :: ByteString -> ByteString -> ByteString
    bxor :: ByteString -> ByteString -> ByteString
    bxor ByteString
    src ByteString
    dst = [Word8] -> ByteString
    B.pack ([Word8] -> ByteString) -> [Word8] -> ByteString
    forall a b. (a -> b) -> a -> b
    $ (Word8 -> Word8 -> Word8) -> ByteString -> ByteString -> [Word8]
    forall a. (Word8 -> Word8 -> a) -> ByteString -> ByteString -> [a]
    B.zipWith Word8 -> Word8 -> Word8
    forall a. Bits a => a -> a -> a
    xor ByteString
    src ByteString
    dst