summaryrefslogtreecommitdiffhomepage
path: root/ByteStrings.hs
blob: cecf617aca3f0d7186f5a6a46f9e3e992a09c3ec (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
{-# OPTIONS_GHC -fno-warn-orphans #-}

{- Copyright 2016 Joey Hess <id@joeyh.name>
 -
 - Licensed under the GNU AGPL version 3 or higher.
 -}

module ByteStrings where

import qualified Data.ByteString as B
import qualified Raaz
import Control.Monad
import Data.Word

allByteStringsOfLength :: Int -> [B.ByteString]
allByteStringsOfLength = go []
  where
	go ws n
		| n == 0 = return (B.pack ws)
		| otherwise = do
			w <- [0..255]
			go (w:ws) (n-1)

-- | Contains every possible byte strings, with shorter ones first.
allByteStrings :: [B.ByteString]
allByteStrings = concatMap allByteStringsOfLength [1..]

chunkByteString :: Int -> B.ByteString -> [B.ByteString]
chunkByteString n = go []
  where
	go cs b
		| B.length b <= n = reverse (b:cs)
		| otherwise = 
			let (h, t) = B.splitAt n b
			in go (h:cs) t

instance Raaz.Random Word8

randomByteStringOfLength :: Int -> Raaz.SystemPRG -> IO B.ByteString
randomByteStringOfLength n prg = B.pack <$> replicateM n randbyte
  where
	randbyte = Raaz.random prg :: IO Word8