diff options
author | joe <joe@jerkface.net> | 2013-08-09 22:18:36 -0400 |
---|---|---|
committer | joe <joe@jerkface.net> | 2013-08-09 22:18:36 -0400 |
commit | 86a4081f0182bbd7b8249584e327e8b4df268a99 (patch) | |
tree | 2365aa0e16aa00682291310f7ccc73b484bd34c4 /Feistel.hs |
Initial commit
Diffstat (limited to 'Feistel.hs')
-rw-r--r-- | Feistel.hs | 50 |
1 files changed, 50 insertions, 0 deletions
diff --git a/Feistel.hs b/Feistel.hs new file mode 100644 index 0000000..2391452 --- /dev/null +++ b/Feistel.hs | |||
@@ -0,0 +1,50 @@ | |||
1 | {-# LANGUAGE UnboxedTuples #-} | ||
2 | module Feistel where | ||
3 | |||
4 | import qualified Data.ByteString as B | ||
5 | import qualified Data.ByteString.Lazy as L | ||
6 | import Data.Word | ||
7 | import Data.Bits | ||
8 | import Data.Binary | ||
9 | import System.Endian | ||
10 | |||
11 | toW32Pair :: B.ByteString -> ( Word32, Word32 ) | ||
12 | toW32Pair b = let (x1, x2) = B.splitAt 4 b | ||
13 | w1 = decode32be x1 | ||
14 | w2 = decode32be x2 | ||
15 | in ( w1,w2 ) | ||
16 | |||
17 | fromW32Pair :: (Word32, Word32) -> B.ByteString | ||
18 | fromW32Pair (w1,w2) | ||
19 | = let w1' = fromIntegral w1 | ||
20 | w2' = fromIntegral w2 | ||
21 | w = (w1' `shiftL` 32) .|. w2' | ||
22 | in encode64be w | ||
23 | |||
24 | |||
25 | decode32be :: B.ByteString -> Word32 | ||
26 | decode32be s = id $! | ||
27 | (fromIntegral (s `B.index` 0) `shiftL` 24) .|. | ||
28 | (fromIntegral (s `B.index` 1) `shiftL` 16) .|. | ||
29 | (fromIntegral (s `B.index` 2) `shiftL` 8) .|. | ||
30 | (fromIntegral (s `B.index` 3) ) | ||
31 | |||
32 | encode64be :: Word64 -> B.ByteString | ||
33 | encode64be w = B.pack . map fromIntegral $ | ||
34 | [ (w `shiftR` 56) .&. 0xff | ||
35 | , (w `shiftR` 48) .&. 0xff | ||
36 | , (w `shiftR` 40) .&. 0xff | ||
37 | , (w `shiftR` 32) .&. 0xff | ||
38 | , (w `shiftR` 24) .&. 0xff | ||
39 | , (w `shiftR` 16) .&. 0xff | ||
40 | , (w `shiftR` 8) .&. 0xff | ||
41 | , w .&. 0xff | ||
42 | ] | ||
43 | |||
44 | splitWord32 :: Word32 -> (# Word8, Word8, Word8, Word8 #) | ||
45 | splitWord32 w = (# a,b,c,d #) | ||
46 | where | ||
47 | a = fromIntegral $ w `shiftR` 24 | ||
48 | b = fromIntegral $ w `shiftR` 16 | ||
49 | c = fromIntegral $ w `shiftR` 8 | ||
50 | d = fromIntegral $ w | ||