AzureKeyVault/R/stored_key.R

120 строки
3.7 KiB
R
Исходник Обычный вид История

2019-04-11 08:00:53 +03:00
stored_key <- R6::R6Class("stored_key",
public=list(
token=NULL,
url=NULL,
name=NULL,
version=NULL,
key=NULL,
attributes=NULL,
managed=NULL,
tags=NULL,
initialize=function(token, url, name, version, properties)
{
self$token <- token
self$url <- url
self$name <- name
self$version <- version
lapply(names(properties), function(n)
{
if(exists(n, self))
self[[n]] <- properties[[n]]
2019-04-11 10:04:38 +03:00
else warning("Unexpected property: ", n)
2019-04-11 08:00:53 +03:00
})
if(is.null(self$version))
self$version <- basename(self$key$kid)
},
2019-04-11 08:18:27 +03:00
encrypt=function(plaintext, algorithm=c("RSA-OAEP", "RSA-OAEP-256", "RSA1_5"))
2019-04-11 08:00:53 +03:00
{
if(!is.raw(plaintext) && !is.character(plaintext) && length(plaintext) != 1)
stop("Can only encrypt raw or character plaintext")
body <- list(
alg=match.arg(algorithm),
value=plaintext
)
self$do_operation("encrypt", body=body, encode="json", http_verb="POST")$value
},
2019-04-11 08:18:27 +03:00
decrypt=function(ciphertext, algorithm=c("RSA-OAEP", "RSA-OAEP-256", "RSA1_5"))
2019-04-11 08:00:53 +03:00
{
if(!is.raw(ciphertext) && !is.character(ciphertext) && length(ciphertext) != 1)
stop("Can only decrypt raw or character ciphertext")
body <- list(
alg=match.arg(algorithm),
value=ciphertext
)
self$do_operation("decrypt", body=body, encode="json", http_verb="POST")$value
},
2019-04-11 08:18:27 +03:00
sign=function(digest,
2019-04-11 08:00:53 +03:00
algorithm=c("ES256", "ES256K", "ES384", "ES512", "PS256",
2019-04-11 08:18:27 +03:00
"PS384", "PS512", "RS256", "RS384", "RS512"))
2019-04-11 08:00:53 +03:00
{
if(!is.raw(digest) && !is.character(digest) && length(digest) != 1)
stop("Can only sign raw or character digest")
body <- list(
alg=match.arg(algorithm),
2019-04-11 08:33:11 +03:00
value=jose::base64url_encode(digest)
2019-04-11 08:00:53 +03:00
)
self$do_operation("sign", body=body, encode="json", http_verb="POST")$value
},
2019-04-11 08:18:27 +03:00
verify=function(signature, digest,
2019-04-11 08:00:53 +03:00
algorithm=c("ES256", "ES256K", "ES384", "ES512", "PS256",
2019-04-11 08:18:27 +03:00
"PS384", "PS512", "RS256", "RS384", "RS512"))
2019-04-11 08:00:53 +03:00
{
if(!is.raw(signature) && !is.character(signature) && length(signature) != 1)
stop("Can only verify raw or character signature")
if(!is.raw(digest) && !is.character(digest) && length(digest) != 1)
stop("Can only verify raw or character digest")
body <- list(
alg=match.arg(algorithm),
2019-04-11 08:33:11 +03:00
digest=jose::base64url_encode(digest),
2019-04-11 08:00:53 +03:00
value=signature
)
self$do_operation("verify", body=body, encode="json", http_verb="POST")$value
},
2019-04-11 08:18:27 +03:00
wrap=function(value, algorithm=c("RSA-OAEP", "RSA-OAEP-256", "RSA1_5"))
2019-04-11 08:00:53 +03:00
{
if(!is.raw(value) && !is.character(value) && length(value) != 1)
stop("Can only wrap raw or character input")
body <- list(
alg=match.arg(algorithm),
value=value
)
self$do_operation("wrapkey", body=body, encode="json", http_verb="POST")$value
},
2019-04-11 08:18:27 +03:00
unwrap=function(value, algorithm=c("RSA-OAEP", "RSA-OAEP-256", "RSA1_5"))
2019-04-11 08:00:53 +03:00
{
if(!is.raw(value) && !is.character(value) && length(value) != 1)
stop("Can only wrap raw or character input")
body <- list(
alg=match.arg(algorithm),
value=value
)
self$do_operation("unwrapkey", body=body, encode="json", http_verb="POST")$value
},
2019-04-11 09:37:28 +03:00
do_operation=function(op="", ..., options=list())
2019-04-11 08:00:53 +03:00
{
url <- self$url
url$path <- construct_path("keys", self$name, self$version, op)
2019-04-11 09:37:28 +03:00
url$query <- options
2019-04-11 08:00:53 +03:00
call_vault_url(self$token, url, ...)
}
))