package props import org scalacheck Gen Properties import org scalache

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
package props
import org.scalacheck.{Gen, Properties}
import org.scalacheck.Prop.forAll
import nxt.crypto.Crypto
import scala.util.Random
import org.apache.commons.lang.RandomStringUtils
object CryptoProperties extends Properties("String") {
val strings = RandomStringUtils.randomAlphanumeric(Random.nextInt(50)+50)
property("encryptDecrypt") = forAll(strings,strings,strings){(msg: String, passphrase1:String, passphrase2:String) =>
val pubKey1 = Crypto.getPublicKey(passphrase1)
val pubKey2 = Crypto.getPublicKey(passphrase2)
val privKey1 = Crypto.getPrivateKey(passphrase1)
val privKey2 = Crypto.getPrivateKey(passphrase2)
val encryptedBytes = Crypto.aesEncrypt(msg.getBytes("UTF-8"), privKey1, pubKey2)
val decryptedBytes = Crypto.aesDecrypt(encryptedBytes, privKey2, pubKey1)
val decrypted = new String(decryptedBytes, "UTF-8")
println(s"msg: $msg (hash ${msg.hashCode}}); decrypted: $decrypted (hash ${decrypted.hashCode}}) ")
decrypted == msg
}
property("rsEncodeDecode") = forAll{(id:Long) =>
Crypto.rsDecode(Crypto.rsEncode(id))==id
}
}