X-Git-Url: http://git.euphorik.ch/?p=crypto_lab2.git;a=blobdiff_plain;f=labo2-fsharp%2FCryptoFile%2FAPI.fs;fp=labo2-fsharp%2FCryptoFile%2FAPI.fs;h=925f933b7b6f48ee165e69358927abd52dce7842;hp=f542f49f0e41b006f702029b0537c4018d327109;hb=57d7ae8d75a854296718cb1056efbeb476309908;hpb=5682c6a31f9a56142007447eadc6ad5045da905e diff --git a/labo2-fsharp/CryptoFile/API.fs b/labo2-fsharp/CryptoFile/API.fs index f542f49..925f933 100644 --- a/labo2-fsharp/CryptoFile/API.fs +++ b/labo2-fsharp/CryptoFile/API.fs @@ -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: - // - // Where the sizes of the three first parts are given by 'hmacSize', 'signatureSize' and 'keysSize'. + /// Format of the container: + /// + /// 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