add zero padding and its test.

This commit is contained in:
Kei Hibino 2016-06-08 22:23:41 +09:00
parent 87867b49bc
commit c2285db4e3
2 changed files with 22 additions and 0 deletions

View File

@ -21,6 +21,7 @@ import qualified Data.ByteArray as B
data Format =
PKCS5 -- ^ PKCS5: PKCS7 with hardcoded size of 8
| PKCS7 Int -- ^ PKCS7 with padding size between 1 and 255
| ZERO Int -- ^ zero padding with block size
deriving (Show, Eq)
-- | Apply some pad to a bytearray
@ -30,6 +31,15 @@ pad (PKCS7 sz) bin = bin `B.append` paddingString
where
paddingString = B.replicate paddingByte (fromIntegral paddingByte)
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.
unpad :: ByteArray byteArray => Format -> byteArray -> Maybe byteArray
@ -46,3 +56,4 @@ unpad (PKCS7 sz) bin
paddingSz = fromIntegral paddingByte
(content, padding) = B.splitAt (len - paddingSz) bin
paddingWitness = B.replicate paddingSz paddingByte :: Bytes
unpad (ZERO sz) bin = Nothing

View File

@ -13,6 +13,12 @@ cases =
, ("xyze", 5, "xyze\x01")
]
zeroCases =
[ ("", 4, "\NUL\NUL\NUL\NUL")
, ("abcdef", 8, "abcdef\NUL\NUL")
, ("0123456789abcdef", 16, "0123456789abcdef")
]
--instance Arbitrary where
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)
]
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"
[ testGroup "Cases" $ map (uncurry testPad) (zip [1..] cases)
, testGroup "ZeroCases" $ map (uncurry testZeroPad) (zip [1..] zeroCases)
]