Report almost done.
[crypto_lab2.git] / labo2-fsharp / CryptoFile / Crypto.fs
index 836d636..24cebd0 100644 (file)
@@ -1,11 +1,11 @@
 namespace CryptoFile
 
+open System
+open System.IO
+open System.Security.Cryptography
+
 // Some cryptography primitives specific to CryptoFile.
 module internal Crypto =
-    open System
-    open System.IO
-    open System.Security.Cryptography
-
     type Data = byte[]
 
     let rsaKeySize = 2048
@@ -75,19 +75,13 @@ module internal Crypto =
         let decryptor = aes.CreateDecryptor (key, iv)
         new CryptoStream (inputStream, decryptor, CryptoStreamMode.Read)
 
+    // Create a stream to compute the HMAC-SHA256 against all data being written.
     let HMACStream (key: byte[]) (outputStream: Stream) : Stream * HMACSHA256 = 
         assert (key.Length = 32)
         let hmac = new HMACSHA256 (key)
         new CryptoStream (outputStream, hmac, CryptoStreamMode.Write) :> Stream, hmac
 
-    (*type HMACStream (buffer: byte[], output: Stream) =
-        inherit Stream () 
-        override this.CanRead with get () = false
-        override this.CanSeek with get () = false
-        override this.CanWrite with get () = true
-        override this.Length with get () = raise <| new NotSupportedException ()
-        override this.Position with get () = raise <| new NotSupportedException ()
-                               and set _ = raise <| new NotSupportedException ()
-        override this.Flush () =
-            output.Flush ()
-        override this.Read (_: byte[], _: int, _: int) = raise <| new NotSupportedException ()*)
\ No newline at end of file
+    let ComputeHMAC (key: byte[]) (inputStream: Stream) : byte[] =
+        assert (key.Length = 32)
+        let hmac = new HMACSHA256 (key)
+        hmac.ComputeHash inputStream
\ No newline at end of file