-- |
-- 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 = b -> [b]
forall {t}. ByteArray t => t -> [t]
split b
bs
  where split :: t -> [t]
split t
b | t -> Int
forall ba. ByteArrayAccess ba => ba -> Int
B.length t
b Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
<= Int
sz = [t
b]
                | Bool
otherwise        =
                        let (t
b1, t
b2) = Int -> t -> (t, t)
forall bs. ByteArray bs => Int -> bs -> (bs, bs)
B.splitAt Int
sz t
b
                         in t
b1 t -> [t] -> [t]
forall a. a -> [a] -> [a]
: t -> [t]
split t
b2