X-Git-Url: http://git.euphorik.ch/?p=crypto_lab2.git;a=blobdiff_plain;f=labo2-fsharp%2FCryptoFile%2FAPI.fs;h=2f80c070fec56e6683a44bb79b55ca085ad668b1;hp=424959da80403c3b1bb88ad0fcfcfa29f687f154;hb=a29300558755d533aff12a6fb668c7e985ca4617;hpb=2fcf3ed38874e9aa6d2ccd6b9917bd3113d76aee diff --git a/labo2-fsharp/CryptoFile/API.fs b/labo2-fsharp/CryptoFile/API.fs index 424959d..2f80c07 100644 --- a/labo2-fsharp/CryptoFile/API.fs +++ b/labo2-fsharp/CryptoFile/API.fs @@ -9,7 +9,7 @@ type internal Metadata (d: (string * string) list) = new (stream : Stream) = let reader = new BinaryReader (stream) let length = reader.ReadByte () |> int - new Metadata ([for i in 1..length -> reader.ReadString (), reader.ReadString ()]) + Metadata ([for i in 1..length -> reader.ReadString (), reader.ReadString ()]) // Write metadata to a stream. member this.WriteTo (stream : Stream) = @@ -24,20 +24,20 @@ type internal Metadata (d: (string * string) list) = | _ -> None) d module API = - module internal Metadata = - let filenameKey = "filename" - let modificationTimeKey = "file-modification-time" + module internal MetadataKeys = + let filename = "filename" + let modificationTime = "file-modification-time" let internal (@@) a1 a2 = Array.append a1 a2 let generatKeysPair : Key * Key = Crypto.generateRSAKeysPair let encryptFile (inputFilePath : string) (outputFilePath : string) (signaturePrivKey: Key) (cryptPubKey : Key) = - let keyAES, keyMAC, iv = Crypto.rand 32, Crypto.rand 32, Crypto.rand 16 - let fileInfo = new FileInfo (inputFilePath) + let keyAES, keyMAC, iv = Crypto.rand 16, Crypto.rand 32, Crypto.rand 16 + let fileInfo = FileInfo (inputFilePath) use inputStream = fileInfo.OpenRead () - use outputStream = new FileStream (outputFilePath, FileMode.Create, FileAccess.Write, FileShare.Read, 4096 * 100) - let writer = new BinaryWriter (outputStream) + use outputStream = new FileStream (outputFilePath, FileMode.Create, FileAccess.Write) + use writer = new BinaryWriter (outputStream) outputStream.Position <- 32L + 256L // Skips mac and signature. They will be written later. @@ -46,11 +46,11 @@ module API = // Plaintext -> cryptoStream -> hmacStream -> cyphertext. let hmacStream, hmac = Crypto.HMACStream keyMAC outputStream use cryptoStream = Crypto.encryptAES keyAES iv hmacStream - let cryptoWriter = new BinaryWriter (cryptoStream) + use cryptoWriter = new BinaryWriter (cryptoStream) // Write the file metadata. - let metaData = new Metadata ([Metadata.filenameKey, fileInfo.Name - Metadata.modificationTimeKey, fileInfo.LastWriteTimeUtc.Ticks.ToString ()]) + let metaData = Metadata ([MetadataKeys.filename, fileInfo.Name + MetadataKeys.modificationTime, fileInfo.LastWriteTimeUtc.Ticks.ToString ()]) metaData.WriteTo cryptoStream // Write the content of the file. @@ -77,9 +77,9 @@ module API = try reader.ReadBytes 256 |> Crypto.decryptRSA decryptPrivKey with | :? Security.Cryptography.CryptographicException -> raise UnableToDecryptAESKeys - let keyAES = keys.[0..31] - let keyMAC = keys.[32..63] - let iv = keys.[64..79] + let keyAES = keys.[0..15] + let keyMAC = keys.[16..47] + let iv = keys.[48..63] // Integrity validation. let mac' = Crypto.ComputeHMAC keyMAC inputStream @@ -93,12 +93,12 @@ module API = // Decrypt metadata. inputStream.Position <- 32L + 256L + 256L use cryptoStream = Crypto.decryptAES keyAES iv inputStream - let metadata = new Metadata (cryptoStream) + let metadata = Metadata cryptoStream // Create the file and write its content and metadata. - let filePath = Path.Combine (targetDirPath, metadata.get Metadata.filenameKey) - let modificationTime = new DateTime (metadata.get Metadata.modificationTimeKey |> int64) - let fileInfo = new FileInfo (filePath) + let filePath = Path.Combine (targetDirPath, metadata.get MetadataKeys.filename) + let modificationTime = DateTime (metadata.get MetadataKeys.modificationTime |> int64) + let fileInfo = FileInfo filePath using (fileInfo.Create ()) <| fun outputStream -> cryptoStream.CopyTo outputStream fileInfo.LastWriteTimeUtc <- modificationTime \ No newline at end of file