Typo.
[crypto_lab2.git] / labo2-fsharp / CryptoFileTests / Program.fs
1 module CryptoFileTests.Main
2
3 open System
4 open System.IO
5 open CryptoFile
6
7 let printUsage () =
8 printfn "%s [ tests | encrypt <in file> <out file> | decrypt <in file> <out dir> ]\n\
9 \ttests: Run some tests to valid the 'CryptoFile' assembly\n\
10 \tencrypt: Encrypt a file <in> to <out>\n\
11 \tdecrypt: Decrypt a file <in> to <out>" System.AppDomain.CurrentDomain.FriendlyName
12
13 // Module to store and retreive the keys to and from a file.
14 // The private keys are stored in plain file! Do not let anyone access these files!
15 module internal Keys =
16 // Try to read the public key and private key from files. If one of a file doesn't exist
17 // a new key pair is created, stored then returned.
18 let getKey (filenamePub: string) (filenamePriv: string) : Key * Key =
19 try
20 use srPub = new StreamReader (filenamePub)
21 use srPriv = new StreamReader (filenamePriv)
22 srPub.ReadToEnd (), srPriv.ReadToEnd ()
23 with
24 | _ ->
25 use swPub = new StreamWriter (filenamePub)
26 use swPriv = new StreamWriter (filenamePriv)
27 let keyPub, keyPriv = API.generatKeysPair ()
28 swPub.Write keyPub
29 swPriv.Write keyPriv
30 keyPub, keyPriv
31
32 let getSig : Key * Key = getKey "keys-sign.pub" "keys-sign.priv"
33 let getCrypt : Key * Key = getKey "keys-crypt.pub" "keys-crypt.priv"
34
35 [<EntryPoint>]
36 let main args =
37 let keySigPub, keySigPriv = Keys.getSig
38 let keyCryptPub, keyCryptPriv = Keys.getCrypt
39
40 match args with
41 | [| "tests" |] -> Tests.doSomeTests ()
42 | [| "encrypt"; input; output |] -> API.encryptFile input output keySigPriv keyCryptPub
43 | [| "decrypt"; input; outputDir |] -> API.decryptFile input outputDir keySigPub keyCryptPriv
44 | _ -> printUsage ()
45 0
46