{-# LANGUAGE Safe #-}
{-# OPTIONS_GHC -fno-warn-missing-methods #-}
module Data.Hash.MD5
( md5
, md5s
, md5i
, MD5(..)
, ABCD(..)
, Zord64
, Str(..)
, BoolList(..)
, WordList(..)
) where
import safe Data.Bits (Bits (complement, rotateL, shiftL, shiftR, xor, (.&.), (.|.)))
import safe Data.Char (chr, ord)
import safe Data.Word (Word32, Word64)
type Zord64 = Word64
type XYZ = (Word32, Word32, Word32)
type Rotation = Int
newtype ABCD = ABCD (Word32, Word32, Word32, Word32) deriving (ABCD -> ABCD -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: ABCD -> ABCD -> Bool
$c/= :: ABCD -> ABCD -> Bool
== :: ABCD -> ABCD -> Bool
$c== :: ABCD -> ABCD -> Bool
Eq, Int -> ABCD -> ShowS
[ABCD] -> ShowS
ABCD -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [ABCD] -> ShowS
$cshowList :: [ABCD] -> ShowS
show :: ABCD -> String
$cshow :: ABCD -> String
showsPrec :: Int -> ABCD -> ShowS
$cshowsPrec :: Int -> ABCD -> ShowS
Show)
newtype Str = Str String
newtype BoolList = BoolList [Bool]
newtype WordList = WordList ([Word32], Zord64)
class MD5 a where
get_next :: a -> ([Word32], Int, a)
len_pad :: Zord64 -> a -> a
finished :: a -> Bool
instance MD5 BoolList where
get_next :: BoolList -> ([Word32], Int, BoolList)
get_next (BoolList [Bool]
s) = ([Bool] -> [Word32]
bools_to_word32s [Bool]
ys, forall (t :: * -> *) a. Foldable t => t a -> Int
length [Bool]
ys, [Bool] -> BoolList
BoolList [Bool]
zs)
where ([Bool]
ys, [Bool]
zs) = forall a. Int -> [a] -> ([a], [a])
splitAt Int
512 [Bool]
s
len_pad :: Zord64 -> BoolList -> BoolList
len_pad Zord64
l (BoolList [Bool]
bs)
= [Bool] -> BoolList
BoolList ([Bool]
bs forall a. [a] -> [a] -> [a]
++ [Bool
True]
forall a. [a] -> [a] -> [a]
++ forall a. Int -> a -> [a]
replicate (forall a b. (Integral a, Num b) => a -> b
fromIntegral forall a b. (a -> b) -> a -> b
$ (Zord64
447 forall a. Num a => a -> a -> a
- Zord64
l) forall a. Bits a => a -> a -> a
.&. Zord64
511) Bool
False
forall a. [a] -> [a] -> [a]
++ [Zord64
l forall a. Bits a => a -> a -> a
.&. (forall a. Bits a => a -> Int -> a
shiftL Zord64
1 Int
x) forall a. Ord a => a -> a -> Bool
> Zord64
0 | Int
x <- (forall {a}. [a] -> [a]
mangle [Int
0..Int
63])]
)
where mangle :: [a] -> [a]
mangle [] = []
mangle [a]
xs = forall {a}. [a] -> [a]
reverse [a]
ys forall a. [a] -> [a] -> [a]
++ [a] -> [a]
mangle [a]
zs
where ([a]
ys, [a]
zs) = forall a. Int -> [a] -> ([a], [a])
splitAt Int
8 [a]
xs
finished :: BoolList -> Bool
finished (BoolList [Bool]
s) = [Bool]
s forall a. Eq a => a -> a -> Bool
== []
instance MD5 Str where
get_next :: Str -> ([Word32], Int, Str)
get_next (Str String
s) = (String -> [Word32]
string_to_word32s String
ys, Int
8 forall a. Num a => a -> a -> a
* forall (t :: * -> *) a. Foldable t => t a -> Int
length String
ys, String -> Str
Str String
zs)
where (String
ys, String
zs) = forall a. Int -> [a] -> ([a], [a])
splitAt Int
64 String
s
len_pad :: Zord64 -> Str -> Str
len_pad Zord64
c64 (Str String
s) = String -> Str
Str (String
s forall a. [a] -> [a] -> [a]
++ String
padding forall a. [a] -> [a] -> [a]
++ String
l)
where padding :: String
padding = Char
'\128'forall a. a -> [a] -> [a]
:forall a. Int -> a -> [a]
replicate (forall a b. (Integral a, Num b) => a -> b
fromIntegral Zord64
zeros) Char
'\000'
zeros :: Zord64
zeros = forall a. Bits a => a -> Int -> a
shiftR ((Zord64
440 forall a. Num a => a -> a -> a
- Zord64
c64) forall a. Bits a => a -> a -> a
.&. Zord64
511) Int
3
l :: String
l = Int -> Zord64 -> String
length_to_chars Int
8 Zord64
c64
finished :: Str -> Bool
finished (Str String
s) = String
s forall a. Eq a => a -> a -> Bool
== String
""
instance MD5 WordList where
get_next :: WordList -> ([Word32], Int, WordList)
get_next (WordList ([Word32]
ws, Zord64
l)) = ([Word32]
xs, forall a b. (Integral a, Num b) => a -> b
fromIntegral Zord64
taken, ([Word32], Zord64) -> WordList
WordList ([Word32]
ys, Zord64
l forall a. Num a => a -> a -> a
- Zord64
taken))
where ([Word32]
xs, [Word32]
ys) = forall a. Int -> [a] -> ([a], [a])
splitAt Int
16 [Word32]
ws
taken :: Zord64
taken = if Zord64
l forall a. Ord a => a -> a -> Bool
> Zord64
511 then Zord64
512 else Zord64
l forall a. Bits a => a -> a -> a
.&. Zord64
511
len_pad :: Zord64 -> WordList -> WordList
len_pad Zord64
c64 (WordList ([Word32]
ws, Zord64
l)) = ([Word32], Zord64) -> WordList
WordList ([Word32]
beginning forall a. [a] -> [a] -> [a]
++ [Word32]
nextish forall a. [a] -> [a] -> [a]
++ [Word32]
blanks forall a. [a] -> [a] -> [a]
++ [Word32]
size, Zord64
newlen)
where beginning :: [Word32]
beginning = if forall (t :: * -> *) a. Foldable t => t a -> Int
length [Word32]
ws forall a. Ord a => a -> a -> Bool
> Int
0 then [Word32]
start forall a. [a] -> [a] -> [a]
++ [Word32]
lastone' else []
start :: [Word32]
start = forall {a}. [a] -> [a]
init [Word32]
ws
lastone :: Word32
lastone = forall a. [a] -> a
last [Word32]
ws
offset :: Zord64
offset = Zord64
c64 forall a. Bits a => a -> a -> a
.&. Zord64
31
lastone' :: [Word32]
lastone' = [if Zord64
offset forall a. Ord a => a -> a -> Bool
> Zord64
0 then Word32
lastone forall a. Num a => a -> a -> a
+ Word32
theone else Word32
lastone]
theone :: Word32
theone = forall a. Bits a => a -> Int -> a
shiftL (forall a. Bits a => a -> Int -> a
shiftR Word32
128 (forall a b. (Integral a, Num b) => a -> b
fromIntegral forall a b. (a -> b) -> a -> b
$ Zord64
offset forall a. Bits a => a -> a -> a
.&. Zord64
7))
(forall a b. (Integral a, Num b) => a -> b
fromIntegral forall a b. (a -> b) -> a -> b
$ Zord64
offset forall a. Bits a => a -> a -> a
.&. (Zord64
31 forall a. Num a => a -> a -> a
- Zord64
7))
nextish :: [Word32]
nextish = if Zord64
offset forall a. Eq a => a -> a -> Bool
== Zord64
0 then [Word32
128] else []
c64' :: Zord64
c64' = Zord64
c64 forall a. Num a => a -> a -> a
+ (Zord64
32 forall a. Num a => a -> a -> a
- Zord64
offset)
num_blanks :: Int
num_blanks = (forall a b. (Integral a, Num b) => a -> b
fromIntegral forall a b. (a -> b) -> a -> b
$ forall a. Bits a => a -> Int -> a
shiftR ((Zord64
448 forall a. Num a => a -> a -> a
- Zord64
c64') forall a. Bits a => a -> a -> a
.&. Zord64
511) Int
5)
blanks :: [Word32]
blanks = forall a. Int -> a -> [a]
replicate Int
num_blanks Word32
0
lowsize :: Word32
lowsize = forall a b. (Integral a, Num b) => a -> b
fromIntegral forall a b. (a -> b) -> a -> b
$ Zord64
c64 forall a. Bits a => a -> a -> a
.&. (forall a. Bits a => a -> Int -> a
shiftL Zord64
1 Int
32 forall a. Num a => a -> a -> a
- Zord64
1)
topsize :: Word32
topsize = forall a b. (Integral a, Num b) => a -> b
fromIntegral forall a b. (a -> b) -> a -> b
$ forall a. Bits a => a -> Int -> a
shiftR Zord64
c64 Int
32
size :: [Word32]
size = [Word32
lowsize, Word32
topsize]
newlen :: Zord64
newlen = Zord64
l forall a. Bits a => a -> a -> a
.&. (forall a. Bits a => a -> a
complement Zord64
511)
forall a. Num a => a -> a -> a
+ if Zord64
c64 forall a. Bits a => a -> a -> a
.&. Zord64
511 forall a. Ord a => a -> a -> Bool
>= Zord64
448 then Zord64
1024 else Zord64
512
finished :: WordList -> Bool
finished (WordList ([Word32]
_, Zord64
z)) = Zord64
z forall a. Eq a => a -> a -> Bool
== Zord64
0
instance Num ABCD where
ABCD (Word32
a1, Word32
b1, Word32
c1, Word32
d1) + :: ABCD -> ABCD -> ABCD
+ ABCD (Word32
a2, Word32
b2, Word32
c2, Word32
d2) = (Word32, Word32, Word32, Word32) -> ABCD
ABCD (Word32
a1 forall a. Num a => a -> a -> a
+ Word32
a2, Word32
b1 forall a. Num a => a -> a -> a
+ Word32
b2, Word32
c1 forall a. Num a => a -> a -> a
+ Word32
c2, Word32
d1 forall a. Num a => a -> a -> a
+ Word32
d2)
md5 :: (MD5 a) => a -> ABCD
md5 :: forall a. MD5 a => a -> ABCD
md5 a
m = forall a. MD5 a => Bool -> Zord64 -> ABCD -> a -> ABCD
md5_main Bool
False Zord64
0 ABCD
magic_numbers a
m
md5s :: (MD5 a) => a -> String
md5s :: forall a. MD5 a => a -> String
md5s = ABCD -> String
abcd_to_string forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. MD5 a => a -> ABCD
md5
md5i :: (MD5 a) => a -> Integer
md5i :: forall a. MD5 a => a -> Integer
md5i = ABCD -> Integer
abcd_to_integer forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. MD5 a => a -> ABCD
md5
md5_main :: (MD5 a) =>
Bool
-> Zord64
-> ABCD
-> a
-> ABCD
md5_main :: forall a. MD5 a => Bool -> Zord64 -> ABCD -> a -> ABCD
md5_main Bool
padded Zord64
ilen ABCD
abcd a
m
= if forall a. MD5 a => a -> Bool
finished a
m Bool -> Bool -> Bool
&& Bool
padded
then ABCD
abcd
else forall a. MD5 a => Bool -> Zord64 -> ABCD -> a -> ABCD
md5_main Bool
padded' (Zord64
ilen forall a. Num a => a -> a -> a
+ Zord64
512) (ABCD
abcd forall a. Num a => a -> a -> a
+ ABCD
abcd') a
m''
where ([Word32]
m16, Int
l, a
m') = forall a. MD5 a => a -> ([Word32], Int, a)
get_next a
m
len' :: Zord64
len' = Zord64
ilen forall a. Num a => a -> a -> a
+ forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
l
(([Word32]
m16', Int
_, a
m''), Bool
padded') = if Bool -> Bool
not Bool
padded Bool -> Bool -> Bool
&& Int
l forall a. Ord a => a -> a -> Bool
< Int
512
then (forall a. MD5 a => a -> ([Word32], Int, a)
get_next forall a b. (a -> b) -> a -> b
$ forall a. MD5 a => Zord64 -> a -> a
len_pad Zord64
len' a
m, Bool
True)
else (([Word32]
m16, Int
l, a
m'), Bool
padded)
abcd' :: ABCD
abcd' = ABCD -> [Word32] -> ABCD
md5_do_block ABCD
abcd [Word32]
m16'
md5_do_block :: ABCD
-> [Word32]
-> ABCD
md5_do_block :: ABCD -> [Word32] -> ABCD
md5_do_block ABCD
abcd0 [Word32]
w = ABCD
abcd4
where ([(Int, Word32)]
r1, [(Int, Word32)]
r2, [(Int, Word32)]
r3, [(Int, Word32)]
r4) = ([(Int, Word32)], [(Int, Word32)], [(Int, Word32)],
[(Int, Word32)])
rounds
perm5 :: [a] -> [a]
perm5 [a
c0,a
c1,a
c2,a
c3,a
c4,a
c5,a
c6,a
c7,a
c8,a
c9,a
c10,a
c11,a
c12,a
c13,a
c14,a
c15]
= [a
c1,a
c6,a
c11,a
c0,a
c5,a
c10,a
c15,a
c4,a
c9,a
c14,a
c3,a
c8,a
c13,a
c2,a
c7,a
c12]
perm5 [a]
_ = forall a. HasCallStack => String -> a
error String
"broke at perm5"
perm3 :: [a] -> [a]
perm3 [a
c0,a
c1,a
c2,a
c3,a
c4,a
c5,a
c6,a
c7,a
c8,a
c9,a
c10,a
c11,a
c12,a
c13,a
c14,a
c15]
= [a
c5,a
c8,a
c11,a
c14,a
c1,a
c4,a
c7,a
c10,a
c13,a
c0,a
c3,a
c6,a
c9,a
c12,a
c15,a
c2]
perm3 [a]
_ = forall a. HasCallStack => String -> a
error String
"broke at perm3"
perm7 :: [a] -> [a]
perm7 [a
c0,a
c1,a
c2,a
c3,a
c4,a
c5,a
c6,a
c7,a
c8,a
c9,a
c10,a
c11,a
c12,a
c13,a
c14,a
c15]
= [a
c0,a
c7,a
c14,a
c5,a
c12,a
c3,a
c10,a
c1,a
c8,a
c15,a
c6,a
c13,a
c4,a
c11,a
c2,a
c9]
perm7 [a]
_ = forall a. HasCallStack => String -> a
error String
"broke at perm7"
abcd1 :: ABCD
abcd1 = (XYZ -> Word32) -> ABCD -> [Word32] -> [(Int, Word32)] -> ABCD
md5_round XYZ -> Word32
md5_f ABCD
abcd0 [Word32]
w [(Int, Word32)]
r1
abcd2 :: ABCD
abcd2 = (XYZ -> Word32) -> ABCD -> [Word32] -> [(Int, Word32)] -> ABCD
md5_round XYZ -> Word32
md5_g ABCD
abcd1 (forall {a}. [a] -> [a]
perm5 [Word32]
w) [(Int, Word32)]
r2
abcd3 :: ABCD
abcd3 = (XYZ -> Word32) -> ABCD -> [Word32] -> [(Int, Word32)] -> ABCD
md5_round XYZ -> Word32
md5_h ABCD
abcd2 (forall {a}. [a] -> [a]
perm3 [Word32]
w) [(Int, Word32)]
r3
abcd4 :: ABCD
abcd4 = (XYZ -> Word32) -> ABCD -> [Word32] -> [(Int, Word32)] -> ABCD
md5_round XYZ -> Word32
md5_i ABCD
abcd3 (forall {a}. [a] -> [a]
perm7 [Word32]
w) [(Int, Word32)]
r4
md5_round :: (XYZ -> Word32)
-> ABCD
-> [Word32]
-> [(Rotation, Word32)]
-> ABCD
md5_round :: (XYZ -> Word32) -> ABCD -> [Word32] -> [(Int, Word32)] -> ABCD
md5_round XYZ -> Word32
f ABCD
abcd [Word32]
s [(Int, Word32)]
ns = forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
foldl ((XYZ -> Word32) -> ABCD -> (Int, Word32) -> ABCD
md5_inner_function XYZ -> Word32
f) ABCD
abcd [(Int, Word32)]
ns'
where ns' :: [(Int, Word32)]
ns' = forall a b c. (a -> b -> c) -> [a] -> [b] -> [c]
zipWith (\Word32
x (Int
y, Word32
z) -> (Int
y, Word32
x forall a. Num a => a -> a -> a
+ Word32
z)) [Word32]
s [(Int, Word32)]
ns
md5_inner_function :: (XYZ -> Word32)
-> ABCD
-> (Rotation, Word32)
-> ABCD
md5_inner_function :: (XYZ -> Word32) -> ABCD -> (Int, Word32) -> ABCD
md5_inner_function XYZ -> Word32
f (ABCD (Word32
a, Word32
b, Word32
c, Word32
d)) (Int
s, Word32
ki) = (Word32, Word32, Word32, Word32) -> ABCD
ABCD (Word32
d, Word32
a', Word32
b, Word32
c)
where mid_a :: Word32
mid_a = Word32
a forall a. Num a => a -> a -> a
+ XYZ -> Word32
f(Word32
b,Word32
c,Word32
d) forall a. Num a => a -> a -> a
+ Word32
ki
rot_a :: Word32
rot_a = forall a. Bits a => a -> Int -> a
rotateL Word32
mid_a Int
s
a' :: Word32
a' = Word32
b forall a. Num a => a -> a -> a
+ Word32
rot_a
md5_f :: XYZ -> Word32
md5_f :: XYZ -> Word32
md5_f (Word32
x, Word32
y, Word32
z) = Word32
z forall a. Bits a => a -> a -> a
`xor` (Word32
x forall a. Bits a => a -> a -> a
.&. (Word32
y forall a. Bits a => a -> a -> a
`xor` Word32
z))
md5_g :: XYZ -> Word32
md5_g :: XYZ -> Word32
md5_g (Word32
x, Word32
y, Word32
z) = XYZ -> Word32
md5_f (Word32
z, Word32
x, Word32
y)
md5_h :: XYZ -> Word32
md5_h :: XYZ -> Word32
md5_h (Word32
x, Word32
y, Word32
z) = Word32
x forall a. Bits a => a -> a -> a
`xor` Word32
y forall a. Bits a => a -> a -> a
`xor` Word32
z
md5_i :: XYZ -> Word32
md5_i :: XYZ -> Word32
md5_i (Word32
x, Word32
y, Word32
z) = Word32
y forall a. Bits a => a -> a -> a
`xor` (Word32
x forall a. Bits a => a -> a -> a
.|. (forall a. Bits a => a -> a
complement Word32
z))
magic_numbers :: ABCD
magic_numbers :: ABCD
magic_numbers = (Word32, Word32, Word32, Word32) -> ABCD
ABCD (Word32
0x67452301, Word32
0xefcdab89, Word32
0x98badcfe, Word32
0x10325476)
rounds :: ([(Rotation, Word32)],
[(Rotation, Word32)],
[(Rotation, Word32)],
[(Rotation, Word32)])
rounds :: ([(Int, Word32)], [(Int, Word32)], [(Int, Word32)],
[(Int, Word32)])
rounds = ([(Int, Word32)]
r1, [(Int, Word32)]
r2, [(Int, Word32)]
r3, [(Int, Word32)]
r4)
where r1 :: [(Int, Word32)]
r1 = [(Int
s11, Word32
0xd76aa478), (Int
s12, Word32
0xe8c7b756), (Int
s13, Word32
0x242070db),
(Int
s14, Word32
0xc1bdceee), (Int
s11, Word32
0xf57c0faf), (Int
s12, Word32
0x4787c62a),
(Int
s13, Word32
0xa8304613), (Int
s14, Word32
0xfd469501), (Int
s11, Word32
0x698098d8),
(Int
s12, Word32
0x8b44f7af), (Int
s13, Word32
0xffff5bb1), (Int
s14, Word32
0x895cd7be),
(Int
s11, Word32
0x6b901122), (Int
s12, Word32
0xfd987193), (Int
s13, Word32
0xa679438e),
(Int
s14, Word32
0x49b40821)]
r2 :: [(Int, Word32)]
r2 = [(Int
s21, Word32
0xf61e2562), (Int
s22, Word32
0xc040b340), (Int
s23, Word32
0x265e5a51),
(Int
s24, Word32
0xe9b6c7aa), (Int
s21, Word32
0xd62f105d), (Int
s22, Word32
0x2441453),
(Int
s23, Word32
0xd8a1e681), (Int
s24, Word32
0xe7d3fbc8), (Int
s21, Word32
0x21e1cde6),
(Int
s22, Word32
0xc33707d6), (Int
s23, Word32
0xf4d50d87), (Int
s24, Word32
0x455a14ed),
(Int
s21, Word32
0xa9e3e905), (Int
s22, Word32
0xfcefa3f8), (Int
s23, Word32
0x676f02d9),
(Int
s24, Word32
0x8d2a4c8a)]
r3 :: [(Int, Word32)]
r3 = [(Int
s31, Word32
0xfffa3942), (Int
s32, Word32
0x8771f681), (Int
s33, Word32
0x6d9d6122),
(Int
s34, Word32
0xfde5380c), (Int
s31, Word32
0xa4beea44), (Int
s32, Word32
0x4bdecfa9),
(Int
s33, Word32
0xf6bb4b60), (Int
s34, Word32
0xbebfbc70), (Int
s31, Word32
0x289b7ec6),
(Int
s32, Word32
0xeaa127fa), (Int
s33, Word32
0xd4ef3085), (Int
s34, Word32
0x4881d05),
(Int
s31, Word32
0xd9d4d039), (Int
s32, Word32
0xe6db99e5), (Int
s33, Word32
0x1fa27cf8),
(Int
s34, Word32
0xc4ac5665)]
r4 :: [(Int, Word32)]
r4 = [(Int
s41, Word32
0xf4292244), (Int
s42, Word32
0x432aff97), (Int
s43, Word32
0xab9423a7),
(Int
s44, Word32
0xfc93a039), (Int
s41, Word32
0x655b59c3), (Int
s42, Word32
0x8f0ccc92),
(Int
s43, Word32
0xffeff47d), (Int
s44, Word32
0x85845dd1), (Int
s41, Word32
0x6fa87e4f),
(Int
s42, Word32
0xfe2ce6e0), (Int
s43, Word32
0xa3014314), (Int
s44, Word32
0x4e0811a1),
(Int
s41, Word32
0xf7537e82), (Int
s42, Word32
0xbd3af235), (Int
s43, Word32
0x2ad7d2bb),
(Int
s44, Word32
0xeb86d391)]
s11 :: Int
s11 = Int
7
s12 :: Int
s12 = Int
12
s13 :: Int
s13 = Int
17
s14 :: Int
s14 = Int
22
s21 :: Int
s21 = Int
5
s22 :: Int
s22 = Int
9
s23 :: Int
s23 = Int
14
s24 :: Int
s24 = Int
20
s31 :: Int
s31 = Int
4
s32 :: Int
s32 = Int
11
s33 :: Int
s33 = Int
16
s34 :: Int
s34 = Int
23
s41 :: Int
s41 = Int
6
s42 :: Int
s42 = Int
10
s43 :: Int
s43 = Int
15
s44 :: Int
s44 = Int
21
abcd_to_string :: ABCD -> String
abcd_to_string :: ABCD -> String
abcd_to_string (ABCD (Word32
a,Word32
b,Word32
c,Word32
d)) = forall (t :: * -> *) a. Foldable t => t [a] -> [a]
concat forall a b. (a -> b) -> a -> b
$ forall a b. (a -> b) -> [a] -> [b]
map Word32 -> String
display_32bits_as_hex [Word32
a,Word32
b,Word32
c,Word32
d]
display_32bits_as_hex :: Word32 -> String
display_32bits_as_hex :: Word32 -> String
display_32bits_as_hex Word32
w = forall {a}. [a] -> [a]
swap_pairs String
cs
where cs :: String
cs = forall a b. (a -> b) -> [a] -> [b]
map (\Int
x -> forall {a}. Integral a => a -> Char
getc forall a b. (a -> b) -> a -> b
$ (forall a. Bits a => a -> Int -> a
shiftR Word32
w (Int
4forall a. Num a => a -> a -> a
*Int
x)) forall a. Bits a => a -> a -> a
.&. Word32
15) [Int
0..Int
7]
getc :: a -> Char
getc a
n = ([Char
'0'..Char
'9'] forall a. [a] -> [a] -> [a]
++ [Char
'a'..Char
'f']) forall a. [a] -> Int -> a
!! (forall a b. (Integral a, Num b) => a -> b
fromIntegral a
n)
swap_pairs :: [a] -> [a]
swap_pairs (a
x1:a
x2:[a]
xs) = a
x2forall a. a -> [a] -> [a]
:a
x1forall a. a -> [a] -> [a]
:[a] -> [a]
swap_pairs [a]
xs
swap_pairs [a]
_ = []
abcd_to_integer :: ABCD -> Integer
abcd_to_integer :: ABCD -> Integer
abcd_to_integer (ABCD (Word32
a,Word32
b,Word32
c,Word32
d)) = Word32 -> Integer
rev_num Word32
a forall a. Num a => a -> a -> a
* Integer
2forall a b. (Num a, Integral b) => a -> b -> a
^(Int
96 :: Int)
forall a. Num a => a -> a -> a
+ Word32 -> Integer
rev_num Word32
b forall a. Num a => a -> a -> a
* Integer
2forall a b. (Num a, Integral b) => a -> b -> a
^(Int
64 :: Int)
forall a. Num a => a -> a -> a
+ Word32 -> Integer
rev_num Word32
c forall a. Num a => a -> a -> a
* Integer
2forall a b. (Num a, Integral b) => a -> b -> a
^(Int
32 :: Int)
forall a. Num a => a -> a -> a
+ Word32 -> Integer
rev_num Word32
d
rev_num :: Word32 -> Integer
rev_num :: Word32 -> Integer
rev_num Word32
i = forall a. Integral a => a -> Integer
toInteger Word32
j forall a. Integral a => a -> a -> a
`mod` (Integer
2forall a b. (Num a, Integral b) => a -> b -> a
^(Int
32 :: Int))
where j :: Word32
j = forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
foldl (\Word32
so_far Int
next -> forall a. Bits a => a -> Int -> a
shiftL Word32
so_far Int
8 forall a. Num a => a -> a -> a
+ (forall a. Bits a => a -> Int -> a
shiftR Word32
i Int
next forall a. Bits a => a -> a -> a
.&. Word32
255))
Word32
0 [Int
0,Int
8,Int
16,Int
24]
string_to_word32s :: String -> [Word32]
string_to_word32s :: String -> [Word32]
string_to_word32s String
"" = []
string_to_word32s String
ss = Word32
thisforall a. a -> [a] -> [a]
:String -> [Word32]
string_to_word32s String
ss'
where (String
s, String
ss') = forall a. Int -> [a] -> ([a], [a])
splitAt Int
4 String
ss
this :: Word32
this = forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr (\Char
c Word32
w -> forall a. Bits a => a -> Int -> a
shiftL Word32
w Int
8 forall a. Num a => a -> a -> a
+ (forall a b. (Integral a, Num b) => a -> b
fromIntegralforall b c a. (b -> c) -> (a -> b) -> a -> c
.Char -> Int
ord) Char
c) Word32
0 String
s
bools_to_word32s :: [Bool] -> [Word32]
bools_to_word32s :: [Bool] -> [Word32]
bools_to_word32s [] = []
bools_to_word32s [Bool]
bs = Word32
thisforall a. a -> [a] -> [a]
:[Bool] -> [Word32]
bools_to_word32s [Bool]
rest
where ([Bool]
bs1, [Bool]
bs1') = forall a. Int -> [a] -> ([a], [a])
splitAt Int
8 [Bool]
bs
([Bool]
bs2, [Bool]
bs2') = forall a. Int -> [a] -> ([a], [a])
splitAt Int
8 [Bool]
bs1'
([Bool]
bs3, [Bool]
bs3') = forall a. Int -> [a] -> ([a], [a])
splitAt Int
8 [Bool]
bs2'
([Bool]
bs4, [Bool]
rest) = forall a. Int -> [a] -> ([a], [a])
splitAt Int
8 [Bool]
bs3'
this :: Word32
this = [[Bool]] -> Word32
boolss_to_word32 [[Bool]
bs1, [Bool]
bs2, [Bool]
bs3, [Bool]
bs4]
bools_to_word8 :: [Bool] -> Word32
bools_to_word8 = forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
foldl (\Word32
w Bool
b -> forall a. Bits a => a -> Int -> a
shiftL Word32
w Int
1 forall a. Num a => a -> a -> a
+ if Bool
b then Word32
1 else Word32
0) Word32
0
boolss_to_word32 :: [[Bool]] -> Word32
boolss_to_word32 = forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr (\[Bool]
w8 Word32
w -> forall a. Bits a => a -> Int -> a
shiftL Word32
w Int
8 forall a. Num a => a -> a -> a
+ [Bool] -> Word32
bools_to_word8 [Bool]
w8) Word32
0
length_to_chars :: Int -> Zord64 -> String
length_to_chars :: Int -> Zord64 -> String
length_to_chars Int
0 Zord64
_ = []
length_to_chars Int
p Zord64
n = Char
thisforall a. a -> [a] -> [a]
:Int -> Zord64 -> String
length_to_chars (Int
pforall a. Num a => a -> a -> a
-Int
1) (forall a. Bits a => a -> Int -> a
shiftR Zord64
n Int
8)
where this :: Char
this = Int -> Char
chr forall a b. (a -> b) -> a -> b
$ forall a b. (Integral a, Num b) => a -> b
fromIntegral forall a b. (a -> b) -> a -> b
$ Zord64
n forall a. Bits a => a -> a -> a
.&. Zord64
255