51912d35306be0821126540543cb8527083be7c0
6 open System.Security.Cryptography
10 let kpub, kpriv
= generateRSAKeysPair
()
11 let plaintext = "Are you from the past?"
12 let cipherdata = encryptRSA
kpub (Encoding.UTF8.GetBytes plaintext)
13 let decryptedData = decryptRSA kpriv
cipherdata
14 let decryptedText = Encoding.UTF8.GetString decryptedData
16 assert (plaintext = decryptedText)
19 let testRSASignature () =
20 let kpub, kpriv
= generateRSAKeysPair
()
21 let plaintext = "Lightspeed is too slow. We'll have to go right to ludicrous speed!"
22 use sha256 = new SHA256Managed ()
23 let signature = signRSA kpriv
(sha256.ComputeHash (Encoding.UTF8.GetBytes plaintext))
24 assert verifySignRSA kpub (sha256.ComputeHash (Encoding.UTF8.GetBytes plaintext)) signature
25 assert not (verifySignRSA kpub (sha256.ComputeHash (Encoding.UTF8.GetBytes "Hello!")) signature)
26 printfn
"testRSASignature OK"
29 let plaintext = "There is no place like 127.0.0.1"
30 use memory = new MemoryStream ()
31 let key = Crypto.rand
16
32 let iv = Crypto.rand
16
34 let input = encryptAES
key iv memory
35 use sw = new StreamWriter (input)
38 input.FlushFinalBlock ()
41 let output = decryptAES
key iv memory
42 use sr = new StreamReader (output)
44 assert (sr.ReadToEnd () = plaintext)
47 let runAllUnitTests () =