Typos in report + clean up.
[crypto_lab2.git] / labo2-fsharp / CryptoFile / API.fs
index f542f49..925f933 100644 (file)
@@ -3,21 +3,21 @@
 open System
 open System.IO
 
-// To read and write metadata.
+/// To read and write metadata.
 type internal Metadata (d: (string * string) list) =
-    // Read metadata from a stream.
+    /// Read metadata from a stream.
     new (stream : Stream) =
         let reader = new BinaryReader (stream)
         let length = reader.ReadByte () |> int
-        Metadata ([for _ in 1..length -> reader.ReadString (), reader.ReadString ()])
+        Metadata ([for _ in 1 .. length -> reader.ReadString (), reader.ReadString ()])
 
-    // Write metadata to a stream.
+    /// Write metadata to a stream.
     member this.WriteTo (stream : Stream) =
         let writer = new BinaryWriter (stream)
         writer.Write (byte d.Length)
         List.iter (fun (key : string, value : string) -> writer.Write key; writer.Write value) d
 
-    // May raise 'KeyNotFoundException'.
+    /// May raise 'KeyNotFoundException'.
     member this.get (key: string) : string =
         List.pick (function
                     | (k, v) when k = key -> Some (v)
@@ -37,9 +37,9 @@ module API =
 
     let generatKeysPair () : Key * Key = Crypto.generateRSAKeysPair ()
 
-    // Format of the container:
-    //  <mac><signature><encrypted keys><cyphertext>
-    // Where the sizes of the three first parts are given by 'hmacSize', 'signatureSize' and 'keysSize'.
+    /// Format of the container:
+    ///  <mac><signature><encrypted keys><cyphertext>
+    /// Where the sizes of the three first parts are given by 'hmacSize', 'signatureSize' and 'keysSize'.
     let encryptFile (inputFilePath : string) (outputFilePath : string) (signaturePrivKey: Key) (cryptPubKey : Key)  =
         let keyAES, keyMAC, iv = Crypto.rand 16, Crypto.rand 32, Crypto.rand 16
         let fileInfo = FileInfo (inputFilePath)
@@ -72,10 +72,10 @@ module API =
         // Write the signature.
         Crypto.signRSA signaturePrivKey hmac.Hash |> writer.Write
 
-    // May raise one of the following error:
-    //  * IntegrityError
-    //  * SignatureMismatch 
-    //  * UnableToDecryptAESKeys
+    /// May raise one of the following error:
+    ///  * IntegrityError
+    ///  * SignatureMismatch 
+    ///  * UnableToDecryptAESKeys
     let decryptFile (sourceFilePath : string) (targetDirPath : string) (signaturePubKey: Key) (decryptPrivKey : Key) =
         use inputStream = new FileStream (sourceFilePath, FileMode.Open, FileAccess.Read)
         use reader = new BinaryReader (inputStream)
@@ -85,9 +85,9 @@ module API =
             try reader.ReadBytes keysSize |> Crypto.decryptRSA decryptPrivKey
             with 
                 | :? Security.Cryptography.CryptographicException -> raise UnableToDecryptKeys
-        let keyAES = keys.[0..15]
-        let keyMAC = keys.[16..47]
-        let iv = keys.[48..63]
+        let keyAES = keys.[0 .. 15]
+        let keyMAC = keys.[16 .. 47]
+        let iv = keys.[48 .. 63]
 
         // Integrity validation.
         let mac' = Crypto.ComputeHMAC keyMAC inputStream