add zero padding and its test.
This commit is contained in:
parent
87867b49bc
commit
c2285db4e3
@ -21,6 +21,7 @@ import qualified Data.ByteArray as B
|
|||||||
data Format =
|
data Format =
|
||||||
PKCS5 -- ^ PKCS5: PKCS7 with hardcoded size of 8
|
PKCS5 -- ^ PKCS5: PKCS7 with hardcoded size of 8
|
||||||
| PKCS7 Int -- ^ PKCS7 with padding size between 1 and 255
|
| PKCS7 Int -- ^ PKCS7 with padding size between 1 and 255
|
||||||
|
| ZERO Int -- ^ zero padding with block size
|
||||||
deriving (Show, Eq)
|
deriving (Show, Eq)
|
||||||
|
|
||||||
-- | Apply some pad to a bytearray
|
-- | Apply some pad to a bytearray
|
||||||
@ -30,6 +31,15 @@ pad (PKCS7 sz) bin = bin `B.append` paddingString
|
|||||||
where
|
where
|
||||||
paddingString = B.replicate paddingByte (fromIntegral paddingByte)
|
paddingString = B.replicate paddingByte (fromIntegral paddingByte)
|
||||||
paddingByte = sz - (B.length bin `mod` sz)
|
paddingByte = sz - (B.length bin `mod` sz)
|
||||||
|
pad (ZERO sz) bin = bin `B.append` paddingString
|
||||||
|
where
|
||||||
|
paddingString = B.replicate paddingSz 0
|
||||||
|
paddingSz
|
||||||
|
| len == 0 = sz
|
||||||
|
| m == 0 = 0
|
||||||
|
| otherwise = sz - m
|
||||||
|
m = len `mod` sz
|
||||||
|
len = B.length bin
|
||||||
|
|
||||||
-- | Try to remove some padding from a bytearray.
|
-- | Try to remove some padding from a bytearray.
|
||||||
unpad :: ByteArray byteArray => Format -> byteArray -> Maybe byteArray
|
unpad :: ByteArray byteArray => Format -> byteArray -> Maybe byteArray
|
||||||
@ -46,3 +56,4 @@ unpad (PKCS7 sz) bin
|
|||||||
paddingSz = fromIntegral paddingByte
|
paddingSz = fromIntegral paddingByte
|
||||||
(content, padding) = B.splitAt (len - paddingSz) bin
|
(content, padding) = B.splitAt (len - paddingSz) bin
|
||||||
paddingWitness = B.replicate paddingSz paddingByte :: Bytes
|
paddingWitness = B.replicate paddingSz paddingByte :: Bytes
|
||||||
|
unpad (ZERO sz) bin = Nothing
|
||||||
|
|||||||
@ -13,6 +13,12 @@ cases =
|
|||||||
, ("xyze", 5, "xyze\x01")
|
, ("xyze", 5, "xyze\x01")
|
||||||
]
|
]
|
||||||
|
|
||||||
|
zeroCases =
|
||||||
|
[ ("", 4, "\NUL\NUL\NUL\NUL")
|
||||||
|
, ("abcdef", 8, "abcdef\NUL\NUL")
|
||||||
|
, ("0123456789abcdef", 16, "0123456789abcdef")
|
||||||
|
]
|
||||||
|
|
||||||
--instance Arbitrary where
|
--instance Arbitrary where
|
||||||
|
|
||||||
testPad :: Int -> (B.ByteString, Int, B.ByteString) -> TestTree
|
testPad :: Int -> (B.ByteString, Int, B.ByteString) -> TestTree
|
||||||
@ -21,6 +27,11 @@ testPad n (inp, sz, padded) =
|
|||||||
, eqTest "unpadded" (Just inp) (unpad (PKCS7 sz) padded)
|
, eqTest "unpadded" (Just inp) (unpad (PKCS7 sz) padded)
|
||||||
]
|
]
|
||||||
|
|
||||||
|
testZeroPad :: Int -> (B.ByteString, Int, B.ByteString) -> TestTree
|
||||||
|
testZeroPad n (inp, sz, padded) =
|
||||||
|
testCase (show n) $ propertyHoldCase [ eqTest "padded" padded (pad (ZERO sz) inp) ]
|
||||||
|
|
||||||
tests = testGroup "Padding"
|
tests = testGroup "Padding"
|
||||||
[ testGroup "Cases" $ map (uncurry testPad) (zip [1..] cases)
|
[ testGroup "Cases" $ map (uncurry testPad) (zip [1..] cases)
|
||||||
|
, testGroup "ZeroCases" $ map (uncurry testZeroPad) (zip [1..] zeroCases)
|
||||||
]
|
]
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user