Skip to content

Commit

Permalink
code cleanup inn ilwrite.fs and il.fs
Browse files Browse the repository at this point in the history
  • Loading branch information
dsyme committed Nov 30, 2014
1 parent f2e301e commit a3ca155
Show file tree
Hide file tree
Showing 4 changed files with 407 additions and 457 deletions.
51 changes: 23 additions & 28 deletions src/absil/il.fs
Original file line number Diff line number Diff line change
Expand Up @@ -214,22 +214,18 @@ module SHA1 =
else k60to79


type chan = SHABytes of byte[]
type sha_instream =
{ stream: chan;
type SHAStream =
{ stream: byte[];
mutable pos: int;
mutable eof: bool; }

let rot_left32 x n = (x <<< n) ||| (x >>>& (32-n))
let rotLeft32 x n = (x <<< n) ||| (x >>>& (32-n))

let inline sha_eof sha = sha.eof

(* padding and length (in bits!) recorded at end *)
let sha_after_eof sha =

// padding and length (in bits!) recorded at end
let shaAfterEof sha =
let n = sha.pos
let len =
(match sha.stream with
| SHABytes s -> s.Length)
let len = sha.stream.Length
if n = len then 0x80
else
let padded_len = (((len + 9 + 63) / 64) * 64) - 8
Expand All @@ -244,22 +240,21 @@ module SHA1 =
elif (n &&& 63) = 63 then (sha.eof <- true; int32 (int64 len * int64 8) &&& 0xff)
else 0x0

let sha_read8 sha =
let b =
match sha.stream with
| SHABytes s -> if sha.pos >= s.Length then sha_after_eof sha else int32 s.[sha.pos]
sha.pos <- sha.pos + 1;
let shaRead8 sha =
let s = sha.stream
let b = if sha.pos >= s.Length then shaAfterEof sha else int32 s.[sha.pos]
sha.pos <- sha.pos + 1
b

let sha_read32 sha =
let b0 = sha_read8 sha
let b1 = sha_read8 sha
let b2 = sha_read8 sha
let b3 = sha_read8 sha
let shaRead32 sha =
let b0 = shaRead8 sha
let b1 = shaRead8 sha
let b2 = shaRead8 sha
let b3 = shaRead8 sha
let res = (b0 <<< 24) ||| (b1 <<< 16) ||| (b2 <<< 8) ||| b3
res

let sha1_hash sha =
let sha1Hash sha =
let mutable h0 = 0x67452301
let mutable h1 = 0xEFCDAB89
let mutable h2 = 0x98BADCFE
Expand All @@ -271,21 +266,21 @@ module SHA1 =
let mutable d = 0
let mutable e = 0
let w = Array.create 80 0x00
while (not (sha_eof sha)) do
while (not sha.eof) do
for i = 0 to 15 do
w.[i] <- sha_read32 sha
w.[i] <- shaRead32 sha
for t = 16 to 79 do
w.[t] <- rot_left32 (w.[t-3] ^^^ w.[t-8] ^^^ w.[t-14] ^^^ w.[t-16]) 1
w.[t] <- rotLeft32 (w.[t-3] ^^^ w.[t-8] ^^^ w.[t-14] ^^^ w.[t-16]) 1
a <- h0
b <- h1
c <- h2
d <- h3
e <- h4
for t = 0 to 79 do
let temp = (rot_left32 a 5) + f(t,b,c,d) + e + w.[t] + k(t)
let temp = (rotLeft32 a 5) + f(t,b,c,d) + e + w.[t] + k(t)
e <- d
d <- c
c <- rot_left32 b 30
c <- rotLeft32 b 30
b <- a
a <- temp
h0 <- h0 + a
Expand All @@ -296,7 +291,7 @@ module SHA1 =
h0,h1,h2,h3,h4

let sha1HashBytes s =
let (_h0,_h1,_h2,h3,h4) = sha1_hash { stream = SHABytes s; pos = 0; eof = false } // the result of the SHA algorithm is stored in registers 3 and 4
let (_h0,_h1,_h2,h3,h4) = sha1Hash { stream = s; pos = 0; eof = false } // the result of the SHA algorithm is stored in registers 3 and 4
Array.map byte [| b0 h4; b1 h4; b2 h4; b3 h4; b0 h3; b1 h3; b2 h3; b3 h3; |]


Expand Down
Loading

0 comments on commit a3ca155

Please sign in to comment.