Skip to content

Commit

Permalink
update to use SHA256 again; avoids strcat
Browse files Browse the repository at this point in the history
  • Loading branch information
shikokuchuo committed Jun 4, 2024
1 parent 28a7380 commit 51d4c02
Show file tree
Hide file tree
Showing 10 changed files with 32 additions and 31 deletions.
6 changes: 3 additions & 3 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,10 @@ Depends:
R (>= 3.5)
Imports:
ggplot2 (>= 3.4.0),
mirai (>= 0.12.0),
nanonext (>= 0.12.0),
mirai (>= 1.0.0),
nanonext (>= 1.0.0),
RcppSimdJson (>= 0.1.9),
secretbase,
secretbase (>= 0.3.0),
shiny (>= 1.4.0),
xts,
zoo
Expand Down
3 changes: 1 addition & 2 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,7 @@ importFrom(ggplot2,theme)
importFrom(ggplot2,theme_grey)
importFrom(mirai,mirai)
importFrom(nanonext,ncurl)
importFrom(nanonext,strcat)
importFrom(secretbase,sha3)
importFrom(secretbase,sha256)
importFrom(shiny,HTML)
importFrom(shiny,checkboxInput)
importFrom(shiny,column)
Expand Down
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# ichimoku 1.5.1.9000 (development)

* `archive()` moves to SHA256 again (using the updated implementation in {secretbase}).

# ichimoku 1.5.1

* Fixes `oanda_studio()` so that the live chart no longer greys out when updating (with recent Shiny versions).
Expand Down
18 changes: 9 additions & 9 deletions R/archive.R
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,11 @@
#'
#' @section Data Verification:
#'
#' A SHA3-256 hash of the original object is written to the archive. This
#' A SHA256 hash of the original object is written to the archive. This
#' allows the data integrity of the restored object to be verified when the
#' archive is read back.
#'
#' For write operations: confirmation of the SHA3-256 hash written to file
#' For write operations: confirmation of the SHA256 hash written to file
#' is displayed.
#'
#' For read operations: a 'data verified' message is issued if the SHA256
Expand Down Expand Up @@ -130,7 +130,7 @@ archive <- function(..., object, file) {

#' Write Objects to Archive
#'
#' Internal function used to write objects, along with their SHA3-256 hash value,
#' Internal function used to write objects, along with their SHA-256 hash value,
#' to archive files in the native RData format.
#'
#' @param object an object.
Expand All @@ -154,17 +154,17 @@ writeArchive <- function(object, file) {
}
}

x_archive_secure_hash <- sha3(object)
x_archive_secure_hash <- sha256(object)
save(object, x_archive_secure_hash, file = file, compress = TRUE)
message(sprintf("Archive written to '%s'\nSHA3-256: %s", file, x_archive_secure_hash))
message(sprintf("Archive written to '%s'\nSHA256: %s", file, x_archive_secure_hash))
invisible(file)

}

#' Read Objects from Archive
#'
#' Internal function used to read objects from native RData files with stored
#' SHA3-256 hash values.
#' SHA256 hash values.
#'
#' @param file the name of the file or a connection where the object is saved to
#' or read from.
Expand All @@ -184,10 +184,10 @@ readArchive <- function(file) {
stop("archive file was not created by archive()", call. = FALSE)

message("Archive read from '", file, "'")
sha256 <- sha3(object)
sha256 <- sha256(object)
if (identical(sha256, x_archive_secure_hash))
message("Data verified by SHA3-256: ", sha256) else
warning(sprintf("SHA3-256 of restored object: %s\ndoes not match the original: %s", sha256, x_archive_secure_hash), call. = FALSE)
message("Data verified by SHA256: ", sha256) else
warning(sprintf("SHA256 of restored object: %s\ndoes not match the original: %s", sha256, x_archive_secure_hash), call. = FALSE)

object

Expand Down
2 changes: 1 addition & 1 deletion R/data.R
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@

.__global__ <- ".data"

.user_agent <- strcat("r-ichimoku/", as.character(packageVersion("ichimoku")))
.user_agent <- sprintf("r-ichimoku/%s", as.character(packageVersion("ichimoku")))

.mlgrid_pairs <- {
cols <- c("chikou", "close", "high", "low", "tenkan", "kijun", "senkouA", "senkouB", "cloudT", "cloudB")
Expand Down
4 changes: 2 additions & 2 deletions R/ichimoku-package.R
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,8 @@
#' scale_x_continuous scale_y_continuous Stat StatIdentity theme theme_grey
#' %+replace%
#' @importFrom mirai mirai
#' @importFrom nanonext ncurl strcat
#' @importFrom secretbase sha3
#' @importFrom nanonext ncurl
#' @importFrom secretbase sha256
#' @importFrom RcppSimdJson is_valid_json
#' @importFrom shiny checkboxInput column downloadButton downloadHandler HTML
#' fillPage fluidPage fluidRow hoverOpts invalidateLater isolate
Expand Down
14 changes: 7 additions & 7 deletions R/oanda.R
Original file line number Diff line number Diff line change
Expand Up @@ -189,14 +189,14 @@ getPrices <- function(instrument, granularity, count = NULL, from = NULL,
url <- sprintf(
"https://api-fx%s.oanda.com/v3/instruments/%s/candles?granularity=%s&price=%s%s%s%s",
switch(server, practice = "practice", live = "trade"), instrument, granularity, price,
if (length(count)) strcat("&count=", as.character(count)) else "",
if (length(from)) strcat("&from=", as.character(from)) else "",
if (length(to)) strcat("&to=", as.character(to)) else ""
if (length(count)) sprintf("&count=%s", as.character(count)) else "",
if (length(from)) sprintf("&from=%s", as.character(from)) else "",
if (length(to)) sprintf("&to=%s", as.character(to)) else ""
)
resp <- ncurl(url,
convert = FALSE,
follow = TRUE,
headers = c(Authorization = strcat("Bearer ", apikey),
headers = c(Authorization = sprintf("Bearer %s", apikey),
`Accept-Datetime-Format` = "UNIX",
`User-Agent` = .user_agent),
response = "date")
Expand Down Expand Up @@ -326,7 +326,7 @@ oanda_stream <- function(instrument, display = 8L, limit, server, apikey) {
url <- paste0("https://stream-fx", switch(server, practice = "practice", live = "trade"),
".oanda.com/v3/accounts/", do_$getAccount(server = server, apikey = apikey),
"/pricing/stream?instruments=", instrument)
headers <- c(Authorization = strcat("Bearer ", apikey),
headers <- c(Authorization = sprintf("Bearer %s", apikey),
`Accept-Datetime-Format` = "UNIX",
`User-Agent` = .user_agent)

Expand Down Expand Up @@ -1013,7 +1013,7 @@ oanda_positions <- function(instrument, time, server, apikey) {
switch(server, practice = "practice", live = "trade"), instrument,
if (missing(time)) "" else sprintf("?time=%.f", unclass(as.POSIXct(time))))
resp <- ncurl(url, convert = FALSE, follow = TRUE,
headers = c(Authorization = strcat("Bearer ", apikey),
headers = c(Authorization = sprintf("Bearer %s", apikey),
`Accept-Datetime-Format` = "UNIX", `User-Agent` = .user_agent))
resp[["status"]] == 200L ||
stop("status code ", resp[["status"]], " - ",
Expand Down Expand Up @@ -1105,7 +1105,7 @@ oanda_orders <- function(instrument, time, server, apikey) {
switch(server, practice = "practice", live = "trade"), instrument,
if (missing(time)) "" else sprintf("?time=%.f", unclass(as.POSIXct(time))))
resp <- ncurl(url, convert = FALSE, follow = TRUE,
headers = c(Authorization = strcat("Bearer ", apikey),
headers = c(Authorization = sprintf("Bearer %s", apikey),
`Accept-Datetime-Format` = "UNIX", `User-Agent` = .user_agent))
resp[["status"]] == 200L ||
stop("status code ", resp[["status"]], " - ",
Expand Down
4 changes: 2 additions & 2 deletions R/switch.R
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ do_ <- function() {
practice = "https://api-fxpractice.oanda.com/v3/accounts",
live = "https://api-fxtrade.oanda.com/v3/accounts")
resp <- ncurl(url, convert = FALSE, follow = TRUE,
headers = c("Authorization" = strcat("Bearer ", apikey),
headers = c("Authorization" = sprintf("Bearer %s", apikey),
"User-Agent" = .user_agent))
resp[["status"]] == 200L ||
stop("status code ", resp[["status"]], " - ", deserialize_json(resp[["data"]]), call. = FALSE)
Expand All @@ -140,7 +140,7 @@ do_ <- function() {
do_$getAccount(server = server, apikey = apikey))
for (i in seq_len(2L)) {
resp <- ncurl(url, convert = FALSE, follow = TRUE,
headers = c("Authorization" = strcat("Bearer ", apikey),
headers = c("Authorization" = sprintf("Bearer %s", apikey),
"User-Agent" = .user_agent))
resp[["status"]] == 200L && break
}
Expand Down
4 changes: 2 additions & 2 deletions man/archive.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions vignettes/reference.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -349,11 +349,11 @@ object <- archive()

#### Data Integrity Verification

Data integrity verification is performed by the SHA3-256 cryptographic hash algorithm from the 'secretbase' package.
Data integrity verification is performed by the SHA256 cryptographic hash algorithm from the 'secretbase' package.

When an archive is written, the serialised object is hashed and the hash is also stored in the archive. The SHA3-256 hash value is printed to the console as confirmation.
When an archive is written, the serialised object is hashed and the hash is also stored in the archive. The SHA256 hash value is printed to the console as confirmation.

When an archive is read back, the SHA3-256 hash of the restored object is checked against the hash of the original stored in the archive. If identical, a 'data verified' message is printed to the console along with the authenticated SHA3-256 hash.
When an archive is read back, the SHA256 hash of the restored object is checked against the hash of the original stored in the archive. If identical, a 'data verified' message is printed to the console along with the authenticated SHA256 hash.

## Supplementary Information

Expand Down

0 comments on commit 51d4c02

Please sign in to comment.