File: Tests.hs

package info (click to toggle)
haskell-crypto-cipher-tests 0.0.11-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 80 kB
  • ctags: 1
  • sloc: haskell: 415; makefile: 3
file content (43 lines) | stat: -rw-r--r-- 1,288 bytes parent folder | download | duplicates (6)
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
43
{-# LANGUAGE ViewPatterns #-}
module Main where

import Test.Framework (defaultMain)
import Crypto.Cipher.Types
import Crypto.Cipher.Types.Unsafe
import Crypto.Cipher.Tests
import qualified Data.ByteString as B
import Data.Bits (xor)

-- | the XOR cipher is so awesome that it doesn't need any key or state.
-- Also it's a stream and block cipher at the same time.
data XorCipher = XorCipher

instance Cipher XorCipher where
    cipherInit _    = XorCipher
    cipherName _    = "xor"
    cipherKeySize _ = KeySizeRange 1 32

instance BlockCipher XorCipher where
    blockSize  _   = 16
    ecbEncrypt _ s = xorBS s
    ecbDecrypt _ s = xorBS s

instance BlockCipherIO XorCipher where
    ecbEncryptMutable cipher d s len = onBlock cipher xorBS d s len
    ecbDecryptMutable cipher d s len = onBlock cipher xorBS d s len

instance StreamCipher XorCipher where
    streamCombine _ b = (B.pack $ B.zipWith xor (B.replicate (B.length b) 0x12) b, XorCipher)

xorBS :: B.ByteString -> B.ByteString
xorBS b = B.pack $ B.zipWith xor (B.replicate (B.length b) 0xa5) b

tests =
    [ testBlockCipher defaultKATs cipher
    , testBlockCipherIO defaultKATs cipher
    , testStreamCipher defaultStreamKATs cipher
    ]
  where cipher :: XorCipher
        cipher = undefined

main = defaultMain tests