-- |
-- 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           Crypto.Internal.ByteArray (ByteArray)
import qualified Crypto.Internal.ByteArray as B

-- | Chunk some input byte array into @sz byte list of byte array.
chunk :: ByteArray b => Int -> b -> [b]
chunk :: forall b. ByteArray b => Int -> b -> [b]
chunk Int
sz b
bs = forall {t}. ByteArray t => t -> [t]
split b
bs
  where split :: t -> [t]
split t
b | forall ba. ByteArrayAccess ba => ba -> Int
B.length t
b forall a. Ord a => a -> a -> Bool
<= Int
sz = [t
b]
                | Bool
otherwise        =
                        let (t
b1, t
b2) = forall bs. ByteArray bs => Int -> bs -> (bs, bs)
B.splitAt Int
sz t
b
                         in t
b1 forall a. a -> [a] -> [a]
: t -> [t]
split t
b2