projects
/
crypto_lab2.git
/ blobdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
|
commitdiff
|
tree
raw
|
inline
| side by side
Typo.
[crypto_lab2.git]
/
labo2-fsharp
/
CryptoFile
/
UnitTests.fs
diff --git
a/labo2-fsharp/CryptoFile/UnitTests.fs
b/labo2-fsharp/CryptoFile/UnitTests.fs
index
fb46eb7
..
51912d3
100644
(file)
--- a/
labo2-fsharp/CryptoFile/UnitTests.fs
+++ b/
labo2-fsharp/CryptoFile/UnitTests.fs
@@
-7,25
+7,19
@@
module UnitTests =
open Crypto
let testRSA () =
open Crypto
let testRSA () =
- let kpub, kpriv = generateRSAKeysPair
-
- let plaintext = "Hello, World!"
- printfn "plaintext: %A" plaintext
-
+ let kpub, kpriv = generateRSAKeysPair ()
+ let plaintext = "Are you from the past?"
let cipherdata = encryptRSA kpub (Encoding.UTF8.GetBytes plaintext)
let cipherdata = encryptRSA kpub (Encoding.UTF8.GetBytes plaintext)
- printfn "cipherdata: (size: %A) %A" cipherdata.Length cipherdata
-
let decryptedData = decryptRSA kpriv cipherdata
let decryptedText = Encoding.UTF8.GetString decryptedData
let decryptedData = decryptRSA kpriv cipherdata
let decryptedText = Encoding.UTF8.GetString decryptedData
- printfn "decryptedtext: %A" decryptedText
assert (plaintext = decryptedText)
printfn "testRSA OK"
let testRSASignature () =
assert (plaintext = decryptedText)
printfn "testRSA OK"
let testRSASignature () =
- let kpub, kpriv = generateRSAKeysPair
- let plaintext = "
Hello, Worl
d!"
-
let
sha256 = new SHA256Managed ()
+ let kpub, kpriv = generateRSAKeysPair
()
+ let plaintext = "
Lightspeed is too slow. We'll have to go right to ludicrous spee
d!"
+
use
sha256 = new SHA256Managed ()
let signature = signRSA kpriv (sha256.ComputeHash (Encoding.UTF8.GetBytes plaintext))
assert verifySignRSA kpub (sha256.ComputeHash (Encoding.UTF8.GetBytes plaintext)) signature
assert not (verifySignRSA kpub (sha256.ComputeHash (Encoding.UTF8.GetBytes "Hello!")) signature)
let signature = signRSA kpriv (sha256.ComputeHash (Encoding.UTF8.GetBytes plaintext))
assert verifySignRSA kpub (sha256.ComputeHash (Encoding.UTF8.GetBytes plaintext)) signature
assert not (verifySignRSA kpub (sha256.ComputeHash (Encoding.UTF8.GetBytes "Hello!")) signature)
@@
-33,17
+27,24
@@
module UnitTests =
let testAES () =
let plaintext = "There is no place like 127.0.0.1"
let testAES () =
let plaintext = "There is no place like 127.0.0.1"
- let output = new MemoryStream ()
- let sr = new StreamReader (output)
- let key = Crypto.rand 32
+ use memory = new MemoryStream ()
+ let key = Crypto.rand 16
let iv = Crypto.rand 16
let iv = Crypto.rand 16
- let input = encryptAES key iv output
- let sw = new StreamWriter (input)
-
+ let input = encryptAES key iv memory
+ use sw = new StreamWriter (input)
sw.Write plaintext
sw.Write plaintext
+ sw.Flush ()
+ input.FlushFinalBlock ()
+
+ memory.Position <- 0L
+ let output = decryptAES key iv memory
+ use sr = new StreamReader (output)
+
assert (sr.ReadToEnd () = plaintext)
assert (sr.ReadToEnd () = plaintext)
+ printfn "testAES OK"
let runAllUnitTests () =
testRSA ()
let runAllUnitTests () =
testRSA ()
- testRSASignature ()
\ No newline at end of file
+ testRSASignature ()
+ testAES ()
\ No newline at end of file