File: test_encrypt.R

package info (click to toggle)
r-cran-openssl 1.4.3%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 1,288 kB
  • sloc: ansic: 3,021; sh: 97; makefile: 5
file content (31 lines) | stat: -rw-r--r-- 944 bytes parent folder | download | duplicates (4)
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
31
context("Test RSA encryption")

test_that("rsa decrypt", {
  key <- read_key("../keys/id_rsa")
  msg <- readBin("../keys/message", raw(), 1000)
  ct <- readBin("../keys/message.rsa.crypt", raw(), 1000)
  expect_equal(msg ,rsa_decrypt(ct, key))
})

test_that("encrypt with various rsa key sizes", {
  for(size in c(512, 1024, 2048, 4096)){
    key <- rsa_keygen(size)
    pubkey <- as.list(key)$pubkey
    msg <- rand_bytes(size / 10)
    ct <- rsa_encrypt(msg, pubkey)
    expect_equal(msg, rsa_decrypt(ct, key))
    bigmsg <- rand_bytes(size / 8)
    expect_error(rsa_encrypt(bigmsg, pubkey), "too large")
  }
})

test_that("envelopes with various rsa key sizes", {
  for(size in c(512, 1024, 2048, 4096)){
    key <- rsa_keygen(size)
    pubkey <- as.list(key)$pubkey
    msg <- serialize(iris, NULL)
    out <- encrypt_envelope(msg, pubkey)
    orig <- decrypt_envelope(out$data, out$iv, out$session, key)
    expect_equal(msg, orig)
  }
})