Skip to content

Commit

Permalink
use sha3-256 from secretbase for archive()
Browse files Browse the repository at this point in the history
  • Loading branch information
shikokuchuo committed Jan 23, 2024
1 parent dc4eb38 commit 0f28859
Show file tree
Hide file tree
Showing 10 changed files with 44 additions and 40 deletions.
3 changes: 2 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Package: ichimoku
Type: Package
Title: Visualization and Tools for Ichimoku Kinko Hyo Strategies
Version: 1.4.13
Version: 1.4.13.9000
Description: An implementation of 'Ichimoku Kinko Hyo', also commonly known as
'cloud charts'. Static and interactive visualizations with tools for
creating, backtesting and development of quantitative 'ichimoku' strategies.
Expand Down Expand Up @@ -33,6 +33,7 @@ Imports:
mirai (>= 0.12.0),
nanonext (>= 0.12.0),
RcppSimdJson (>= 0.1.9),
secretbase,
shiny (>= 1.4.0),
xts,
zoo
Expand Down
2 changes: 1 addition & 1 deletion NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,8 @@ importFrom(ggplot2,theme)
importFrom(ggplot2,theme_grey)
importFrom(mirai,mirai)
importFrom(nanonext,ncurl)
importFrom(nanonext,sha256)
importFrom(nanonext,strcat)
importFrom(secretbase,sha3)
importFrom(shiny,HTML)
importFrom(shiny,checkboxInput)
importFrom(shiny,column)
Expand Down
11 changes: 11 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,14 @@
# ichimoku 1.4.13.9000 (development)

#### New features:

* `archive()` updated to use the fast and memory-efficient implementation of SHA3-256 from {secretbase} for data verification.
+ Note: archive files created using earlier package versions can no longer be verified using `archive()` but may nevertheless be loaded using `readRDS()`.

#### Updates:

* Requires secretbase.

# ichimoku 1.4.13

#### Updates:
Expand Down
35 changes: 15 additions & 20 deletions R/archive.R
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright (C) 2021-2023 Hibiki AI Limited <[email protected]>
# Copyright (C) 2021-2024 Hibiki AI Limited <[email protected]>
#
# This file is part of ichimoku.
#
Expand Down Expand Up @@ -45,12 +45,12 @@
#'
#' @section Data Verification:
#'
#' A SHA256 hash of the original object is written to the archive. This
#' A SHA3-256 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 SHA256 hash written to file is
#' displayed.
#' For write operations: confirmation of the SHA3-256 hash written to file
#' is displayed.
#'
#' For read operations: a 'data verified' message is issued if the SHA256
#' hash found within the data file has been authenticated.
Expand Down Expand Up @@ -128,7 +128,7 @@ archive <- function(..., object, file) {

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

x_archive_sha256 <- sha256(object)
save(object, x_archive_sha256, file = file, compress = TRUE)
message(sprintf("Archive written to '%s'\nSHA256: %s", file, x_archive_sha256))
x_archive_secure_hash <- sha3(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))
invisible(file)

}

#' Read Objects from Archive
#'
#' Internal function used to read objects from native RData files with stored
#' sha256 hash values.
#' SHA3-256 hash values.
#'
#' @param file the name of the file or a connection where the object is saved to
#' or read from.
Expand All @@ -176,21 +176,16 @@ readArchive <- function(file) {
is.character(file) ||
stop("in archive(file): 'file' must be supplied as a string.\nDid you omit the surrounding quotes \"\"?", call. = FALSE)

object <- x_archive_sha256 <- NULL
object <- x_archive_secure_hash <- NULL
x_archive_names <- load(file)
x_archive_names[2L] == "x_archive_sha256" && x_archive_names[1L] == "object" ||
x_archive_names[2L] == "x_archive_secure_hash" && x_archive_names[1L] == "object" ||
stop("archive file was not created by archive()", call. = FALSE)

message("Archive read from '", file, "'")
if (is.na(x_archive_sha256[1L])) {
# for legacy compatibility with previous implementations of archive
message("Data unverified: SHA256 hash not present")
} else {
sha256 <- sha256(object)
if (identical(sha256, x_archive_sha256))
message("Data verified by SHA256: ", sha256) else
warning(sprintf("SHA256 of restored object: %s\ndoes not match the original: %s", sha256, x_archive_sha256), call. = FALSE)
}
sha256 <- sha3(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)

object

Expand Down
5 changes: 3 additions & 2 deletions R/ichimoku-package.R
Original file line number Diff line number Diff line change
Expand Up @@ -83,13 +83,14 @@
#' scale_x_continuous scale_y_continuous Stat StatIdentity theme theme_grey
#' %+replace%
#' @importFrom mirai mirai
#' @importFrom nanonext ncurl sha256 strcat
#' @importFrom nanonext ncurl strcat
#' @importFrom secretbase sha3
#' @importFrom RcppSimdJson is_valid_json
#' @importFrom shiny checkboxInput column downloadButton downloadHandler HTML
#' fillPage fluidPage fluidRow hoverOpts invalidateLater isolate
#' numericInput observeEvent plotOutput reactive reactiveVal renderPlot
#' renderUI req runApp selectInput shinyApp sliderInput stopApp tags
#' textInput uiOutput wellPanel
#' @importFrom RcppSimdJson is_valid_json
#' @importFrom stats na.omit sd
#' @importFrom utils packageVersion str
#' @importFrom xts endpoints
Expand Down
2 changes: 1 addition & 1 deletion README.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ knitr::opts_chunk$set(
[![CRAN Status](https://www.r-pkg.org/badges/version/ichimoku?color=00008b)](https://CRAN.R-project.org/package=ichimoku)
[![ichimoku status badge](https://shikokuchuo.r-universe.dev/badges/ichimoku?color=a4d1eb)](https://shikokuchuo.r-universe.dev/ichimoku)
[![R-CMD-check](https://github.com/shikokuchuo/ichimoku/actions/workflows/R-CMD-check.yaml/badge.svg)](https://github.com/shikokuchuo/ichimoku/actions/workflows/R-CMD-check.yaml)
[![codecov](https://codecov.io/gh/shikokuchuo/ichimoku/branch/main/graph/badge.svg)](https://app.codecov.io/gh/shikokuchuo/ichimoku)
[![codecov](https://codecov.io/gh/shikokuchuo/ichimoku/graph/badge.svg)](https://app.codecov.io/gh/shikokuchuo/ichimoku)
[![DOI](https://zenodo.org/badge/367928545.svg)](https://zenodo.org/badge/latestdoi/367928545)
<!-- badges: end -->

Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ Status](https://www.r-pkg.org/badges/version/ichimoku?color=00008b)](https://CRA
[![ichimoku status
badge](https://shikokuchuo.r-universe.dev/badges/ichimoku?color=a4d1eb)](https://shikokuchuo.r-universe.dev/ichimoku)
[![R-CMD-check](https://github.com/shikokuchuo/ichimoku/actions/workflows/R-CMD-check.yaml/badge.svg)](https://github.com/shikokuchuo/ichimoku/actions/workflows/R-CMD-check.yaml)
[![codecov](https://codecov.io/gh/shikokuchuo/ichimoku/branch/main/graph/badge.svg)](https://app.codecov.io/gh/shikokuchuo/ichimoku)
[![codecov](https://codecov.io/gh/shikokuchuo/ichimoku/graph/badge.svg)](https://app.codecov.io/gh/shikokuchuo/ichimoku)
[![DOI](https://zenodo.org/badge/367928545.svg)](https://zenodo.org/badge/latestdoi/367928545)
<!-- badges: end -->

Expand Down
6 changes: 3 additions & 3 deletions man/archive.Rd

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

9 changes: 2 additions & 7 deletions tests/testthat/test-archive.R
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,9 @@ test_that("archive functions ok", {
save(sample_ohlc_data, file = file)
expect_error(archive(file), "was not created")
unlink(file)
x_archive_secure_hash <- "010101010101"
file <- tempfile()
x_archive_sha256 <- NA
save(object, x_archive_sha256, file = file)
expect_message(restored <- archive(file), "unverified")
unlink(file)
x_archive_sha256 <- "010101010101"
file <- tempfile()
save(object, x_archive_sha256, file = file)
save(object, x_archive_secure_hash, file = file)
expect_warning(expect_message(restored <- archive(file), "Archive read"), "does not match the original")
unlink(file)
})
9 changes: 5 additions & 4 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 MbedTLS cryptographic-grade SHA256 hashing algorithm from the 'nanonext' package.
Data integrity verification is performed by the SHA3-256 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 SHA256 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 SHA3-256 hash value is printed to the console as confirmation.

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.
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.

## Supplementary Information

Expand Down Expand Up @@ -427,7 +427,8 @@ ichimoku currently has the following external package dependencies:
- shiny - for interactive and live visualizations
- xts, zoo - for enhanced time series methods
- mirai - for running applications in background processes
- nanonext - for interfacing with external APIs and verifying the data integrity of archived objects
- nanonext - for interfacing with external APIs
- secretbase - for verifying the data integrity of archived objects
- RcppSimdJson - for parsing data received from external APIs

Optional (enables additional features):
Expand Down

0 comments on commit 0f28859

Please sign in to comment.