Binary files /tmp/tmp1_292A/2MFP4nMwc8/r-cran-digest-0.6.25/build/vignette.rds and /tmp/tmp1_292A/wZ0VrCYuJV/r-cran-digest-0.6.27/build/vignette.rds differ diff -Nru r-cran-digest-0.6.25/ChangeLog r-cran-digest-0.6.27/ChangeLog --- r-cran-digest-0.6.25/ChangeLog 2020-02-22 14:05:29.000000000 +0000 +++ r-cran-digest-0.6.27/ChangeLog 2020-10-20 13:03:00.000000000 +0000 @@ -1,3 +1,90 @@ +2020-10-20 Dirk Eddelbuettel + + * DESCRIPTION (Version, Date): Release 0.6.27 + +2020-10-19 Dirk Eddelbuettel + + * src/blake3_dispatch.c: Comment-out include of intrinsics to also + comment-out erroring on insufficient architectures such as Solaris + +2020-10-17 Dirk Eddelbuettel + + * DESCRIPTION (Version, Date): Release 0.6.26 + + * docs/: Added package website + * docs-src/: Added package website inputs + + * .travis.yml (dist): Change to focal + +2020-10-15 Dirk Eddelbuettel + + * README.md: Update URLs + * man/digest.Rd: Ditto + * man/hmac.Rd: Ditto + +2020-10-14 Dirk Eddelbuettel + + * DESCRIPTION (Version, Date): Roll minor version + + * inst/tinytest/test_blake3.R: Skip file-based blake3 tests on Windows + + * vignettes/sha1.Rmd: Switch to minidown and 'framework: water' + * DESCRIPTION (Suggests): Add minidown + +2020-10-07 Dirk Eddelbuettel + + * DESCRIPTION (Version, Date): Roll minor version + +2020-10-06 Winston Chang + + * R/digest.R: Speedup via explicit alternative values for + match.arg() and a direct call to serialize() + * R/vdigest.R: Idem + * R/init.R: Idem (for serialize()) + * inst/tinytest/test_digest.R: New test ensuring all variants hit + +2020-09-21 Dirk Eddelbuettel + + * .travis.yml: Updated to 'bionic', R 4.0, and BSPM + +2020-08-03 Dirk Eddelbuettel + + * DESCRIPTION (Depends): Roll Depends: on to R (>= 3.3.0) due to use + of startsWith(), with thanks to Florian Pein + +2020-05-20 Dirk Eddelbuettel + + * .travis.yml: Switch to bionic and R 4.0.0 + +2020-05-19 Dirk Schumacher + + * src/digest.c: Support file operation + * inst/tinytest/test_blake3.R: Update test + +2020-05-19 Dirk Eddelbuettel + + * DESCRIPTION (Version): Roll minor version and date + +2020-05-19 Dirk Schumacher + + * src/blake3.c: Add blake3 implementation + * src/blake3.h: Idem + * src/blake3_dispatch.c: Idem + * src/blake3_impl.h: Idem + * src/blake3_portable.c: Idem + * src/digest.c: Support blake3 call + * R/digest.R: Idem + * inst/tinytest/test_blake3.R: Add tests + * man/digest.Rd: Add documentation + +2020-03-05 Harris McGehee + + * man/sha1.Rd: Correct typo + +2020-02-26 Dirk Eddelbuettel + + * README.md: Add Debian badge + 2020-02-22 Dirk Eddelbuettel * DESCRIPTION (Version, Date): Release 0.6.25 diff -Nru r-cran-digest-0.6.25/debian/changelog r-cran-digest-0.6.27/debian/changelog --- r-cran-digest-0.6.25/debian/changelog 2020-05-30 09:53:41.000000000 +0000 +++ r-cran-digest-0.6.27/debian/changelog 2020-10-24 23:37:01.000000000 +0000 @@ -1,8 +1,18 @@ -r-cran-digest (0.6.25-1build1) groovy; urgency=medium +r-cran-digest (0.6.27-1) unstable; urgency=medium - * No-change rebuild against r-api-4.0 + * New upstream release - -- Graham Inggs Sat, 30 May 2020 09:53:41 +0000 + -- Dirk Eddelbuettel Sat, 24 Oct 2020 18:37:01 -0500 + +r-cran-digest (0.6.26-1) unstable; urgency=medium + + * New upstream release + + * debian/control: Set Build-Depends: to current R version + * debian/control: Switch to virtual debhelper-compat (= 11) + * debian/compat: Removed + + -- Dirk Eddelbuettel Sat, 17 Oct 2020 12:05:23 -0500 r-cran-digest (0.6.25-1) unstable; urgency=medium diff -Nru r-cran-digest-0.6.25/debian/compat r-cran-digest-0.6.27/debian/compat --- r-cran-digest-0.6.25/debian/compat 2018-08-23 00:38:04.000000000 +0000 +++ r-cran-digest-0.6.27/debian/compat 1970-01-01 00:00:00.000000000 +0000 @@ -1 +0,0 @@ -9 diff -Nru r-cran-digest-0.6.25/debian/control r-cran-digest-0.6.27/debian/control --- r-cran-digest-0.6.25/debian/control 2020-02-12 19:57:37.000000000 +0000 +++ r-cran-digest-0.6.27/debian/control 2020-10-17 17:05:40.000000000 +0000 @@ -2,7 +2,7 @@ Section: gnu-r Priority: optional Maintainer: Dirk Eddelbuettel -Build-Depends: debhelper (>= 10), r-base-dev (>= 3.6.2), dh-r +Build-Depends: debhelper-compat (= 11), r-base-dev (>= 4.0.3), dh-r Standards-Version: 4.5.0 Vcs-Browser: https://salsa.debian.org/edd/r-cran-digest Vcs-Git: https://salsa.debian.org/edd/r-cran-digest.git diff -Nru r-cran-digest-0.6.25/DESCRIPTION r-cran-digest-0.6.27/DESCRIPTION --- r-cran-digest-0.6.25/DESCRIPTION 2020-02-23 00:10:02.000000000 +0000 +++ r-cran-digest-0.6.27/DESCRIPTION 2020-10-24 22:10:06.000000000 +0000 @@ -4,14 +4,15 @@ Mario Frasca, Bryan Lewis, Murray Stokely, Hannes Muehleisen, Duncan Murdoch, Jim Hester, Wush Wu, Qiang Kou, Thierry Onkelinx, Michel Lang, Viliam Simko, Kurt Hornik, Radford Neal, Kendon Bell, - Matthew de Queljoe, Ion Suruceanu, and Bill Denney. -Version: 0.6.25 -Date: 2020-02-22 + Matthew de Queljoe, Ion Suruceanu, Bill Denney, Dirk Schumacher, + and Winston Chang. +Version: 0.6.27 +Date: 2020-10-20 Maintainer: Dirk Eddelbuettel Title: Create Compact Hash Digests of R Objects -Description: Implementation of a function 'digest()' for the creation - of hash digests of arbitrary R objects (using the 'md5', 'sha-1', 'sha-256', - 'crc32', 'xxhash', 'murmurhash' and 'spookyhash' algorithms) permitting easy +Description: Implementation of a function 'digest()' for the creation of hash + digests of arbitrary R objects (using the 'md5', 'sha-1', 'sha-256', 'crc32', + 'xxhash', 'murmurhash', 'spookyhash' and 'blake3' algorithms) permitting easy comparison of R language objects, as well as functions such as'hmac()' to create hash-based message authentication code. Please note that this package is not meant to be deployed for cryptographic purposes for which more @@ -19,12 +20,12 @@ used. URL: http://dirk.eddelbuettel.com/code/digest.html BugReports: https://github.com/eddelbuettel/digest/issues -Depends: R (>= 3.1.0) +Depends: R (>= 3.3.0) Imports: utils License: GPL (>= 2) -Suggests: tinytest, knitr, rmarkdown +Suggests: tinytest, knitr, rmarkdown, minidown VignetteBuilder: knitr NeedsCompilation: yes -Packaged: 2020-02-22 14:55:28.282493 UTC; edd +Packaged: 2020-10-20 13:03:32 UTC; edd Repository: CRAN -Date/Publication: 2020-02-23 00:10:02 UTC +Date/Publication: 2020-10-24 22:10:06 UTC diff -Nru r-cran-digest-0.6.25/inst/doc/sha1.html r-cran-digest-0.6.27/inst/doc/sha1.html --- r-cran-digest-0.6.25/inst/doc/sha1.html 2020-02-22 14:55:28.000000000 +0000 +++ r-cran-digest-0.6.27/inst/doc/sha1.html 2020-10-20 13:03:32.000000000 +0000 @@ -1,38 +1,31 @@ - - - + - - - - - - - - - - - -Calculating SHA1 hashes with digest() and sha1() - - - - - + - + + + + + + + + + + + - - - - - -

Calculating SHA1 hashes with digest() and sha1()

-

Thierry Onkelinx and Dirk Eddelbuettel

-

2020-02-22

- - - +
+
+

Calculating SHA1 hashes with digest() and sha1()

+

Thierry Onkelinx and Dirk Eddelbuettel

+

2020-10-20

+
+

NB: This vignette is work-in-progress and not yet complete.

-
+

Short intro on hashes

TBD

-
-
+ +

Difference between digest() and sha1()

R FAQ 7.31 illustrates potential problems with floating point arithmetic. Mathematically the equality \(x = \sqrt{x}^2\) should hold. But the precision of floating points numbers is finite. Hence some rounding is done, leading to numbers which are no longer identical.

An illustration:

-
# FAQ 7.31
-a0 <- 2
-b <- sqrt(a0)
-a1 <- b ^ 2
-identical(a0, a1)
-
## [1] FALSE
-
a0 - a1
-
## [1] -4.440892e-16
-
a <- c(a0, a1)
-# hexadecimal representation
-sprintf("%a", a)
-
## [1] "0x1p+1"               "0x1.0000000000001p+1"
+
# FAQ 7.31
+a0 <- 2
+b <- sqrt(a0)
+a1 <- b ^ 2
+identical(a0, a1)
+## [1] FALSE
+a0 - a1
+## [1] -4.440892e-16
+a <- c(a0, a1)
+# hexadecimal representation
+sprintf("%a", a)
+## [1] "0x1p+1"               "0x1.0000000000001p+1"

Although the difference is small, any difference will result in different hash when using the digest() function. However, the sha1() function tackles this problem by using the hexadecimal representation of the numbers and truncates that representation to a certain number of digits prior to calculating the hash function.

-
library(digest)
-# different hashes with digest
-sapply(a, digest, algo = "sha1")
-
## [1] "315a5aa84aa6cfa4f3fb4b652a596770be0365e8"
-## [2] "5e3999bf79c230f7430e97d7f30070a7eff5ec92"
-
# same hash with sha1 with default digits (14)
-sapply(a, sha1)
-
## [1] "8a938d8f5fb9b8ccb6893aa1068babcc517f32d4"
-## [2] "8a938d8f5fb9b8ccb6893aa1068babcc517f32d4"
-
# larger digits can lead to different hashes
-sapply(a, sha1, digits = 15)
-
## [1] "98eb1dc9fada00b945d3ef01c77049ee5a4b7b9c"
-## [2] "5a173e2445df1134908037f69ac005fbd8afee74"
-
# decreasing the number of digits gives a stronger truncation
-# the hash will change when then truncation gives a different result
-# case where truncating gives same hexadecimal value
-sapply(a, sha1, digits = 13)
-
## [1] "43b3b465c975af322c85473190a9214b79b79bf6"
-## [2] "43b3b465c975af322c85473190a9214b79b79bf6"
-
sapply(a, sha1, digits = 10)
-
## [1] "6b537a9693c750ed535ca90527152f06e358aa3a"
-## [2] "6b537a9693c750ed535ca90527152f06e358aa3a"
-
# case where truncating gives different hexadecimal value
-c(sha1(pi), sha1(pi, digits = 13), sha1(pi, digits = 10))
-
## [1] "169388cf1ce60dc4e9904a22bc934c5db33d975b"
-## [2] "20dc38536b6689d5f2d053f30efb09c44af78378"
-## [3] "3a727417bd1807af2f0148cf3de69abff32c23ec"
+
library(digest)
+# different hashes with digest
+sapply(a, digest, algo = "sha1")
+## [1] "315a5aa84aa6cfa4f3fb4b652a596770be0365e8"
+## [2] "5e3999bf79c230f7430e97d7f30070a7eff5ec92"
+# same hash with sha1 with default digits (14)
+sapply(a, sha1)
+## [1] "8a938d8f5fb9b8ccb6893aa1068babcc517f32d4"
+## [2] "8a938d8f5fb9b8ccb6893aa1068babcc517f32d4"
+# larger digits can lead to different hashes
+sapply(a, sha1, digits = 15)
+## [1] "98eb1dc9fada00b945d3ef01c77049ee5a4b7b9c"
+## [2] "5a173e2445df1134908037f69ac005fbd8afee74"
+# decreasing the number of digits gives a stronger truncation
+# the hash will change when then truncation gives a different result
+# case where truncating gives same hexadecimal value
+sapply(a, sha1, digits = 13)
+## [1] "43b3b465c975af322c85473190a9214b79b79bf6"
+## [2] "43b3b465c975af322c85473190a9214b79b79bf6"
+sapply(a, sha1, digits = 10)
+## [1] "6b537a9693c750ed535ca90527152f06e358aa3a"
+## [2] "6b537a9693c750ed535ca90527152f06e358aa3a"
+# case where truncating gives different hexadecimal value
+c(sha1(pi), sha1(pi, digits = 13), sha1(pi, digits = 10))
+## [1] "169388cf1ce60dc4e9904a22bc934c5db33d975b"
+## [2] "20dc38536b6689d5f2d053f30efb09c44af78378"
+## [3] "3a727417bd1807af2f0148cf3de69abff32c23ec"

The result of floating point arithematic on 32-bit and 64-bit can be slightly different. E.g. print(pi ^ 11, 22) returns 294204.01797389047 on 32-bit and 294204.01797389053 on 64-bit. Note that only the last 2 digits are different.

---+++ @@ -403,16 +463,16 @@
-
-
+ +

Choosing digest() or sha1()

TBD

-
-
+ +

Creating a sha1 method for other classes

-
+

How to

-
    +
    1. Identify the relevant components for the hash.
    2. Determine the class of each relevant component and check if they are handled by sha1().
        @@ -430,185 +490,186 @@
      • digits = 4 is recommended in case some numerics stem from floating point arithmetic.
    -
-
+
+

summary.lm

Let’s illustrate this using the summary of a simple linear regression. Suppose that we want a hash that takes into account the coefficients, their standard error and sigma.

-
# taken from the help file of lm.influence
-lm_SR <- lm(sr ~ pop15 + pop75 + dpi + ddpi, data = LifeCycleSavings)
-lm_sum <- summary(lm_SR)
-class(lm_sum)
-
## [1] "summary.lm"
-
# str() gives the structure of the lm object
-str(lm_sum)
-
## List of 11
-##  $ call         : language lm(formula = sr ~ pop15 + pop75 + dpi + ddpi, data = LifeCycleSavings)
-##  $ terms        :Classes 'terms', 'formula'  language sr ~ pop15 + pop75 + dpi + ddpi
-##   .. ..- attr(*, "variables")= language list(sr, pop15, pop75, dpi, ddpi)
-##   .. ..- attr(*, "factors")= int [1:5, 1:4] 0 1 0 0 0 0 0 1 0 0 ...
-##   .. .. ..- attr(*, "dimnames")=List of 2
-##   .. .. .. ..$ : chr [1:5] "sr" "pop15" "pop75" "dpi" ...
-##   .. .. .. ..$ : chr [1:4] "pop15" "pop75" "dpi" "ddpi"
-##   .. ..- attr(*, "term.labels")= chr [1:4] "pop15" "pop75" "dpi" "ddpi"
-##   .. ..- attr(*, "order")= int [1:4] 1 1 1 1
-##   .. ..- attr(*, "intercept")= int 1
-##   .. ..- attr(*, "response")= int 1
-##   .. ..- attr(*, ".Environment")=<environment: R_GlobalEnv> 
-##   .. ..- attr(*, "predvars")= language list(sr, pop15, pop75, dpi, ddpi)
-##   .. ..- attr(*, "dataClasses")= Named chr [1:5] "numeric" "numeric" "numeric" "numeric" ...
-##   .. .. ..- attr(*, "names")= chr [1:5] "sr" "pop15" "pop75" "dpi" ...
-##  $ residuals    : Named num [1:50] 0.864 0.616 2.219 -0.698 3.553 ...
-##   ..- attr(*, "names")= chr [1:50] "Australia" "Austria" "Belgium" "Bolivia" ...
-##  $ coefficients : num [1:5, 1:4] 28.566087 -0.461193 -1.691498 -0.000337 0.409695 ...
-##   ..- attr(*, "dimnames")=List of 2
-##   .. ..$ : chr [1:5] "(Intercept)" "pop15" "pop75" "dpi" ...
-##   .. ..$ : chr [1:4] "Estimate" "Std. Error" "t value" "Pr(>|t|)"
-##  $ aliased      : Named logi [1:5] FALSE FALSE FALSE FALSE FALSE
-##   ..- attr(*, "names")= chr [1:5] "(Intercept)" "pop15" "pop75" "dpi" ...
-##  $ sigma        : num 3.8
-##  $ df           : int [1:3] 5 45 5
-##  $ r.squared    : num 0.338
-##  $ adj.r.squared: num 0.28
-##  $ fstatistic   : Named num [1:3] 5.76 4 45
-##   ..- attr(*, "names")= chr [1:3] "value" "numdf" "dendf"
-##  $ cov.unscaled : num [1:5, 1:5] 3.74 -7.24e-02 -4.46e-01 -7.86e-05 -1.88e-02 ...
-##   ..- attr(*, "dimnames")=List of 2
-##   .. ..$ : chr [1:5] "(Intercept)" "pop15" "pop75" "dpi" ...
-##   .. ..$ : chr [1:5] "(Intercept)" "pop15" "pop75" "dpi" ...
-##  - attr(*, "class")= chr "summary.lm"
-
# extract the coefficients and their standard error
-coef_sum <- coef(lm_sum)[, c("Estimate", "Std. Error")]
-# extract sigma
-sigma <- lm_sum$sigma
-# check the class of each component
-class(coef_sum)
-
## [1] "matrix"
-
class(sigma)
-
## [1] "numeric"
-
# sha1() has methods for both matrix and numeric
-# because the values originate from floating point arithmetic it is better to use a low number of digits
-sha1(coef_sum, digits = 4)
-
## [1] "1ab538262c9aad03d17f33c644d6c8d0b27367e8"
-
sha1(sigma, digits = 4)
-
## [1] "cbc83d1791ef1eeadd824ea9a038891b5889056b"
-
# we want a single hash
-# combining the components in a list is a solution that works
-sha1(list(coef_sum, sigma), digits = 4)
-
## [1] "d2e6e07d97e2a97882fd3bbf6e4455140c0c6412"
-
# now turn everything into an S3 method
-#   - a function with name "sha1.classname"
-#   - must have the same arguments as sha1()
-sha1.summary.lm <- function(x, digits = 4, zapsmall = 7){
-    coef_sum <- coef(x)[, c("Estimate", "Std. Error")]
-    sigma <- x$sigma
-    combined <- list(coef_sum, sigma)
-    sha1(combined, digits = digits, zapsmall = zapsmall)
-}
-sha1(lm_sum)
-
## [1] "d2e6e07d97e2a97882fd3bbf6e4455140c0c6412"
-
# try an altered dataset
-LCS2 <- LifeCycleSavings[rownames(LifeCycleSavings) != "Zambia", ]
-lm_SR2 <- lm(sr ~ pop15 + pop75 + dpi + ddpi, data = LCS2)
-sha1(summary(lm_SR2))
-
## [1] "637d8121e49b30dbc0b0e8cd02ff5a4a8b9d89e1"
-
-
+
# taken from the help file of lm.influence
+lm_SR <- lm(sr ~ pop15 + pop75 + dpi + ddpi, data = LifeCycleSavings)
+lm_sum <- summary(lm_SR)
+class(lm_sum)
+## [1] "summary.lm"
+# str() gives the structure of the lm object
+str(lm_sum)
+## List of 11
+##  $ call         : language lm(formula = sr ~ pop15 + pop75 + dpi + ddpi, data = LifeCycleSavings)
+##  $ terms        :Classes 'terms', 'formula'  language sr ~ pop15 + pop75 + dpi + ddpi
+##   .. ..- attr(*, "variables")= language list(sr, pop15, pop75, dpi, ddpi)
+##   .. ..- attr(*, "factors")= int [1:5, 1:4] 0 1 0 0 0 0 0 1 0 0 ...
+##   .. .. ..- attr(*, "dimnames")=List of 2
+##   .. .. .. ..$ : chr [1:5] "sr" "pop15" "pop75" "dpi" ...
+##   .. .. .. ..$ : chr [1:4] "pop15" "pop75" "dpi" "ddpi"
+##   .. ..- attr(*, "term.labels")= chr [1:4] "pop15" "pop75" "dpi" "ddpi"
+##   .. ..- attr(*, "order")= int [1:4] 1 1 1 1
+##   .. ..- attr(*, "intercept")= int 1
+##   .. ..- attr(*, "response")= int 1
+##   .. ..- attr(*, ".Environment")=<environment: R_GlobalEnv> 
+##   .. ..- attr(*, "predvars")= language list(sr, pop15, pop75, dpi, ddpi)
+##   .. ..- attr(*, "dataClasses")= Named chr [1:5] "numeric" "numeric" "numeric" "numeric" ...
+##   .. .. ..- attr(*, "names")= chr [1:5] "sr" "pop15" "pop75" "dpi" ...
+##  $ residuals    : Named num [1:50] 0.864 0.616 2.219 -0.698 3.553 ...
+##   ..- attr(*, "names")= chr [1:50] "Australia" "Austria" "Belgium" "Bolivia" ...
+##  $ coefficients : num [1:5, 1:4] 28.566087 -0.461193 -1.691498 -0.000337 0.409695 ...
+##   ..- attr(*, "dimnames")=List of 2
+##   .. ..$ : chr [1:5] "(Intercept)" "pop15" "pop75" "dpi" ...
+##   .. ..$ : chr [1:4] "Estimate" "Std. Error" "t value" "Pr(>|t|)"
+##  $ aliased      : Named logi [1:5] FALSE FALSE FALSE FALSE FALSE
+##   ..- attr(*, "names")= chr [1:5] "(Intercept)" "pop15" "pop75" "dpi" ...
+##  $ sigma        : num 3.8
+##  $ df           : int [1:3] 5 45 5
+##  $ r.squared    : num 0.338
+##  $ adj.r.squared: num 0.28
+##  $ fstatistic   : Named num [1:3] 5.76 4 45
+##   ..- attr(*, "names")= chr [1:3] "value" "numdf" "dendf"
+##  $ cov.unscaled : num [1:5, 1:5] 3.74 -7.24e-02 -4.46e-01 -7.86e-05 -1.88e-02 ...
+##   ..- attr(*, "dimnames")=List of 2
+##   .. ..$ : chr [1:5] "(Intercept)" "pop15" "pop75" "dpi" ...
+##   .. ..$ : chr [1:5] "(Intercept)" "pop15" "pop75" "dpi" ...
+##  - attr(*, "class")= chr "summary.lm"
+# extract the coefficients and their standard error
+coef_sum <- coef(lm_sum)[, c("Estimate", "Std. Error")]
+# extract sigma
+sigma <- lm_sum$sigma
+# check the class of each component
+class(coef_sum)
+## [1] "matrix" "array"
+class(sigma)
+## [1] "numeric"
+# sha1() has methods for both matrix and numeric
+# because the values originate from floating point arithmetic it is better to use a low number of digits
+sha1(coef_sum, digits = 4)
+## [1] "3f0b0c552f94d753fcc8deb4d3e9fc11a83197af"
+sha1(sigma, digits = 4)
+## [1] "cbc83d1791ef1eeadd824ea9a038891b5889056b"
+# we want a single hash
+# combining the components in a list is a solution that works
+sha1(list(coef_sum, sigma), digits = 4)
+## [1] "476d27265365cd41662eedf059b335d38a221cc2"
+# now turn everything into an S3 method
+#   - a function with name "sha1.classname"
+#   - must have the same arguments as sha1()
+sha1.summary.lm <- function(x, digits = 4, zapsmall = 7){
+    coef_sum <- coef(x)[, c("Estimate", "Std. Error")]
+    sigma <- x$sigma
+    combined <- list(coef_sum, sigma)
+    sha1(combined, digits = digits, zapsmall = zapsmall)
+}
+sha1(lm_sum)
+## [1] "476d27265365cd41662eedf059b335d38a221cc2"
+
+# try an altered dataset
+LCS2 <- LifeCycleSavings[rownames(LifeCycleSavings) != "Zambia", ]
+lm_SR2 <- lm(sr ~ pop15 + pop75 + dpi + ddpi, data = LCS2)
+sha1(summary(lm_SR2))
+## [1] "90beb028833bf0542997fde7c3f19e5b9fdfeef4"
+ +

lm

Let’s illustrate this using the summary of a simple linear regression. Suppose that we want a hash that takes into account the coefficients, their standard error and sigma.

-
class(lm_SR)
-
## [1] "lm"
-
# str() gives the structure of the lm object
-str(lm_SR)
-
## List of 12
-##  $ coefficients : Named num [1:5] 28.566087 -0.461193 -1.691498 -0.000337 0.409695
-##   ..- attr(*, "names")= chr [1:5] "(Intercept)" "pop15" "pop75" "dpi" ...
-##  $ residuals    : Named num [1:50] 0.864 0.616 2.219 -0.698 3.553 ...
-##   ..- attr(*, "names")= chr [1:50] "Australia" "Austria" "Belgium" "Bolivia" ...
-##  $ effects      : Named num [1:50] -68.38 -14.29 7.3 -3.52 -7.94 ...
-##   ..- attr(*, "names")= chr [1:50] "(Intercept)" "pop15" "pop75" "dpi" ...
-##  $ rank         : int 5
-##  $ fitted.values: Named num [1:50] 10.57 11.45 10.95 6.45 9.33 ...
-##   ..- attr(*, "names")= chr [1:50] "Australia" "Austria" "Belgium" "Bolivia" ...
-##  $ assign       : int [1:5] 0 1 2 3 4
-##  $ qr           :List of 5
-##   ..$ qr   : num [1:50, 1:5] -7.071 0.141 0.141 0.141 0.141 ...
-##   .. ..- attr(*, "dimnames")=List of 2
-##   .. .. ..$ : chr [1:50] "Australia" "Austria" "Belgium" "Bolivia" ...
-##   .. .. ..$ : chr [1:5] "(Intercept)" "pop15" "pop75" "dpi" ...
-##   .. ..- attr(*, "assign")= int [1:5] 0 1 2 3 4
-##   ..$ qraux: num [1:5] 1.14 1.17 1.16 1.15 1.05
-##   ..$ pivot: int [1:5] 1 2 3 4 5
-##   ..$ tol  : num 1e-07
-##   ..$ rank : int 5
-##   ..- attr(*, "class")= chr "qr"
-##  $ df.residual  : int 45
-##  $ xlevels      : Named list()
-##  $ call         : language lm(formula = sr ~ pop15 + pop75 + dpi + ddpi, data = LifeCycleSavings)
-##  $ terms        :Classes 'terms', 'formula'  language sr ~ pop15 + pop75 + dpi + ddpi
-##   .. ..- attr(*, "variables")= language list(sr, pop15, pop75, dpi, ddpi)
-##   .. ..- attr(*, "factors")= int [1:5, 1:4] 0 1 0 0 0 0 0 1 0 0 ...
-##   .. .. ..- attr(*, "dimnames")=List of 2
-##   .. .. .. ..$ : chr [1:5] "sr" "pop15" "pop75" "dpi" ...
-##   .. .. .. ..$ : chr [1:4] "pop15" "pop75" "dpi" "ddpi"
-##   .. ..- attr(*, "term.labels")= chr [1:4] "pop15" "pop75" "dpi" "ddpi"
-##   .. ..- attr(*, "order")= int [1:4] 1 1 1 1
-##   .. ..- attr(*, "intercept")= int 1
-##   .. ..- attr(*, "response")= int 1
-##   .. ..- attr(*, ".Environment")=<environment: R_GlobalEnv> 
-##   .. ..- attr(*, "predvars")= language list(sr, pop15, pop75, dpi, ddpi)
-##   .. ..- attr(*, "dataClasses")= Named chr [1:5] "numeric" "numeric" "numeric" "numeric" ...
-##   .. .. ..- attr(*, "names")= chr [1:5] "sr" "pop15" "pop75" "dpi" ...
-##  $ model        :'data.frame':   50 obs. of  5 variables:
-##   ..$ sr   : num [1:50] 11.43 12.07 13.17 5.75 12.88 ...
-##   ..$ pop15: num [1:50] 29.4 23.3 23.8 41.9 42.2 ...
-##   ..$ pop75: num [1:50] 2.87 4.41 4.43 1.67 0.83 2.85 1.34 0.67 1.06 1.14 ...
-##   ..$ dpi  : num [1:50] 2330 1508 2108 189 728 ...
-##   ..$ ddpi : num [1:50] 2.87 3.93 3.82 0.22 4.56 2.43 2.67 6.51 3.08 2.8 ...
-##   ..- attr(*, "terms")=Classes 'terms', 'formula'  language sr ~ pop15 + pop75 + dpi + ddpi
-##   .. .. ..- attr(*, "variables")= language list(sr, pop15, pop75, dpi, ddpi)
-##   .. .. ..- attr(*, "factors")= int [1:5, 1:4] 0 1 0 0 0 0 0 1 0 0 ...
-##   .. .. .. ..- attr(*, "dimnames")=List of 2
-##   .. .. .. .. ..$ : chr [1:5] "sr" "pop15" "pop75" "dpi" ...
-##   .. .. .. .. ..$ : chr [1:4] "pop15" "pop75" "dpi" "ddpi"
-##   .. .. ..- attr(*, "term.labels")= chr [1:4] "pop15" "pop75" "dpi" "ddpi"
-##   .. .. ..- attr(*, "order")= int [1:4] 1 1 1 1
-##   .. .. ..- attr(*, "intercept")= int 1
-##   .. .. ..- attr(*, "response")= int 1
-##   .. .. ..- attr(*, ".Environment")=<environment: R_GlobalEnv> 
-##   .. .. ..- attr(*, "predvars")= language list(sr, pop15, pop75, dpi, ddpi)
-##   .. .. ..- attr(*, "dataClasses")= Named chr [1:5] "numeric" "numeric" "numeric" "numeric" ...
-##   .. .. .. ..- attr(*, "names")= chr [1:5] "sr" "pop15" "pop75" "dpi" ...
-##  - attr(*, "class")= chr "lm"
-
# extract the model and the terms
-lm_model <- lm_SR$model
-lm_terms <- lm_SR$terms
-# check their class
-class(lm_model) # handled by sha1()
-
## [1] "data.frame"
-
class(lm_terms) # not handled by sha1()
-
## [1] "terms"   "formula"
-
# define a method for formula
-sha1.formula <- function(x, digits = 14, zapsmall = 7, ..., algo = "sha1"){
-    sha1(as.character(x), digits = digits, zapsmall = zapsmall, algo = algo)
-}
-sha1(lm_terms)
-
## [1] "2737d209720aa7d1c0555050ad06ebe89f3850cd"
-
sha1(lm_model)
-
## [1] "27b7dd9e3e09b9577da6947b8473b63a1d0b6eb4"
-
# define a method for lm
-sha1.lm <- function(x, digits = 14, zapsmall = 7, ..., algo = "sha1"){
-    lm_model <- x$model
-    lm_terms <- x$terms
-    combined <- list(lm_model, lm_terms)
-    sha1(combined, digits = digits, zapsmall = zapsmall, ..., algo = algo)
-}
-sha1(lm_SR)
-
## [1] "7eda2a9d58e458c8e782e40ce140d62b836b2a2f"
-
sha1(lm_SR2)
-
## [1] "4d3abdb1f17bd12fdf9d9b91a2ad04c07824fe4a"
-
- -
+
class(lm_SR)
+## [1] "lm"
+# str() gives the structure of the lm object
+str(lm_SR)
+## List of 12
+##  $ coefficients : Named num [1:5] 28.566087 -0.461193 -1.691498 -0.000337 0.409695
+##   ..- attr(*, "names")= chr [1:5] "(Intercept)" "pop15" "pop75" "dpi" ...
+##  $ residuals    : Named num [1:50] 0.864 0.616 2.219 -0.698 3.553 ...
+##   ..- attr(*, "names")= chr [1:50] "Australia" "Austria" "Belgium" "Bolivia" ...
+##  $ effects      : Named num [1:50] -68.38 -14.29 7.3 -3.52 -7.94 ...
+##   ..- attr(*, "names")= chr [1:50] "(Intercept)" "pop15" "pop75" "dpi" ...
+##  $ rank         : int 5
+##  $ fitted.values: Named num [1:50] 10.57 11.45 10.95 6.45 9.33 ...
+##   ..- attr(*, "names")= chr [1:50] "Australia" "Austria" "Belgium" "Bolivia" ...
+##  $ assign       : int [1:5] 0 1 2 3 4
+##  $ qr           :List of 5
+##   ..$ qr   : num [1:50, 1:5] -7.071 0.141 0.141 0.141 0.141 ...
+##   .. ..- attr(*, "dimnames")=List of 2
+##   .. .. ..$ : chr [1:50] "Australia" "Austria" "Belgium" "Bolivia" ...
+##   .. .. ..$ : chr [1:5] "(Intercept)" "pop15" "pop75" "dpi" ...
+##   .. ..- attr(*, "assign")= int [1:5] 0 1 2 3 4
+##   ..$ qraux: num [1:5] 1.14 1.17 1.16 1.15 1.05
+##   ..$ pivot: int [1:5] 1 2 3 4 5
+##   ..$ tol  : num 1e-07
+##   ..$ rank : int 5
+##   ..- attr(*, "class")= chr "qr"
+##  $ df.residual  : int 45
+##  $ xlevels      : Named list()
+##  $ call         : language lm(formula = sr ~ pop15 + pop75 + dpi + ddpi, data = LifeCycleSavings)
+##  $ terms        :Classes 'terms', 'formula'  language sr ~ pop15 + pop75 + dpi + ddpi
+##   .. ..- attr(*, "variables")= language list(sr, pop15, pop75, dpi, ddpi)
+##   .. ..- attr(*, "factors")= int [1:5, 1:4] 0 1 0 0 0 0 0 1 0 0 ...
+##   .. .. ..- attr(*, "dimnames")=List of 2
+##   .. .. .. ..$ : chr [1:5] "sr" "pop15" "pop75" "dpi" ...
+##   .. .. .. ..$ : chr [1:4] "pop15" "pop75" "dpi" "ddpi"
+##   .. ..- attr(*, "term.labels")= chr [1:4] "pop15" "pop75" "dpi" "ddpi"
+##   .. ..- attr(*, "order")= int [1:4] 1 1 1 1
+##   .. ..- attr(*, "intercept")= int 1
+##   .. ..- attr(*, "response")= int 1
+##   .. ..- attr(*, ".Environment")=<environment: R_GlobalEnv> 
+##   .. ..- attr(*, "predvars")= language list(sr, pop15, pop75, dpi, ddpi)
+##   .. ..- attr(*, "dataClasses")= Named chr [1:5] "numeric" "numeric" "numeric" "numeric" ...
+##   .. .. ..- attr(*, "names")= chr [1:5] "sr" "pop15" "pop75" "dpi" ...
+##  $ model        :'data.frame':   50 obs. of  5 variables:
+##   ..$ sr   : num [1:50] 11.43 12.07 13.17 5.75 12.88 ...
+##   ..$ pop15: num [1:50] 29.4 23.3 23.8 41.9 42.2 ...
+##   ..$ pop75: num [1:50] 2.87 4.41 4.43 1.67 0.83 2.85 1.34 0.67 1.06 1.14 ...
+##   ..$ dpi  : num [1:50] 2330 1508 2108 189 728 ...
+##   ..$ ddpi : num [1:50] 2.87 3.93 3.82 0.22 4.56 2.43 2.67 6.51 3.08 2.8 ...
+##   ..- attr(*, "terms")=Classes 'terms', 'formula'  language sr ~ pop15 + pop75 + dpi + ddpi
+##   .. .. ..- attr(*, "variables")= language list(sr, pop15, pop75, dpi, ddpi)
+##   .. .. ..- attr(*, "factors")= int [1:5, 1:4] 0 1 0 0 0 0 0 1 0 0 ...
+##   .. .. .. ..- attr(*, "dimnames")=List of 2
+##   .. .. .. .. ..$ : chr [1:5] "sr" "pop15" "pop75" "dpi" ...
+##   .. .. .. .. ..$ : chr [1:4] "pop15" "pop75" "dpi" "ddpi"
+##   .. .. ..- attr(*, "term.labels")= chr [1:4] "pop15" "pop75" "dpi" "ddpi"
+##   .. .. ..- attr(*, "order")= int [1:4] 1 1 1 1
+##   .. .. ..- attr(*, "intercept")= int 1
+##   .. .. ..- attr(*, "response")= int 1
+##   .. .. ..- attr(*, ".Environment")=<environment: R_GlobalEnv> 
+##   .. .. ..- attr(*, "predvars")= language list(sr, pop15, pop75, dpi, ddpi)
+##   .. .. ..- attr(*, "dataClasses")= Named chr [1:5] "numeric" "numeric" "numeric" "numeric" ...
+##   .. .. .. ..- attr(*, "names")= chr [1:5] "sr" "pop15" "pop75" "dpi" ...
+##  - attr(*, "class")= chr "lm"
+# extract the model and the terms
+lm_model <- lm_SR$model
+lm_terms <- lm_SR$terms
+# check their class
+class(lm_model) # handled by sha1()
+## [1] "data.frame"
+class(lm_terms) # not handled by sha1()
+## [1] "terms"   "formula"
+# define a method for formula
+sha1.formula <- function(x, digits = 14, zapsmall = 7, ..., algo = "sha1"){
+    sha1(as.character(x), digits = digits, zapsmall = zapsmall, algo = algo)
+}
+sha1(lm_terms)
+## [1] "2737d209720aa7d1c0555050ad06ebe89f3850cd"
+sha1(lm_model)
+## [1] "27b7dd9e3e09b9577da6947b8473b63a1d0b6eb4"
+# define a method for lm
+sha1.lm <- function(x, digits = 14, zapsmall = 7, ..., algo = "sha1"){
+    lm_model <- x$model
+    lm_terms <- x$terms
+    combined <- list(lm_model, lm_terms)
+    sha1(combined, digits = digits, zapsmall = zapsmall, ..., algo = algo)
+}
+sha1(lm_SR)
+## [1] "7eda2a9d58e458c8e782e40ce140d62b836b2a2f"
+sha1(lm_SR2)
+## [1] "4d3abdb1f17bd12fdf9d9b91a2ad04c07824fe4a"
+ + +

Using hashes to track changes in analysis

Use case

    @@ -631,7 +692,7 @@

    file fingerprint ~ sha1() on the stable parts

    status fingerprint ~ sha1() on the parts that result for the model

-
    +
    1. Prepare analysis objects
    2. Store each analysis object in a rda file which uses the file fingerprint as filename
        @@ -644,22 +705,8 @@
      • Otherwise run the analysis and update the status and status fingerprint
    -
- - - - - - - - - + +
+
diff -Nru r-cran-digest-0.6.25/inst/doc/sha1.R r-cran-digest-0.6.27/inst/doc/sha1.R --- r-cran-digest-0.6.25/inst/doc/sha1.R 2020-02-22 14:55:27.000000000 +0000 +++ r-cran-digest-0.6.27/inst/doc/sha1.R 2020-10-20 13:03:32.000000000 +0000 @@ -1,3 +1,10 @@ +## ----include=FALSE------------------------------------------------------------ +knitr::opts_chunk$set(collapse = TRUE, + class.output = NULL, + class.message = NULL, + class.warning = NULL, + class.error = NULL) + ## ----faq7_31------------------------------------------------------------------ # FAQ 7.31 a0 <- 2 diff -Nru r-cran-digest-0.6.25/inst/doc/sha1.Rmd r-cran-digest-0.6.27/inst/doc/sha1.Rmd --- r-cran-digest-0.6.25/inst/doc/sha1.Rmd 2018-01-21 13:50:54.000000000 +0000 +++ r-cran-digest-0.6.27/inst/doc/sha1.Rmd 2020-10-17 02:51:09.000000000 +0000 @@ -2,13 +2,26 @@ title: "Calculating SHA1 hashes with digest() and sha1()" author: "Thierry Onkelinx and Dirk Eddelbuettel" date: "`r Sys.Date()`" -output: rmarkdown::html_vignette +output: + minidown::mini_document: + framework: water + keep_md: TRUE + vignette: > %\VignetteIndexEntry{sha1() versus digest()} %\VignetteEngine{knitr::rmarkdown} %\VignetteEncoding{UTF-8} --- +```{r include=FALSE} +knitr::opts_chunk$set(collapse = TRUE, + class.output = NULL, + class.message = NULL, + class.warning = NULL, + class.error = NULL) +``` + + NB: This vignette is work-in-progress and not yet complete. ## Short intro on hashes diff -Nru r-cran-digest-0.6.25/inst/tinytest/test_blake3.R r-cran-digest-0.6.27/inst/tinytest/test_blake3.R --- r-cran-digest-0.6.25/inst/tinytest/test_blake3.R 1970-01-01 00:00:00.000000000 +0000 +++ r-cran-digest-0.6.27/inst/tinytest/test_blake3.R 2020-10-14 15:09:09.000000000 +0000 @@ -0,0 +1,63 @@ +suppressMessages(library(digest)) + +isWindows <- Sys.info()[["sysname"]] == "Windows" + +res <- digest(charToRaw("test"), "blake3", serialize = FALSE) + +# generated using the blake3 rust implementation +expected <- "4878ca0425c739fa427f7eda20fe845f6b2e46ba5fe2a14df5b1e32f50603215" +expect_equal( + res, + expected +) + +res <- digest(charToRaw("test"), "blake3", serialize = FALSE, raw = TRUE) +expect_equal( + paste0(res, collapse = ""), + expected +) + +## blake3 example +blake3Input <- + c( + "abc", + "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq", + "" + ) +blake3Output <- + c( + "6437b3ac38465133ffb63b75273a8db548c558465d79db03fd359c6cd5bd9d85", + "c19012cc2aaf0dc3d8e5c45a1b79114d2df42abb2a410bf54be09e891af06ff8", + "af1349b9f5f9a1a6a0404dea36dcc9499bcb25c9adc112b7cc9a93cae41f3262" + ) +for (i in seq(along = blake3Input)) { + blake3 <- digest(blake3Input[i], algo = "blake3", serialize = FALSE) + expect_equal( + blake3, + blake3Output[i] + ) +} + +if (isWindows) exit_file("Skipping remainder on Windows platform") + +# test file +# used the b3sum rust crate +file <- tempfile() +writeLines("test", file) +res <- digest(file = file, algo = "blake3") +try(file.remove(file)) +expect_equal( + res, + "dea2b412aa90f1b43a06ca5e8b8feafec45ae1357971322749480f4e1572eaa2" +) + +# try another long text +file <- tempfile() +text <- paste0(rep(LETTERS, 1000), collapse = "-") +writeLines(text, file) +res <- digest(file = file, algo = "blake3") +try(file.remove(file)) +expect_equal( + res, + "a84aef38d9a7ad55fa458d9d1857eb895832e358385e8b6466c6af54ea46eeaa" +) diff -Nru r-cran-digest-0.6.25/inst/tinytest/test_digest.R r-cran-digest-0.6.27/inst/tinytest/test_digest.R --- r-cran-digest-0.6.25/inst/tinytest/test_digest.R 2019-10-21 00:42:29.000000000 +0000 +++ r-cran-digest-0.6.27/inst/tinytest/test_digest.R 2020-10-07 17:38:22.000000000 +0000 @@ -297,3 +297,20 @@ #expect_true( # grepl("spookyhash algorithm is not available without serialization.", error.message) #) + + +## Ensure that all values of algo are actually allowed (in case a new one is +## added in the future). The call to match.arg() passes choices explicitly +## because it is significantly faster to do it than to have it automatically +## infer the possible choices from the function's formals. + +# Grab the possible values of algo, then call digest() for each one. +algos <- eval(formals(digest)$algo) +for (algo in algos) { + digest(123, algo = algo) +} +# Same for getVDigest +algos <- eval(formals(getVDigest)$algo) +for (algo in algos) { + getVDigest(algo = algo) +} diff -Nru r-cran-digest-0.6.25/man/digest.Rd r-cran-digest-0.6.27/man/digest.Rd --- r-cran-digest-0.6.25/man/digest.Rd 2019-10-02 12:09:13.000000000 +0000 +++ r-cran-digest-0.6.27/man/digest.Rd 2020-10-15 18:16:57.000000000 +0000 @@ -14,7 +14,8 @@ } \usage{ digest(object, algo=c("md5", "sha1", "crc32", "sha256", "sha512", - "xxhash32", "xxhash64", "murmur32", "spookyhash"), serialize=TRUE, file=FALSE, + "xxhash32", "xxhash64", "murmur32", "spookyhash", + "blake3"), serialize=TRUE, file=FALSE, length=Inf, skip="auto", ascii=FALSE, raw=FALSE, seed=0, errormode=c("stop","warn","silent"), serializeVersion=.getSerializeVersion()) @@ -26,7 +27,7 @@ \item{algo}{The algorithms to be used; currently available choices are \code{md5}, which is also the default, \code{sha1}, \code{crc32}, \code{sha256}, \code{sha512}, \code{xxhash32}, \code{xxhash64}, - \code{murmur32} and \code{spookyhash}.} + \code{murmur32}, \code{spookyhash} and \code{blake3}.} \item{serialize}{A logical variable indicating whether the object should be serialized using \code{serialize} (in ASCII form). Setting this to \code{FALSE} allows to compare the digest @@ -95,12 +96,14 @@ that integrates R's serialization directly with the algorithm allowing for memory-efficient incremental calculation of the hash is by Gabe Becker. + For blake3, the C implementation by Samuel Neves and Jack O'Connor is used. + Please note that this package is not meant to be used for cryptographic purposes for which more comprehensive (and widely tested) libraries such as OpenSSL should be used. Also, it is known that crc32 is not collision-proof. For sha-1, recent results indicate certain cryptographic weaknesses as well. For more details, see for example - \url{http://www.schneier.com/blog/archives/2005/02/cryptanalysis_o.html}. + \url{https://www.schneier.com/blog/archives/2005/02/cryptanalysis_o.html}. } \section{Change Management}{ Version 0.6.16 of digest corrects an error in which \code{crc32} was not @@ -110,7 +113,7 @@ consistent with prior version (but not be consistentnly eight characters). } \references{ - MD5: \url{http://www.ietf.org/rfc/rfc1321.txt}. + MD5: \url{https://www.ietf.org/rfc/rfc1321.txt}. SHA-1: \url{https://en.wikipedia.org/wiki/SHA-1}. SHA-256: \url{https://csrc.nist.gov/publications/fips/fips180-2/fips180-2withchangenotice.pdf}. @@ -119,7 +122,7 @@ \url{https://en.wikipedia.org/wiki/Cyclic_redundancy_check} for general information on CRC algorithms. - \url{https://www.aarongifford.com/computers/sha.html} for the + \url{https://aarongifford.com/computers/sha.html} for the integrated C implementation of sha-512. The page for the code underlying the C functions used here for sha-1 @@ -139,6 +142,9 @@ \url{https://github.com/aappleby/smhasher} for documentation on MurmurHash. \url{https://burtleburtle.net/bob/hash/spooky.html} for the original source code of SpookyHash. + + \url{https://github.com/BLAKE3-team/BLAKE3/} for the original source code of blake3. + } \author{Dirk Eddelbuettel \email{edd@debian.org} for the \R interface; Antoine Lucas for the integration of crc32; Jarek Tuszynski for the @@ -294,6 +300,22 @@ stopifnot(identical(spooky, spookyOutput[i])) } +## blake3 example +blake3Input <- + c("abc", + "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq", + "") +blake3Output <- + c("6437b3ac38465133ffb63b75273a8db548c558465d79db03fd359c6cd5bd9d85", + "c19012cc2aaf0dc3d8e5c45a1b79114d2df42abb2a410bf54be09e891af06ff8", + "af1349b9f5f9a1a6a0404dea36dcc9499bcb25c9adc112b7cc9a93cae41f3262") + +for (i in seq(along=blake3Input)) { + blake3 <- digest(blake3Input[i], algo="blake3", serialize=FALSE) + cat(blake3, "\n") + stopifnot(identical(blake3, blake3Output[i])) +} + # example of a digest of a standard R list structure digest(list(LETTERS, data.frame(a=letters[1:5], b=matrix(1:10,ncol=2)))) diff -Nru r-cran-digest-0.6.25/man/hmac.Rd r-cran-digest-0.6.27/man/hmac.Rd --- r-cran-digest-0.6.25/man/hmac.Rd 2019-10-02 12:09:40.000000000 +0000 +++ r-cran-digest-0.6.27/man/hmac.Rd 2020-10-15 18:16:57.000000000 +0000 @@ -32,7 +32,7 @@ digest as specified in the RFC 2104. } \references{ - MD5: \url{http://www.ietf.org/rfc/rfc1321.txt}. + MD5: \url{https://www.ietf.org/rfc/rfc1321.txt}. SHA-1: \url{https://en.wikipedia.org/wiki/SHA-1}. SHA-256: \url{https://csrc.nist.gov/publications/fips/fips180-2/fips180-2withchangenotice.pdf}. @@ -41,7 +41,7 @@ \url{https://en.wikipedia.org/wiki/Cyclic_redundancy_check} for general information on CRC algorithms. - \url{https://www.aarongifford.com/computers/sha.html} for the + \url{https://aarongifford.com/computers/sha.html} for the integrated C implementation of sha-512. The page for the code underlying the C functions used here for sha-1 diff -Nru r-cran-digest-0.6.25/man/sha1.Rd r-cran-digest-0.6.27/man/sha1.Rd --- r-cran-digest-0.6.25/man/sha1.Rd 2019-12-13 11:36:29.000000000 +0000 +++ r-cran-digest-0.6.27/man/sha1.Rd 2020-03-05 21:25:05.000000000 +0000 @@ -61,7 +61,7 @@ \item{x}{the object to calculate the SHA1} \item{digits}{the approximate number of significant digits in base 10. Will -be converted to a base 16 equivalent. Defaults to \code{digits = 14}, expect for +be converted to a base 16 equivalent. Defaults to \code{digits = 14}, except for sha1.anova where \code{digits = 4}} \item{zapsmall}{the approximate negative magnitude of the smallest relevant diff -Nru r-cran-digest-0.6.25/MD5 r-cran-digest-0.6.27/MD5 --- r-cran-digest-0.6.25/MD5 2020-02-23 00:10:02.000000000 +0000 +++ r-cran-digest-0.6.27/MD5 2020-10-24 22:10:06.000000000 +0000 @@ -1,27 +1,28 @@ -a43f2b56a855207fc2447e26e66672d3 *ChangeLog -688421add5ab2160fc88cebf42cebc88 *DESCRIPTION +7832ea9b03c0220978b752dd512b91fe *ChangeLog +bca5aed044851cff3721b907a410b914 *DESCRIPTION 14402ee218da75987f233d61fa84ab8d *NAMESPACE 82c734f29cbd8b0e29893316d3d5a480 *R/AES.R -0159b8f6047e5d77fe57f00763fd7d78 *R/digest.R +82f716f31c280b6ed497d65744547a96 *R/digest.R 481f6406e42aeaf0eb9da80ebd1c3601 *R/digest2int.R 3afa681f8cae1c57282057525baf3731 *R/hmac.R -5e4f6d949319a0c0f4401620e04ed03d *R/init.R +214eeb4956a952e880cc31f8bfa61606 *R/init.R 14cddbd8465ff30bec5a355855130888 *R/sha1.R -624e0d2a647f52c6a97162d7e8dc1e71 *R/vdigest.R +08f81c9f8bcc8487184b483e1aa89dc1 *R/vdigest.R a673761518607ae09b8f6357c6304c60 *R/zzz.R -ab78958e35a734d7e7606e04019aa2d8 *README.md -09a2660ed8d537e0d8fed28a84685d44 *build/vignette.rds +1483dba308cf66d511af06f3080be96b *README.md +458b788e371981ec62929aaa7bb708c0 *build/vignette.rds b47a60fc7c78d503aba2c3af8d8f3d9a *cleanup cdd17dda453ee280bdbaac3b405874d6 *demo/00Index 247a7f1a523cb39feb2941b00b82906a *demo/vectorised.R b234ee4d69f5fce4486a80fdaf4a4263 *inst/GPL-2 -2110052aea8c1b68a2a46a44adb0d930 *inst/doc/sha1.R -912b8395bbeb8c511e1f75b46d8157a6 *inst/doc/sha1.Rmd -d1014ff342c28aed7a055cedbae95a79 *inst/doc/sha1.html +e6dfa6dd02e6629409e8309d1a3765df *inst/doc/sha1.R +2d671c998321d0cd98dffcfb18ae992f *inst/doc/sha1.Rmd +7225013804cf54f6a63e9974fe27227e *inst/doc/sha1.html 82a62fb6337a2be9ae069fee3d50b614 *inst/include/pmurhashAPI.h 4d75623e0ffe3c04eac1752b7d3d8f6b *inst/tinytest/test_aes.R +f930ce5c1f9bbcd3b6fe97fd7b5b27f8 *inst/tinytest/test_blake3.R 052184be0e884920438e1779a03f8759 *inst/tinytest/test_crc32.R -453c3717652e79d5c296d73548e7b2f9 *inst/tinytest/test_digest.R +8ec7f892f2bcb4b146f78078f987237b *inst/tinytest/test_digest.R 6681e580fd409258ed4fbdeb77a14cd4 *inst/tinytest/test_digest2int.R 7bcee9c21c2214e6c5b1850bf96cbe24 *inst/tinytest/test_encoding.R 2b3efdc9712ac7f956f79a80da4d0d00 *inst/tinytest/test_hmac.R @@ -31,20 +32,25 @@ 612e0a4832b86b69c8c04f2916f3b16a *inst/tinytest/test_raw.R 6e3548a6e9bdf2681f6bdc173ee0a8d2 *inst/tinytest/test_sha1.R 7f560b14f8ccea6d6b0f14a5db4d77b5 *man/AES.Rd -b34c122fe4fbc771b24466ed3bfe1823 *man/digest.Rd +29c8312bfbaa4b9ec90546e21c40501c *man/digest.Rd d45f3ac5c54a9b0f08ba342b22b9d332 *man/digest2int.Rd -ef54bd40938fb18aecb93d5b12e6d8f2 *man/hmac.Rd +4e1c2885b0335799671cc924991f5471 *man/hmac.Rd 9d566e9d729f574b922fb174f20f1d3b *man/makeRaw.Rd -a304624860fb468a819ffdcfb444f761 *man/sha1.Rd +b00315435b5f3bf83ef66af1911270ec *man/sha1.Rd f1cd7ff7ffb82a0d16b703cd320d70ed *man/vdigest.Rd 1f06212fc4eccfc3c1eb9a2738b5926b *src/Makevars f2ba0dcaa6ae788bb3cbca6a9e52b6d2 *src/SpookyV2.cpp 29f0a9285b34812cdf91dcf65d0594c4 *src/SpookyV2.h a529d657388750546c7e03442eb01c31 *src/aes.c 12398d282dd19894ed5548fc55fff7c4 *src/aes.h +90974df9d6984b6a4f06aae3d75bbe6a *src/blake3.c +23350b7351de5b42a6aff1bbeadd4338 *src/blake3.h +5d44f6c6e0e8333b0d343beba8c4f772 *src/blake3_dispatch.c +653116045f28590f7408b41ee0d9b152 *src/blake3_impl.h +ceb8de8ba5e178fef7ed7964ae300ee4 *src/blake3_portable.c 2cb721d135ea8eec6aeceedcd26706e5 *src/crc32.c 4d1cfa258e52f3f91b09afdba248fec5 *src/crc32.h -43dcaeac25cb9e4184d8fff8ceda10d5 *src/digest.c +35eb9b72ef819453891650a9034765a6 *src/digest.c dc3762ab113fccd9ae068ae763906c89 *src/digest2int.c 05344eb938698407395c04bf91c9c150 *src/init.c 972a4374ab3e726afbab89332562daf2 *src/md5.c @@ -65,4 +71,4 @@ 95fdcdaa6c64cf71726e192d5eb1b608 *src/zlib.h c4564a01a95ad6d04fec9c4ef0561cee *src/zutil.h 55b29bb6c8292127b5a709d9eeb3c3bb *tests/tinytest.R -912b8395bbeb8c511e1f75b46d8157a6 *vignettes/sha1.Rmd +2d671c998321d0cd98dffcfb18ae992f *vignettes/sha1.Rmd diff -Nru r-cran-digest-0.6.25/R/digest.R r-cran-digest-0.6.27/R/digest.R --- r-cran-digest-0.6.25/R/digest.R 2020-02-17 23:34:09.000000000 +0000 +++ r-cran-digest-0.6.27/R/digest.R 2020-10-07 17:38:22.000000000 +0000 @@ -24,7 +24,8 @@ ## along with digest. If not, see . digest <- function(object, algo=c("md5", "sha1", "crc32", "sha256", "sha512", - "xxhash32", "xxhash64", "murmur32", "spookyhash"), + "xxhash32", "xxhash64", "murmur32", + "spookyhash", "blake3"), serialize=TRUE, file=FALSE, length=Inf, @@ -35,8 +36,12 @@ errormode=c("stop","warn","silent"), serializeVersion=.getSerializeVersion()) { - algo <- match.arg(algo) - errormode <- match.arg(errormode) + # Explicitly specify choices; this is much faster than having match.arg() + # infer them from the function's formals. + algo <- match.arg(algo, c("md5", "sha1", "crc32", "sha256", "sha512", + "xxhash32", "xxhash64", "murmur32", + "spookyhash", "blake3")) + errormode <- match.arg(errormode, c("stop", "warn", "silent")) if (is.infinite(length)) { length <- -1 # internally we use -1 for infinite len @@ -47,32 +52,31 @@ file <- TRUE # nocov } - streaming_algos <- c("spookyhash") - non_streaming_algos <- c("md5", "sha1", "crc32", "sha256", "sha512", - "xxhash32", "xxhash64", "murmur32") - if(algo %in% streaming_algos && !serialize){ + is_streaming_algo <- algo == "spookyhash" + + if (is_streaming_algo && !serialize) { .errorhandler(paste0(algo, " algorithm is not available without serialization."), # #nocov mode=errormode) # #nocov } if (serialize && !file) { - if(algo %in% non_streaming_algos){ - ## support the 'nosharing' option in pqR's base::serialize() + if (!is_streaming_algo) { + ## support the 'nosharing' option in pqR's serialize() object <- if (.hasNoSharing()) - base::serialize (object, connection=NULL, ascii=ascii, + serialize (object, connection=NULL, ascii=ascii, nosharing=TRUE, version=serializeVersion) else - base::serialize (object, connection=NULL, ascii=ascii, + serialize (object, connection=NULL, ascii=ascii, version=serializeVersion) } ## we support raw vectors, so no mangling of 'object' is necessary ## regardless of R version ## skip="auto" - skips the serialization header [SU] - if (any(!is.na(pmatch(skip,"auto")))) + if (is.character(skip) && skip == "auto") skip <- set_skip(object, ascii) } else if (!is.character(object) && !inherits(object,"raw") && - algo %in% non_streaming_algos) { + !is_streaming_algo) { return(.errorhandler(paste("Argument object must be of type character", # #nocov "or raw vector if serialize is FALSE"), mode=errormode)) # #nocov } @@ -80,7 +84,7 @@ return(.errorhandler("file=TRUE can only be used with a character object", # #nocov mode=errormode)) # #nocov - if (file && algo %in% streaming_algos) + if (file && is_streaming_algo) return(.errorhandler(paste0(algo, " algorithm can not be used with files."), # #nocov mode=errormode)) # #nocov @@ -98,7 +102,7 @@ ## into 0 because auto should have been converted into a number earlier ## if it was valid [SU] if (is.character(skip)) skip <- 0 - if (algo %in% non_streaming_algos) { + if (!is_streaming_algo) { val <- .Call(digest_impl, object, as.integer(algoint), @@ -144,7 +148,8 @@ xxhash32 = 6, xxhash64 = 7, murmur32 = 8, - spookyhash = 9 + spookyhash = 9, + blake3 = 10 ) ## HB 14 Mar 2007: diff -Nru r-cran-digest-0.6.25/R/init.R r-cran-digest-0.6.27/R/init.R --- r-cran-digest-0.6.25/R/init.R 2019-12-06 04:07:52.000000000 +0000 +++ r-cran-digest-0.6.27/R/init.R 2020-10-07 17:38:22.000000000 +0000 @@ -32,7 +32,7 @@ .pkgenv[["isWindows"]] <- Sys.info()[["sysname"]] == "Windows" ## cache if serialize() supports 'nosharing' - .pkgenv[["hasNoSharing"]] <- "nosharing" %in% names(formals(base::serialize)) + .pkgenv[["hasNoSharing"]] <- "nosharing" %in% names(formals(serialize)) } .getSerializeVersion <- function() { @@ -61,6 +61,6 @@ } .hasNoSharing <- function() { - ## return the cached value of "nosharing" %in% names(formals(base::serialize)) + ## return the cached value of "nosharing" %in% names(formals(serialize)) .pkgenv[["hasNoSharing"]] } diff -Nru r-cran-digest-0.6.25/R/vdigest.R r-cran-digest-0.6.27/R/vdigest.R --- r-cran-digest-0.6.25/R/vdigest.R 2019-12-06 04:07:52.000000000 +0000 +++ r-cran-digest-0.6.27/R/vdigest.R 2020-10-07 17:38:22.000000000 +0000 @@ -21,8 +21,9 @@ getVDigest <- function(algo = c("md5", "sha1", "crc32", "sha256", "sha512", "xxhash32", "xxhash64", "murmur32", "spookyhash"), errormode=c("stop","warn","silent")){ - algo <- match.arg(algo) - errormode <- match.arg(errormode) + algo <- match.arg(algo, c("md5", "sha1", "crc32", "sha256", "sha512", + "xxhash32", "xxhash64", "murmur32", "spookyhash")) + errormode <- match.arg(errormode, c("stop","warn","silent")) algoint <- algo_int(algo) non_streaming_algos <- c("md5", "sha1", "crc32", "sha256", "sha512", "xxhash32", "xxhash64", "murmur32") @@ -50,7 +51,7 @@ } if (serialize && !file) { - ## support the 'nosharing' option in pqR's base::serialize() + ## support the 'nosharing' option in pqR's serialize() object <- if (.hasNoSharing()) serialize_( object, @@ -170,6 +171,6 @@ serialize_ <- function(object, ...){ if (length(object)) - return(lapply(object, base::serialize, ...)) - base::serialize(object, ...) + return(lapply(object, serialize, ...)) + serialize(object, ...) } diff -Nru r-cran-digest-0.6.25/README.md r-cran-digest-0.6.27/README.md --- r-cran-digest-0.6.25/README.md 2019-10-19 11:08:13.000000000 +0000 +++ r-cran-digest-0.6.27/README.md 2020-10-17 14:54:14.000000000 +0000 @@ -3,12 +3,14 @@ [![Build Status](https://travis-ci.org/eddelbuettel/digest.svg)](https://travis-ci.org/eddelbuettel/digest) [![License](http://img.shields.io/badge/license-GPL%20%28%3E=%202%29-brightgreen.svg?style=flat)](http://www.gnu.org/licenses/gpl-2.0.html) [![CRAN](http://www.r-pkg.org/badges/version/digest)](https://cran.r-project.org/package=digest) -[![Dependencies](https://tinyverse.netlify.com/badge/digest)](https://cran.r-project.org/package=digest) -[![Downloads](http://cranlogs.r-pkg.org/badges/digest?color=brightgreen)](http://www.r-pkg.org/pkg/digest) +[![Dependencies](https://tinyverse.netlify.com/badge/digest)](https://cran.r-project.org/package=digest) +[![Debian package](https://img.shields.io/debian/v/r-cran-digest/sid?color=green)](https://packages.debian.org/sid/r-cran-digest) +[![Downloads](http://cranlogs.r-pkg.org/badges/digest?color=brightgreen)](https://www.r-pkg.org:443/pkg/digest) [![CRAN use](https://jangorecki.gitlab.io/rdeps/digest/CRAN_usage.svg?sanitize=true)](https://cran.r-project.org/package=digest) [![CRAN indirect](https://jangorecki.gitlab.io/rdeps/digest/indirect_usage.svg?sanitize=true)](https://cran.r-project.org/package=digest) [![BioConductor use](https://jangorecki.gitlab.io/rdeps/digest/BioC_usage.svg?sanitize=true)](https://cran.r-project.org/package=digest) -[![Code Coverage](https://img.shields.io/codecov/c/github/eddelbuettel/digest/master.svg)](https://codecov.io/gh/eddelbuettel/digest) +[![Code Coverage](https://img.shields.io/codecov/c/github/eddelbuettel/digest/master.svg)](https://codecov.io/gh/eddelbuettel/digest) +[![Documentation](https://img.shields.io/badge/documentation-is_here-blue)](https://eddelbuettel.github.io/digest/) Compact hash representations of arbitrary R objects @@ -16,8 +18,8 @@ The digest package provides a principal function `digest()` for the creation of hash digests of arbitrary R objects (using the md5, sha-1, -sha-256, crc32, xxhash, murmurhash, and spookyhash algorithms) permitting -easy comparison of R language objects. +sha-256, crc32, xxhash, murmurhash, spookyhash and blake3 algorithms) +permitting easy comparison of R language objects. #### Examples @@ -77,8 +79,10 @@ Dirk Eddelbuettel, with contributions by Antoine Lucas, Jarek Tuszynski, Henrik Bengtsson, Simon Urbanek, Mario Frasca, Bryan Lewis, Murray Stokely, Hannes Muehleisen, Duncan Murdoch, Jim Hester, Wush Wu, Qiang Kou, Thierry -Onkelinx, Michel Lang, Viliam Simko, Kurt Hornik, Radford Neal, Kendon Bell, -Matthew de Queljoe, Ion Suruceanu, and Bill Denney. +Onkelinx, Michel Lang, Viliam Simko, Kurt Hornik, Radford Neal, Kendon Bell, +Matthew de Queljoe, Ion Suruceanu, Bill Denney, Dirk Schumacher, and Winston +Chang. + ### License diff -Nru r-cran-digest-0.6.25/src/blake3.c r-cran-digest-0.6.27/src/blake3.c --- r-cran-digest-0.6.25/src/blake3.c 1970-01-01 00:00:00.000000000 +0000 +++ r-cran-digest-0.6.27/src/blake3.c 2020-05-20 11:42:23.000000000 +0000 @@ -0,0 +1,606 @@ +/* blake3 license: + * The C code is copyright Samuel Neves and Jack O'Connor, 2019-2020. + * This work is released into the public domain with CC0 1.0. + * Alternatively, it is licensed under the Apache License 2.0. + * For full license see + * https://raw.githubusercontent.com/BLAKE3-team/BLAKE3/master/LICENSE + */ + +#include +#include +#include + +#include "blake3.h" +#include "blake3_impl.h" + +INLINE void chunk_state_init(blake3_chunk_state *self, const uint32_t key[8], + uint8_t flags) { + memcpy(self->cv, key, BLAKE3_KEY_LEN); + self->chunk_counter = 0; + memset(self->buf, 0, BLAKE3_BLOCK_LEN); + self->buf_len = 0; + self->blocks_compressed = 0; + self->flags = flags; +} + +INLINE void chunk_state_reset(blake3_chunk_state *self, const uint32_t key[8], + uint64_t chunk_counter) { + memcpy(self->cv, key, BLAKE3_KEY_LEN); + self->chunk_counter = chunk_counter; + self->blocks_compressed = 0; + memset(self->buf, 0, BLAKE3_BLOCK_LEN); + self->buf_len = 0; +} + +INLINE size_t chunk_state_len(const blake3_chunk_state *self) { + return (BLAKE3_BLOCK_LEN * (size_t)self->blocks_compressed) + + ((size_t)self->buf_len); +} + +INLINE size_t chunk_state_fill_buf(blake3_chunk_state *self, + const uint8_t *input, size_t input_len) { + size_t take = BLAKE3_BLOCK_LEN - ((size_t)self->buf_len); + if (take > input_len) { + take = input_len; + } + uint8_t *dest = self->buf + ((size_t)self->buf_len); + memcpy(dest, input, take); + self->buf_len += (uint8_t)take; + return take; +} + +INLINE uint8_t chunk_state_maybe_start_flag(const blake3_chunk_state *self) { + if (self->blocks_compressed == 0) { + return CHUNK_START; + } else { + return 0; + } +} + +typedef struct { + uint32_t input_cv[8]; + uint64_t counter; + uint8_t block[BLAKE3_BLOCK_LEN]; + uint8_t block_len; + uint8_t flags; +} output_t; + +INLINE output_t make_output(const uint32_t input_cv[8], + const uint8_t block[BLAKE3_BLOCK_LEN], + uint8_t block_len, uint64_t counter, + uint8_t flags) { + output_t ret; + memcpy(ret.input_cv, input_cv, 32); + memcpy(ret.block, block, BLAKE3_BLOCK_LEN); + ret.block_len = block_len; + ret.counter = counter; + ret.flags = flags; + return ret; +} + +// Chaining values within a given chunk (specifically the compress_in_place +// interface) are represented as words. This avoids unnecessary bytes<->words +// conversion overhead in the portable implementation. However, the hash_many +// interface handles both user input and parent node blocks, so it accepts +// bytes. For that reason, chaining values in the CV stack are represented as +// bytes. +INLINE void output_chaining_value(const output_t *self, uint8_t cv[32]) { + uint32_t cv_words[8]; + memcpy(cv_words, self->input_cv, 32); + blake3_compress_in_place(cv_words, self->block, self->block_len, + self->counter, self->flags); + memcpy(cv, cv_words, 32); +} + +INLINE void output_root_bytes(const output_t *self, uint64_t seek, uint8_t *out, + size_t out_len) { + uint64_t output_block_counter = seek / 64; + size_t offset_within_block = seek % 64; + uint8_t wide_buf[64]; + while (out_len > 0) { + blake3_compress_xof(self->input_cv, self->block, self->block_len, + output_block_counter, self->flags | ROOT, wide_buf); + size_t available_bytes = 64 - offset_within_block; + size_t memcpy_len; + if (out_len > available_bytes) { + memcpy_len = available_bytes; + } else { + memcpy_len = out_len; + } + memcpy(out, wide_buf + offset_within_block, memcpy_len); + out += memcpy_len; + out_len -= memcpy_len; + output_block_counter += 1; + offset_within_block = 0; + } +} + +INLINE void chunk_state_update(blake3_chunk_state *self, const uint8_t *input, + size_t input_len) { + if (self->buf_len > 0) { + size_t take = chunk_state_fill_buf(self, input, input_len); + input += take; + input_len -= take; + if (input_len > 0) { + blake3_compress_in_place( + self->cv, self->buf, BLAKE3_BLOCK_LEN, self->chunk_counter, + self->flags | chunk_state_maybe_start_flag(self)); + self->blocks_compressed += 1; + self->buf_len = 0; + memset(self->buf, 0, BLAKE3_BLOCK_LEN); + } + } + + while (input_len > BLAKE3_BLOCK_LEN) { + blake3_compress_in_place(self->cv, input, BLAKE3_BLOCK_LEN, + self->chunk_counter, + self->flags | chunk_state_maybe_start_flag(self)); + self->blocks_compressed += 1; + input += BLAKE3_BLOCK_LEN; + input_len -= BLAKE3_BLOCK_LEN; + } + + size_t take = chunk_state_fill_buf(self, input, input_len); + input += take; + input_len -= take; +} + +INLINE output_t chunk_state_output(const blake3_chunk_state *self) { + uint8_t block_flags = + self->flags | chunk_state_maybe_start_flag(self) | CHUNK_END; + return make_output(self->cv, self->buf, self->buf_len, self->chunk_counter, + block_flags); +} + +INLINE output_t parent_output(const uint8_t block[BLAKE3_BLOCK_LEN], + const uint32_t key[8], uint8_t flags) { + return make_output(key, block, BLAKE3_BLOCK_LEN, 0, flags | PARENT); +} + +// Given some input larger than one chunk, return the number of bytes that +// should go in the left subtree. This is the largest power-of-2 number of +// chunks that leaves at least 1 byte for the right subtree. +INLINE size_t left_len(size_t content_len) { + // Subtract 1 to reserve at least one byte for the right side. content_len + // should always be greater than BLAKE3_CHUNK_LEN. + size_t full_chunks = (content_len - 1) / BLAKE3_CHUNK_LEN; + return round_down_to_power_of_2(full_chunks) * BLAKE3_CHUNK_LEN; +} + +// Use SIMD parallelism to hash up to MAX_SIMD_DEGREE chunks at the same time +// on a single thread. Write out the chunk chaining values and return the +// number of chunks hashed. These chunks are never the root and never empty; +// those cases use a different codepath. +INLINE size_t compress_chunks_parallel(const uint8_t *input, size_t input_len, + const uint32_t key[8], + uint64_t chunk_counter, uint8_t flags, + uint8_t *out) { +#if defined(BLAKE3_TESTING) + assert(0 < input_len); + assert(input_len <= MAX_SIMD_DEGREE * BLAKE3_CHUNK_LEN); +#endif + + const uint8_t *chunks_array[MAX_SIMD_DEGREE]; + size_t input_position = 0; + size_t chunks_array_len = 0; + while (input_len - input_position >= BLAKE3_CHUNK_LEN) { + chunks_array[chunks_array_len] = &input[input_position]; + input_position += BLAKE3_CHUNK_LEN; + chunks_array_len += 1; + } + + blake3_hash_many(chunks_array, chunks_array_len, + BLAKE3_CHUNK_LEN / BLAKE3_BLOCK_LEN, key, chunk_counter, + true, flags, CHUNK_START, CHUNK_END, out); + + // Hash the remaining partial chunk, if there is one. Note that the empty + // chunk (meaning the empty message) is a different codepath. + if (input_len > input_position) { + uint64_t counter = chunk_counter + (uint64_t)chunks_array_len; + blake3_chunk_state chunk_state; + chunk_state_init(&chunk_state, key, flags); + chunk_state.chunk_counter = counter; + chunk_state_update(&chunk_state, &input[input_position], + input_len - input_position); + output_t output = chunk_state_output(&chunk_state); + output_chaining_value(&output, &out[chunks_array_len * BLAKE3_OUT_LEN]); + return chunks_array_len + 1; + } else { + return chunks_array_len; + } +} + +// Use SIMD parallelism to hash up to MAX_SIMD_DEGREE parents at the same time +// on a single thread. Write out the parent chaining values and return the +// number of parents hashed. (If there's an odd input chaining value left over, +// return it as an additional output.) These parents are never the root and +// never empty; those cases use a different codepath. +INLINE size_t compress_parents_parallel(const uint8_t *child_chaining_values, + size_t num_chaining_values, + const uint32_t key[8], uint8_t flags, + uint8_t *out) { +#if defined(BLAKE3_TESTING) + assert(2 <= num_chaining_values); + assert(num_chaining_values <= 2 * MAX_SIMD_DEGREE_OR_2); +#endif + + const uint8_t *parents_array[MAX_SIMD_DEGREE_OR_2]; + size_t parents_array_len = 0; + while (num_chaining_values - (2 * parents_array_len) >= 2) { + parents_array[parents_array_len] = + &child_chaining_values[2 * parents_array_len * BLAKE3_OUT_LEN]; + parents_array_len += 1; + } + + blake3_hash_many(parents_array, parents_array_len, 1, key, + 0, // Parents always use counter 0. + false, flags | PARENT, + 0, // Parents have no start flags. + 0, // Parents have no end flags. + out); + + // If there's an odd child left over, it becomes an output. + if (num_chaining_values > 2 * parents_array_len) { + memcpy(&out[parents_array_len * BLAKE3_OUT_LEN], + &child_chaining_values[2 * parents_array_len * BLAKE3_OUT_LEN], + BLAKE3_OUT_LEN); + return parents_array_len + 1; + } else { + return parents_array_len; + } +} + +// The wide helper function returns (writes out) an array of chaining values +// and returns the length of that array. The number of chaining values returned +// is the dyanmically detected SIMD degree, at most MAX_SIMD_DEGREE. Or fewer, +// if the input is shorter than that many chunks. The reason for maintaining a +// wide array of chaining values going back up the tree, is to allow the +// implementation to hash as many parents in parallel as possible. +// +// As a special case when the SIMD degree is 1, this function will still return +// at least 2 outputs. This guarantees that this function doesn't perform the +// root compression. (If it did, it would use the wrong flags, and also we +// wouldn't be able to implement exendable ouput.) Note that this function is +// not used when the whole input is only 1 chunk long; that's a different +// codepath. +// +// Why not just have the caller split the input on the first update(), instead +// of implementing this special rule? Because we don't want to limit SIMD or +// multi-threading parallelism for that update(). +static size_t blake3_compress_subtree_wide(const uint8_t *input, + size_t input_len, + const uint32_t key[8], + uint64_t chunk_counter, + uint8_t flags, uint8_t *out) { + // Note that the single chunk case does *not* bump the SIMD degree up to 2 + // when it is 1. If this implementation adds multi-threading in the future, + // this gives us the option of multi-threading even the 2-chunk case, which + // can help performance on smaller platforms. + if (input_len <= blake3_simd_degree() * BLAKE3_CHUNK_LEN) { + return compress_chunks_parallel(input, input_len, key, chunk_counter, flags, + out); + } + + // With more than simd_degree chunks, we need to recurse. Start by dividing + // the input into left and right subtrees. (Note that this is only optimal + // as long as the SIMD degree is a power of 2. If we ever get a SIMD degree + // of 3 or something, we'll need a more complicated strategy.) + size_t left_input_len = left_len(input_len); + size_t right_input_len = input_len - left_input_len; + const uint8_t *right_input = &input[left_input_len]; + uint64_t right_chunk_counter = + chunk_counter + (uint64_t)(left_input_len / BLAKE3_CHUNK_LEN); + + // Make space for the child outputs. Here we use MAX_SIMD_DEGREE_OR_2 to + // account for the special case of returning 2 outputs when the SIMD degree + // is 1. + uint8_t cv_array[2 * MAX_SIMD_DEGREE_OR_2 * BLAKE3_OUT_LEN]; + size_t degree = blake3_simd_degree(); + if (left_input_len > BLAKE3_CHUNK_LEN && degree == 1) { + // The special case: We always use a degree of at least two, to make + // sure there are two outputs. Except, as noted above, at the chunk + // level, where we allow degree=1. (Note that the 1-chunk-input case is + // a different codepath.) + degree = 2; + } + uint8_t *right_cvs = &cv_array[degree * BLAKE3_OUT_LEN]; + + // Recurse! If this implementation adds multi-threading support in the + // future, this is where it will go. + size_t left_n = blake3_compress_subtree_wide(input, left_input_len, key, + chunk_counter, flags, cv_array); + size_t right_n = blake3_compress_subtree_wide( + right_input, right_input_len, key, right_chunk_counter, flags, right_cvs); + + // The special case again. If simd_degree=1, then we'll have left_n=1 and + // right_n=1. Rather than compressing them into a single output, return + // them directly, to make sure we always have at least two outputs. + if (left_n == 1) { + memcpy(out, cv_array, 2 * BLAKE3_OUT_LEN); + return 2; + } + + // Otherwise, do one layer of parent node compression. + size_t num_chaining_values = left_n + right_n; + return compress_parents_parallel(cv_array, num_chaining_values, key, flags, + out); +} + +// Hash a subtree with compress_subtree_wide(), and then condense the resulting +// list of chaining values down to a single parent node. Don't compress that +// last parent node, however. Instead, return its message bytes (the +// concatenated chaining values of its children). This is necessary when the +// first call to update() supplies a complete subtree, because the topmost +// parent node of that subtree could end up being the root. It's also necessary +// for extended output in the general case. +// +// As with compress_subtree_wide(), this function is not used on inputs of 1 +// chunk or less. That's a different codepath. +INLINE void compress_subtree_to_parent_node( + const uint8_t *input, size_t input_len, const uint32_t key[8], + uint64_t chunk_counter, uint8_t flags, uint8_t out[2 * BLAKE3_OUT_LEN]) { +#if defined(BLAKE3_TESTING) + assert(input_len > BLAKE3_CHUNK_LEN); +#endif + + uint8_t cv_array[2 * MAX_SIMD_DEGREE_OR_2 * BLAKE3_OUT_LEN]; + size_t num_cvs = blake3_compress_subtree_wide(input, input_len, key, + chunk_counter, flags, cv_array); + + // If MAX_SIMD_DEGREE is greater than 2 and there's enough input, + // compress_subtree_wide() returns more than 2 chaining values. Condense + // them into 2 by forming parent nodes repeatedly. + uint8_t out_array[MAX_SIMD_DEGREE_OR_2 * BLAKE3_OUT_LEN / 2]; + while (num_cvs > 2) { + num_cvs = + compress_parents_parallel(cv_array, num_cvs, key, flags, out_array); + memcpy(cv_array, out_array, num_cvs * BLAKE3_OUT_LEN); + } + memcpy(out, cv_array, 2 * BLAKE3_OUT_LEN); +} + +INLINE void hasher_init_base(blake3_hasher *self, const uint32_t key[8], + uint8_t flags) { + memcpy(self->key, key, BLAKE3_KEY_LEN); + chunk_state_init(&self->chunk, key, flags); + self->cv_stack_len = 0; +} + +void blake3_hasher_init(blake3_hasher *self) { hasher_init_base(self, IV, 0); } + +void blake3_hasher_init_keyed(blake3_hasher *self, + const uint8_t key[BLAKE3_KEY_LEN]) { + uint32_t key_words[8]; + load_key_words(key, key_words); + hasher_init_base(self, key_words, KEYED_HASH); +} + +void blake3_hasher_init_derive_key(blake3_hasher *self, const char *context) { + blake3_hasher context_hasher; + hasher_init_base(&context_hasher, IV, DERIVE_KEY_CONTEXT); + blake3_hasher_update(&context_hasher, context, strlen(context)); + uint8_t context_key[BLAKE3_KEY_LEN]; + blake3_hasher_finalize(&context_hasher, context_key, BLAKE3_KEY_LEN); + uint32_t context_key_words[8]; + load_key_words(context_key, context_key_words); + hasher_init_base(self, context_key_words, DERIVE_KEY_MATERIAL); +} + +// As described in hasher_push_cv() below, we do "lazy merging", delaying +// merges until right before the next CV is about to be added. This is +// different from the reference implementation. Another difference is that we +// aren't always merging 1 chunk at a time. Instead, each CV might represent +// any power-of-two number of chunks, as long as the smaller-above-larger stack +// order is maintained. Instead of the "count the trailing 0-bits" algorithm +// described in the spec, we use a "count the total number of 1-bits" variant +// that doesn't require us to retain the subtree size of the CV on top of the +// stack. The principle is the same: each CV that should remain in the stack is +// represented by a 1-bit in the total number of chunks (or bytes) so far. +INLINE void hasher_merge_cv_stack(blake3_hasher *self, uint64_t total_len) { + size_t post_merge_stack_len = (size_t)popcnt(total_len); + while (self->cv_stack_len > post_merge_stack_len) { + uint8_t *parent_node = + &self->cv_stack[(self->cv_stack_len - 2) * BLAKE3_OUT_LEN]; + output_t output = parent_output(parent_node, self->key, self->chunk.flags); + output_chaining_value(&output, parent_node); + self->cv_stack_len -= 1; + } +} + +// In reference_impl.rs, we merge the new CV with existing CVs from the stack +// before pushing it. We can do that because we know more input is coming, so +// we know none of the merges are root. +// +// This setting is different. We want to feed as much input as possible to +// compress_subtree_wide(), without setting aside anything for the chunk_state. +// If the user gives us 64 KiB, we want to parallelize over all 64 KiB at once +// as a single subtree, if at all possible. +// +// This leads to two problems: +// 1) This 64 KiB input might be the only call that ever gets made to update. +// In this case, the root node of the 64 KiB subtree would be the root node +// of the whole tree, and it would need to be ROOT finalized. We can't +// compress it until we know. +// 2) This 64 KiB input might complete a larger tree, whose root node is +// similarly going to be the the root of the whole tree. For example, maybe +// we have 196 KiB (that is, 128 + 64) hashed so far. We can't compress the +// node at the root of the 256 KiB subtree until we know how to finalize it. +// +// The second problem is solved with "lazy merging". That is, when we're about +// to add a CV to the stack, we don't merge it with anything first, as the +// reference impl does. Instead we do merges using the *previous* CV that was +// added, which is sitting on top of the stack, and we put the new CV +// (unmerged) on top of the stack afterwards. This guarantees that we never +// merge the root node until finalize(). +// +// Solving the first problem requires an additional tool, +// compress_subtree_to_parent_node(). That function always returns the top +// *two* chaining values of the subtree it's compressing. We then do lazy +// merging with each of them separately, so that the second CV will always +// remain unmerged. (That also helps us support extendable output when we're +// hashing an input all-at-once.) +INLINE void hasher_push_cv(blake3_hasher *self, uint8_t new_cv[BLAKE3_OUT_LEN], + uint64_t chunk_counter) { + hasher_merge_cv_stack(self, chunk_counter); + memcpy(&self->cv_stack[self->cv_stack_len * BLAKE3_OUT_LEN], new_cv, + BLAKE3_OUT_LEN); + self->cv_stack_len += 1; +} + +void blake3_hasher_update(blake3_hasher *self, const void *input, + size_t input_len) { + // Explicitly checking for zero avoids causing UB by passing a null pointer + // to memcpy. This comes up in practice with things like: + // std::vector v; + // blake3_hasher_update(&hasher, v.data(), v.size()); + if (input_len == 0) { + return; + } + + const uint8_t *input_bytes = (const uint8_t *)input; + + // If we have some partial chunk bytes in the internal chunk_state, we need + // to finish that chunk first. + if (chunk_state_len(&self->chunk) > 0) { + size_t take = BLAKE3_CHUNK_LEN - chunk_state_len(&self->chunk); + if (take > input_len) { + take = input_len; + } + chunk_state_update(&self->chunk, input_bytes, take); + input_bytes += take; + input_len -= take; + // If we've filled the current chunk and there's more coming, finalize this + // chunk and proceed. In this case we know it's not the root. + if (input_len > 0) { + output_t output = chunk_state_output(&self->chunk); + uint8_t chunk_cv[32]; + output_chaining_value(&output, chunk_cv); + hasher_push_cv(self, chunk_cv, self->chunk.chunk_counter); + chunk_state_reset(&self->chunk, self->key, self->chunk.chunk_counter + 1); + } else { + return; + } + } + + // Now the chunk_state is clear, and we have more input. If there's more than + // a single chunk (so, definitely not the root chunk), hash the largest whole + // subtree we can, with the full benefits of SIMD (and maybe in the future, + // multi-threading) parallelism. Two restrictions: + // - The subtree has to be a power-of-2 number of chunks. Only subtrees along + // the right edge can be incomplete, and we don't know where the right edge + // is going to be until we get to finalize(). + // - The subtree must evenly divide the total number of chunks up until this + // point (if total is not 0). If the current incomplete subtree is only + // waiting for 1 more chunk, we can't hash a subtree of 4 chunks. We have + // to complete the current subtree first. + // Because we might need to break up the input to form powers of 2, or to + // evenly divide what we already have, this part runs in a loop. + while (input_len > BLAKE3_CHUNK_LEN) { + size_t subtree_len = round_down_to_power_of_2(input_len); + uint64_t count_so_far = self->chunk.chunk_counter * BLAKE3_CHUNK_LEN; + // Shrink the subtree_len until it evenly divides the count so far. We know + // that subtree_len itself is a power of 2, so we can use a bitmasking + // trick instead of an actual remainder operation. (Note that if the caller + // consistently passes power-of-2 inputs of the same size, as is hopefully + // typical, this loop condition will always fail, and subtree_len will + // always be the full length of the input.) + // + // An aside: We don't have to shrink subtree_len quite this much. For + // example, if count_so_far is 1, we could pass 2 chunks to + // compress_subtree_to_parent_node. Since we'll get 2 CVs back, we'll still + // get the right answer in the end, and we might get to use 2-way SIMD + // parallelism. The problem with this optimization, is that it gets us + // stuck always hashing 2 chunks. The total number of chunks will remain + // odd, and we'll never graduate to higher degrees of parallelism. See + // https://github.com/BLAKE3-team/BLAKE3/issues/69. + while ((((uint64_t)(subtree_len - 1)) & count_so_far) != 0) { + subtree_len /= 2; + } + // The shrunken subtree_len might now be 1 chunk long. If so, hash that one + // chunk by itself. Otherwise, compress the subtree into a pair of CVs. + uint64_t subtree_chunks = subtree_len / BLAKE3_CHUNK_LEN; + if (subtree_len <= BLAKE3_CHUNK_LEN) { + blake3_chunk_state chunk_state; + chunk_state_init(&chunk_state, self->key, self->chunk.flags); + chunk_state.chunk_counter = self->chunk.chunk_counter; + chunk_state_update(&chunk_state, input_bytes, subtree_len); + output_t output = chunk_state_output(&chunk_state); + uint8_t cv[BLAKE3_OUT_LEN]; + output_chaining_value(&output, cv); + hasher_push_cv(self, cv, chunk_state.chunk_counter); + } else { + // This is the high-performance happy path, though getting here depends + // on the caller giving us a long enough input. + uint8_t cv_pair[2 * BLAKE3_OUT_LEN]; + compress_subtree_to_parent_node(input_bytes, subtree_len, self->key, + self->chunk.chunk_counter, + self->chunk.flags, cv_pair); + hasher_push_cv(self, cv_pair, self->chunk.chunk_counter); + hasher_push_cv(self, &cv_pair[BLAKE3_OUT_LEN], + self->chunk.chunk_counter + (subtree_chunks / 2)); + } + self->chunk.chunk_counter += subtree_chunks; + input_bytes += subtree_len; + input_len -= subtree_len; + } + + // If there's any remaining input less than a full chunk, add it to the chunk + // state. In that case, also do a final merge loop to make sure the subtree + // stack doesn't contain any unmerged pairs. The remaining input means we + // know these merges are non-root. This merge loop isn't strictly necessary + // here, because hasher_push_chunk_cv already does its own merge loop, but it + // simplifies blake3_hasher_finalize below. + if (input_len > 0) { + chunk_state_update(&self->chunk, input_bytes, input_len); + hasher_merge_cv_stack(self, self->chunk.chunk_counter); + } +} + +void blake3_hasher_finalize(const blake3_hasher *self, uint8_t *out, + size_t out_len) { + blake3_hasher_finalize_seek(self, 0, out, out_len); +} + +void blake3_hasher_finalize_seek(const blake3_hasher *self, uint64_t seek, + uint8_t *out, size_t out_len) { + // Explicitly checking for zero avoids causing UB by passing a null pointer + // to memcpy. This comes up in practice with things like: + // std::vector v; + // blake3_hasher_finalize(&hasher, v.data(), v.size()); + if (out_len == 0) { + return; + } + + // If the subtree stack is empty, then the current chunk is the root. + if (self->cv_stack_len == 0) { + output_t output = chunk_state_output(&self->chunk); + output_root_bytes(&output, seek, out, out_len); + return; + } + // If there are any bytes in the chunk state, finalize that chunk and do a + // roll-up merge between that chunk hash and every subtree in the stack. In + // this case, the extra merge loop at the end of blake3_hasher_update + // guarantees that none of the subtrees in the stack need to be merged with + // each other first. Otherwise, if there are no bytes in the chunk state, + // then the top of the stack is a chunk hash, and we start the merge from + // that. + output_t output; + size_t cvs_remaining; + if (chunk_state_len(&self->chunk) > 0) { + cvs_remaining = self->cv_stack_len; + output = chunk_state_output(&self->chunk); + } else { + // There are always at least 2 CVs in the stack in this case. + cvs_remaining = self->cv_stack_len - 2; + output = parent_output(&self->cv_stack[cvs_remaining * 32], self->key, + self->chunk.flags); + } + while (cvs_remaining > 0) { + cvs_remaining -= 1; + uint8_t parent_block[BLAKE3_BLOCK_LEN]; + memcpy(parent_block, &self->cv_stack[cvs_remaining * 32], 32); + output_chaining_value(&output, &parent_block[32]); + output = parent_output(parent_block, self->key, self->chunk.flags); + } + output_root_bytes(&output, seek, out, out_len); +} diff -Nru r-cran-digest-0.6.25/src/blake3_dispatch.c r-cran-digest-0.6.27/src/blake3_dispatch.c --- r-cran-digest-0.6.25/src/blake3_dispatch.c 1970-01-01 00:00:00.000000000 +0000 +++ r-cran-digest-0.6.27/src/blake3_dispatch.c 2020-10-20 13:02:56.000000000 +0000 @@ -0,0 +1,261 @@ +/* blake3 license: + * The C code is copyright Samuel Neves and Jack O'Connor, 2019-2020. + * This work is released into the public domain with CC0 1.0. + * Alternatively, it is licensed under the Apache License 2.0. + * For full license see + * https://raw.githubusercontent.com/BLAKE3-team/BLAKE3/master/LICENSE + */ + +#include +#include +#include + +#include "blake3_impl.h" + +// Commenting out this block as intrinic headers are not need or used +// and the 'non-MSC and non-GNU' leads to an error on Solaris +// #if defined(IS_X86) +// #if defined(_MSC_VER) +// #include +// #elif defined(__GNUC__) +// #include +// #else +// #error "Unimplemented!" +// #endif +// #endif + +// This function is disabled in this R package for now +// #if defined(IS_X86) +// static uint64_t xgetbv() { +// #if defined(_MSC_VER) +// return _xgetbv(0); +// #else +// uint32_t eax = 0, edx = 0; +// __asm__ __volatile__("xgetbv\n" : "=a"(eax), "=d"(edx) : "c"(0)); +// return ((uint64_t)edx << 32) | eax; +// #endif +// } +// +// static void cpuid(uint32_t out[4], uint32_t id) { +// #if defined(_MSC_VER) +// __cpuid((int *)out, id); +// #elif defined(__i386__) || defined(_M_IX86) +// __asm__ __volatile__("movl %%ebx, %1\n" +// "cpuid\n" +// "xchgl %1, %%ebx\n" +// : "=a"(out[0]), "=r"(out[1]), "=c"(out[2]), "=d"(out[3]) +// : "a"(id)); +// #else +// __asm__ __volatile__("cpuid\n" +// : "=a"(out[0]), "=b"(out[1]), "=c"(out[2]), "=d"(out[3]) +// : "a"(id)); +// #endif +// } +// +// static void cpuidex(uint32_t out[4], uint32_t id, uint32_t sid) { +// #if defined(_MSC_VER) +// __cpuidex((int *)out, id, sid); +// #elif defined(__i386__) || defined(_M_IX86) +// __asm__ __volatile__("movl %%ebx, %1\n" +// "cpuid\n" +// "xchgl %1, %%ebx\n" +// : "=a"(out[0]), "=r"(out[1]), "=c"(out[2]), "=d"(out[3]) +// : "a"(id), "c"(sid)); +// #else +// __asm__ __volatile__("cpuid\n" +// : "=a"(out[0]), "=b"(out[1]), "=c"(out[2]), "=d"(out[3]) +// : "a"(id), "c"(sid)); +// #endif +// } +// +// #endif + +enum cpu_feature { + SSE2 = 1 << 0, + SSSE3 = 1 << 1, + SSE41 = 1 << 2, + AVX = 1 << 3, + AVX2 = 1 << 4, + AVX512F = 1 << 5, + AVX512VL = 1 << 6, + /* ... */ + UNDEFINED = 1 << 30 +}; + +// This function is disabled in this R package for now +// #if !defined(BLAKE3_TESTING) +// static /* Allow the variable to be controlled manually for testing */ +// #endif +// enum cpu_feature g_cpu_features = UNDEFINED; +// +// #if !defined(BLAKE3_TESTING) +// static +// #endif +// enum cpu_feature +// get_cpu_features() { +// +// if (g_cpu_features != UNDEFINED) { +// return g_cpu_features; +// } else { +// #if defined(IS_X86) +// uint32_t regs[4] = {0}; +// uint32_t *eax = ®s[0], *ebx = ®s[1], *ecx = ®s[2], *edx = ®s[3]; +// (void)edx; +// enum cpu_feature features = 0; +// cpuid(regs, 0); +// const int max_id = *eax; +// cpuid(regs, 1); +// #if defined(__amd64__) || defined(_M_X64) +// features |= SSE2; +// #else +// if (*edx & (1UL << 26)) +// features |= SSE2; +// #endif +// if (*ecx & (1UL << 0)) +// features |= SSSE3; +// if (*ecx & (1UL << 19)) +// features |= SSE41; +// +// if (*ecx & (1UL << 27)) { // OSXSAVE +// const uint64_t mask = xgetbv(); +// if ((mask & 6) == 6) { // SSE and AVX states +// if (*ecx & (1UL << 28)) +// features |= AVX; +// if (max_id >= 7) { +// cpuidex(regs, 7, 0); +// if (*ebx & (1UL << 5)) +// features |= AVX2; +// if ((mask & 224) == 224) { // Opmask, ZMM_Hi256, Hi16_Zmm +// if (*ebx & (1UL << 31)) +// features |= AVX512VL; +// if (*ebx & (1UL << 16)) +// features |= AVX512F; +// } +// } +// } +// } +// g_cpu_features = features; +// return features; +// #else +// /* How to detect NEON? */ +// return 0; +// #endif +// } +// } + +void blake3_compress_in_place(uint32_t cv[8], + const uint8_t block[BLAKE3_BLOCK_LEN], + uint8_t block_len, uint64_t counter, + uint8_t flags) { +#if defined(IS_X86) + // const enum cpu_feature features = get_cpu_features(); + // never true for this R package +#if !defined(BLAKE3_NO_AVX512) + if (features & AVX512VL) { + blake3_compress_in_place_avx512(cv, block, block_len, counter, flags); + return; + } +#endif +#if !defined(BLAKE3_NO_SSE41) + if (features & SSE41) { + blake3_compress_in_place_sse41(cv, block, block_len, counter, flags); + return; + } +#endif +#endif + blake3_compress_in_place_portable(cv, block, block_len, counter, flags); +} + +void blake3_compress_xof(const uint32_t cv[8], + const uint8_t block[BLAKE3_BLOCK_LEN], + uint8_t block_len, uint64_t counter, uint8_t flags, + uint8_t out[64]) { +#if defined(IS_X86) + // const enum cpu_feature features = get_cpu_features(); + // never true for this R package +#if !defined(BLAKE3_NO_AVX512) + if (features & AVX512VL) { + blake3_compress_xof_avx512(cv, block, block_len, counter, flags, out); + return; + } +#endif +#if !defined(BLAKE3_NO_SSE41) + if (features & SSE41) { + blake3_compress_xof_sse41(cv, block, block_len, counter, flags, out); + return; + } +#endif +#endif + blake3_compress_xof_portable(cv, block, block_len, counter, flags, out); +} + +void blake3_hash_many(const uint8_t *const *inputs, size_t num_inputs, + size_t blocks, const uint32_t key[8], uint64_t counter, + bool increment_counter, uint8_t flags, + uint8_t flags_start, uint8_t flags_end, uint8_t *out) { +#if defined(IS_X86) + // const enum cpu_feature features = get_cpu_features(); + // never true for this R package +#if !defined(BLAKE3_NO_AVX512) + if ((features & (AVX512F|AVX512VL)) == (AVX512F|AVX512VL)) { + blake3_hash_many_avx512(inputs, num_inputs, blocks, key, counter, + increment_counter, flags, flags_start, flags_end, + out); + return; + } +#endif +#if !defined(BLAKE3_NO_AVX2) + if (features & AVX2) { + blake3_hash_many_avx2(inputs, num_inputs, blocks, key, counter, + increment_counter, flags, flags_start, flags_end, + out); + return; + } +#endif +#if !defined(BLAKE3_NO_SSE41) + if (features & SSE41) { + blake3_hash_many_sse41(inputs, num_inputs, blocks, key, counter, + increment_counter, flags, flags_start, flags_end, + out); + return; + } +#endif +#endif + +#if defined(BLAKE3_USE_NEON) + blake3_hash_many_neon(inputs, num_inputs, blocks, key, counter, + increment_counter, flags, flags_start, flags_end, out); + return; +#endif + + blake3_hash_many_portable(inputs, num_inputs, blocks, key, counter, + increment_counter, flags, flags_start, flags_end, + out); +} + +// The dynamically detected SIMD degree of the current platform. +size_t blake3_simd_degree(void) { +#if defined(IS_X86) + // const enum cpu_feature features = get_cpu_features(); + // never true for this R package +#if !defined(BLAKE3_NO_AVX512) + if ((features & (AVX512F|AVX512VL)) == (AVX512F|AVX512VL)) { + return 16; + } +#endif +#if !defined(BLAKE3_NO_AVX2) + if (features & AVX2) { + return 8; + } +#endif +#if !defined(BLAKE3_NO_SSE41) + if (features & SSE41) { + return 4; + } +#endif +#endif +#if defined(BLAKE3_USE_NEON) + return 4; +#endif + return 1; +} diff -Nru r-cran-digest-0.6.25/src/blake3.h r-cran-digest-0.6.27/src/blake3.h --- r-cran-digest-0.6.25/src/blake3.h 1970-01-01 00:00:00.000000000 +0000 +++ r-cran-digest-0.6.27/src/blake3.h 2020-05-20 11:42:23.000000000 +0000 @@ -0,0 +1,68 @@ +/* blake3 license: + * The C code is copyright Samuel Neves and Jack O'Connor, 2019-2020. + * This work is released into the public domain with CC0 1.0. + * Alternatively, it is licensed under the Apache License 2.0. + * For full license see + * https://raw.githubusercontent.com/BLAKE3-team/BLAKE3/master/LICENSE + */ + +#ifndef BLAKE3_H +#define BLAKE3_H + +#include +#include + +#define BLAKE3_NO_AVX512 +#define BLAKE3_NO_SSE41 +#define BLAKE3_NO_AVX2 + +#ifdef __cplusplus +extern "C" { +#endif + +#define BLAKE3_KEY_LEN 32 +#define BLAKE3_OUT_LEN 32 +#define BLAKE3_BLOCK_LEN 64 +#define BLAKE3_CHUNK_LEN 1024 +#define BLAKE3_MAX_DEPTH 54 +#define BLAKE3_MAX_SIMD_DEGREE 16 + +// This struct is a private implementation detail. It has to be here because +// it's part of blake3_hasher below. +typedef struct { + uint32_t cv[8]; + uint64_t chunk_counter; + uint8_t buf[BLAKE3_BLOCK_LEN]; + uint8_t buf_len; + uint8_t blocks_compressed; + uint8_t flags; +} blake3_chunk_state; + +typedef struct { + uint32_t key[8]; + blake3_chunk_state chunk; + uint8_t cv_stack_len; + // The stack size is MAX_DEPTH + 1 because we do lazy merging. For example, + // with 7 chunks, we have 3 entries in the stack. Adding an 8th chunk + // requires a 4th entry, rather than merging everything down to 1, because we + // don't know whether more input is coming. This is different from how the + // reference implementation does things. + uint8_t cv_stack[(BLAKE3_MAX_DEPTH + 1) * BLAKE3_OUT_LEN]; +} blake3_hasher; + +void blake3_hasher_init(blake3_hasher *self); +void blake3_hasher_init_keyed(blake3_hasher *self, + const uint8_t key[BLAKE3_KEY_LEN]); +void blake3_hasher_init_derive_key(blake3_hasher *self, const char *context); +void blake3_hasher_update(blake3_hasher *self, const void *input, + size_t input_len); +void blake3_hasher_finalize(const blake3_hasher *self, uint8_t *out, + size_t out_len); +void blake3_hasher_finalize_seek(const blake3_hasher *self, uint64_t seek, + uint8_t *out, size_t out_len); + +#ifdef __cplusplus +} +#endif + +#endif /* BLAKE3_H */ diff -Nru r-cran-digest-0.6.25/src/blake3_impl.h r-cran-digest-0.6.27/src/blake3_impl.h --- r-cran-digest-0.6.25/src/blake3_impl.h 1970-01-01 00:00:00.000000000 +0000 +++ r-cran-digest-0.6.27/src/blake3_impl.h 2020-05-20 11:42:23.000000000 +0000 @@ -0,0 +1,243 @@ +/* blake3 license: + * The C code is copyright Samuel Neves and Jack O'Connor, 2019-2020. + * This work is released into the public domain with CC0 1.0. + * Alternatively, it is licensed under the Apache License 2.0. + * For full license see + * https://raw.githubusercontent.com/BLAKE3-team/BLAKE3/master/LICENSE + */ + +#ifndef BLAKE3_IMPL_H +#define BLAKE3_IMPL_H + +#include +#include +#include +#include +#include + +#include "blake3.h" + +// internal flags +enum blake3_flags { + CHUNK_START = 1 << 0, + CHUNK_END = 1 << 1, + PARENT = 1 << 2, + ROOT = 1 << 3, + KEYED_HASH = 1 << 4, + DERIVE_KEY_CONTEXT = 1 << 5, + DERIVE_KEY_MATERIAL = 1 << 6, +}; + +// This C implementation tries to support recent versions of GCC, Clang, and +// MSVC. +#if defined(_MSC_VER) +#define INLINE static __forceinline +#else +#define INLINE static inline __attribute__((always_inline)) +#endif + +#if defined(__x86_64__) || defined(_M_X64) +#define IS_X86 +#define IS_X86_64 +#endif + +#if defined(__i386__) || defined(_M_IX86) +#define IS_X86 +#define IS_X86_32 +#endif + +#if defined(IS_X86) +#if defined(_MSC_VER) +#include +#endif +#include +#endif + +#if defined(IS_X86) +#define MAX_SIMD_DEGREE 16 +#elif defined(BLAKE3_USE_NEON) +#define MAX_SIMD_DEGREE 4 +#else +#define MAX_SIMD_DEGREE 1 +#endif + +// There are some places where we want a static size that's equal to the +// MAX_SIMD_DEGREE, but also at least 2. +#define MAX_SIMD_DEGREE_OR_2 (MAX_SIMD_DEGREE > 2 ? MAX_SIMD_DEGREE : 2) + +static const uint32_t IV[8] = {0x6A09E667UL, 0xBB67AE85UL, 0x3C6EF372UL, + 0xA54FF53AUL, 0x510E527FUL, 0x9B05688CUL, + 0x1F83D9ABUL, 0x5BE0CD19UL}; + +static const uint8_t MSG_SCHEDULE[7][16] = { + {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}, + {2, 6, 3, 10, 7, 0, 4, 13, 1, 11, 12, 5, 9, 14, 15, 8}, + {3, 4, 10, 12, 13, 2, 7, 14, 6, 5, 9, 0, 11, 15, 8, 1}, + {10, 7, 12, 9, 14, 3, 13, 15, 4, 0, 11, 2, 5, 8, 1, 6}, + {12, 13, 9, 11, 15, 10, 14, 8, 7, 2, 5, 3, 0, 1, 6, 4}, + {9, 14, 11, 5, 8, 12, 15, 1, 13, 3, 0, 10, 2, 6, 4, 7}, + {11, 15, 5, 0, 1, 9, 8, 6, 14, 10, 2, 12, 3, 4, 7, 13}, +}; + +/* Find index of the highest set bit */ +/* x is assumed to be nonzero. */ +static unsigned int highest_one(uint64_t x) { +#if defined(__GNUC__) || defined(__clang__) + return 63 ^ __builtin_clzll(x); +#elif defined(_MSC_VER) && defined(IS_X86_64) + unsigned long index; + _BitScanReverse64(&index, x); + return index; +#elif defined(_MSC_VER) && defined(IS_X86_32) + if(x >> 32) { + unsigned long index; + _BitScanReverse(&index, x >> 32); + return 32 + index; + } else { + unsigned long index; + _BitScanReverse(&index, x); + return index; + } +#else + unsigned int c = 0; + if(x & 0xffffffff00000000ULL) { x >>= 32; c += 32; } + if(x & 0x00000000ffff0000ULL) { x >>= 16; c += 16; } + if(x & 0x000000000000ff00ULL) { x >>= 8; c += 8; } + if(x & 0x00000000000000f0ULL) { x >>= 4; c += 4; } + if(x & 0x000000000000000cULL) { x >>= 2; c += 2; } + if(x & 0x0000000000000002ULL) { c += 1; } + return c; +#endif +} + +// Count the number of 1 bits. +INLINE unsigned int popcnt(uint64_t x) { +#if defined(__GNUC__) || defined(__clang__) + return __builtin_popcountll(x); +#else + unsigned int count = 0; + while (x != 0) { + count += 1; + x &= x - 1; + } + return count; +#endif +} + +// Largest power of two less than or equal to x. As a special case, returns 1 +// when x is 0. +INLINE uint64_t round_down_to_power_of_2(uint64_t x) { + return 1ULL << highest_one(x | 1); +} + +INLINE uint32_t counter_low(uint64_t counter) { return (uint32_t)counter; } + +INLINE uint32_t counter_high(uint64_t counter) { + return (uint32_t)(counter >> 32); +} + +INLINE uint32_t load32(const void *src) { + const uint8_t *p = (const uint8_t *)src; + return ((uint32_t)(p[0]) << 0) | ((uint32_t)(p[1]) << 8) | + ((uint32_t)(p[2]) << 16) | ((uint32_t)(p[3]) << 24); +} + +INLINE void load_key_words(const uint8_t key[BLAKE3_KEY_LEN], + uint32_t key_words[8]) { + key_words[0] = load32(&key[0 * 4]); + key_words[1] = load32(&key[1 * 4]); + key_words[2] = load32(&key[2 * 4]); + key_words[3] = load32(&key[3 * 4]); + key_words[4] = load32(&key[4 * 4]); + key_words[5] = load32(&key[5 * 4]); + key_words[6] = load32(&key[6 * 4]); + key_words[7] = load32(&key[7 * 4]); +} + +void blake3_compress_in_place(uint32_t cv[8], + const uint8_t block[BLAKE3_BLOCK_LEN], + uint8_t block_len, uint64_t counter, + uint8_t flags); + +void blake3_compress_xof(const uint32_t cv[8], + const uint8_t block[BLAKE3_BLOCK_LEN], + uint8_t block_len, uint64_t counter, uint8_t flags, + uint8_t out[64]); + +void blake3_hash_many(const uint8_t *const *inputs, size_t num_inputs, + size_t blocks, const uint32_t key[8], uint64_t counter, + bool increment_counter, uint8_t flags, + uint8_t flags_start, uint8_t flags_end, uint8_t *out); + +size_t blake3_simd_degree(void); + + +// Declarations for implementation-specific functions. +void blake3_compress_in_place_portable(uint32_t cv[8], + const uint8_t block[BLAKE3_BLOCK_LEN], + uint8_t block_len, uint64_t counter, + uint8_t flags); + +void blake3_compress_xof_portable(const uint32_t cv[8], + const uint8_t block[BLAKE3_BLOCK_LEN], + uint8_t block_len, uint64_t counter, + uint8_t flags, uint8_t out[64]); + +void blake3_hash_many_portable(const uint8_t *const *inputs, size_t num_inputs, + size_t blocks, const uint32_t key[8], + uint64_t counter, bool increment_counter, + uint8_t flags, uint8_t flags_start, + uint8_t flags_end, uint8_t *out); + +#if defined(IS_X86) +#if !defined(BLAKE3_NO_SSE41) +void blake3_compress_in_place_sse41(uint32_t cv[8], + const uint8_t block[BLAKE3_BLOCK_LEN], + uint8_t block_len, uint64_t counter, + uint8_t flags); +void blake3_compress_xof_sse41(const uint32_t cv[8], + const uint8_t block[BLAKE3_BLOCK_LEN], + uint8_t block_len, uint64_t counter, + uint8_t flags, uint8_t out[64]); +void blake3_hash_many_sse41(const uint8_t *const *inputs, size_t num_inputs, + size_t blocks, const uint32_t key[8], + uint64_t counter, bool increment_counter, + uint8_t flags, uint8_t flags_start, + uint8_t flags_end, uint8_t *out); +#endif +#if !defined(BLAKE3_NO_AVX2) +void blake3_hash_many_avx2(const uint8_t *const *inputs, size_t num_inputs, + size_t blocks, const uint32_t key[8], + uint64_t counter, bool increment_counter, + uint8_t flags, uint8_t flags_start, + uint8_t flags_end, uint8_t *out); +#endif +#if !defined(BLAKE3_NO_AVX512) +void blake3_compress_in_place_avx512(uint32_t cv[8], + const uint8_t block[BLAKE3_BLOCK_LEN], + uint8_t block_len, uint64_t counter, + uint8_t flags); + +void blake3_compress_xof_avx512(const uint32_t cv[8], + const uint8_t block[BLAKE3_BLOCK_LEN], + uint8_t block_len, uint64_t counter, + uint8_t flags, uint8_t out[64]); + +void blake3_hash_many_avx512(const uint8_t *const *inputs, size_t num_inputs, + size_t blocks, const uint32_t key[8], + uint64_t counter, bool increment_counter, + uint8_t flags, uint8_t flags_start, + uint8_t flags_end, uint8_t *out); +#endif +#endif + +#if defined(BLAKE3_USE_NEON) +void blake3_hash_many_neon(const uint8_t *const *inputs, size_t num_inputs, + size_t blocks, const uint32_t key[8], + uint64_t counter, bool increment_counter, + uint8_t flags, uint8_t flags_start, + uint8_t flags_end, uint8_t *out); +#endif + + +#endif /* BLAKE3_IMPL_H */ diff -Nru r-cran-digest-0.6.25/src/blake3_portable.c r-cran-digest-0.6.27/src/blake3_portable.c --- r-cran-digest-0.6.25/src/blake3_portable.c 1970-01-01 00:00:00.000000000 +0000 +++ r-cran-digest-0.6.27/src/blake3_portable.c 2020-05-20 11:42:23.000000000 +0000 @@ -0,0 +1,176 @@ +/* blake3 license: + * The C code is copyright Samuel Neves and Jack O'Connor, 2019-2020. + * This work is released into the public domain with CC0 1.0. + * Alternatively, it is licensed under the Apache License 2.0. + * For full license see + * https://raw.githubusercontent.com/BLAKE3-team/BLAKE3/master/LICENSE + */ + +#include "blake3_impl.h" +#include + +INLINE void store32(void *dst, uint32_t w) { + uint8_t *p = (uint8_t *)dst; + p[0] = (uint8_t)(w >> 0); + p[1] = (uint8_t)(w >> 8); + p[2] = (uint8_t)(w >> 16); + p[3] = (uint8_t)(w >> 24); +} + +INLINE uint32_t rotr32(uint32_t w, uint32_t c) { + return (w >> c) | (w << (32 - c)); +} + +INLINE void g(uint32_t *state, size_t a, size_t b, size_t c, size_t d, + uint32_t x, uint32_t y) { + state[a] = state[a] + state[b] + x; + state[d] = rotr32(state[d] ^ state[a], 16); + state[c] = state[c] + state[d]; + state[b] = rotr32(state[b] ^ state[c], 12); + state[a] = state[a] + state[b] + y; + state[d] = rotr32(state[d] ^ state[a], 8); + state[c] = state[c] + state[d]; + state[b] = rotr32(state[b] ^ state[c], 7); +} + +INLINE void round_fn(uint32_t state[16], const uint32_t *msg, size_t round) { + // Select the message schedule based on the round. + const uint8_t *schedule = MSG_SCHEDULE[round]; + + // Mix the columns. + g(state, 0, 4, 8, 12, msg[schedule[0]], msg[schedule[1]]); + g(state, 1, 5, 9, 13, msg[schedule[2]], msg[schedule[3]]); + g(state, 2, 6, 10, 14, msg[schedule[4]], msg[schedule[5]]); + g(state, 3, 7, 11, 15, msg[schedule[6]], msg[schedule[7]]); + + // Mix the rows. + g(state, 0, 5, 10, 15, msg[schedule[8]], msg[schedule[9]]); + g(state, 1, 6, 11, 12, msg[schedule[10]], msg[schedule[11]]); + g(state, 2, 7, 8, 13, msg[schedule[12]], msg[schedule[13]]); + g(state, 3, 4, 9, 14, msg[schedule[14]], msg[schedule[15]]); +} + +INLINE void compress_pre(uint32_t state[16], const uint32_t cv[8], + const uint8_t block[BLAKE3_BLOCK_LEN], + uint8_t block_len, uint64_t counter, uint8_t flags) { + uint32_t block_words[16]; + block_words[0] = load32(block + 4 * 0); + block_words[1] = load32(block + 4 * 1); + block_words[2] = load32(block + 4 * 2); + block_words[3] = load32(block + 4 * 3); + block_words[4] = load32(block + 4 * 4); + block_words[5] = load32(block + 4 * 5); + block_words[6] = load32(block + 4 * 6); + block_words[7] = load32(block + 4 * 7); + block_words[8] = load32(block + 4 * 8); + block_words[9] = load32(block + 4 * 9); + block_words[10] = load32(block + 4 * 10); + block_words[11] = load32(block + 4 * 11); + block_words[12] = load32(block + 4 * 12); + block_words[13] = load32(block + 4 * 13); + block_words[14] = load32(block + 4 * 14); + block_words[15] = load32(block + 4 * 15); + + state[0] = cv[0]; + state[1] = cv[1]; + state[2] = cv[2]; + state[3] = cv[3]; + state[4] = cv[4]; + state[5] = cv[5]; + state[6] = cv[6]; + state[7] = cv[7]; + state[8] = IV[0]; + state[9] = IV[1]; + state[10] = IV[2]; + state[11] = IV[3]; + state[12] = counter_low(counter); + state[13] = counter_high(counter); + state[14] = (uint32_t)block_len; + state[15] = (uint32_t)flags; + + round_fn(state, &block_words[0], 0); + round_fn(state, &block_words[0], 1); + round_fn(state, &block_words[0], 2); + round_fn(state, &block_words[0], 3); + round_fn(state, &block_words[0], 4); + round_fn(state, &block_words[0], 5); + round_fn(state, &block_words[0], 6); +} + +void blake3_compress_in_place_portable(uint32_t cv[8], + const uint8_t block[BLAKE3_BLOCK_LEN], + uint8_t block_len, uint64_t counter, + uint8_t flags) { + uint32_t state[16]; + compress_pre(state, cv, block, block_len, counter, flags); + cv[0] = state[0] ^ state[8]; + cv[1] = state[1] ^ state[9]; + cv[2] = state[2] ^ state[10]; + cv[3] = state[3] ^ state[11]; + cv[4] = state[4] ^ state[12]; + cv[5] = state[5] ^ state[13]; + cv[6] = state[6] ^ state[14]; + cv[7] = state[7] ^ state[15]; +} + +void blake3_compress_xof_portable(const uint32_t cv[8], + const uint8_t block[BLAKE3_BLOCK_LEN], + uint8_t block_len, uint64_t counter, + uint8_t flags, uint8_t out[64]) { + uint32_t state[16]; + compress_pre(state, cv, block, block_len, counter, flags); + + store32(&out[0 * 4], state[0] ^ state[8]); + store32(&out[1 * 4], state[1] ^ state[9]); + store32(&out[2 * 4], state[2] ^ state[10]); + store32(&out[3 * 4], state[3] ^ state[11]); + store32(&out[4 * 4], state[4] ^ state[12]); + store32(&out[5 * 4], state[5] ^ state[13]); + store32(&out[6 * 4], state[6] ^ state[14]); + store32(&out[7 * 4], state[7] ^ state[15]); + store32(&out[8 * 4], state[8] ^ cv[0]); + store32(&out[9 * 4], state[9] ^ cv[1]); + store32(&out[10 * 4], state[10] ^ cv[2]); + store32(&out[11 * 4], state[11] ^ cv[3]); + store32(&out[12 * 4], state[12] ^ cv[4]); + store32(&out[13 * 4], state[13] ^ cv[5]); + store32(&out[14 * 4], state[14] ^ cv[6]); + store32(&out[15 * 4], state[15] ^ cv[7]); +} + +INLINE void hash_one_portable(const uint8_t *input, size_t blocks, + const uint32_t key[8], uint64_t counter, + uint8_t flags, uint8_t flags_start, + uint8_t flags_end, uint8_t out[BLAKE3_OUT_LEN]) { + uint32_t cv[8]; + memcpy(cv, key, BLAKE3_KEY_LEN); + uint8_t block_flags = flags | flags_start; + while (blocks > 0) { + if (blocks == 1) { + block_flags |= flags_end; + } + blake3_compress_in_place_portable(cv, input, BLAKE3_BLOCK_LEN, counter, + block_flags); + input = &input[BLAKE3_BLOCK_LEN]; + blocks -= 1; + block_flags = flags; + } + memcpy(out, cv, 32); +} + +void blake3_hash_many_portable(const uint8_t *const *inputs, size_t num_inputs, + size_t blocks, const uint32_t key[8], + uint64_t counter, bool increment_counter, + uint8_t flags, uint8_t flags_start, + uint8_t flags_end, uint8_t *out) { + while (num_inputs > 0) { + hash_one_portable(inputs[0], blocks, key, counter, flags, flags_start, + flags_end, out); + if (increment_counter) { + counter += 1; + } + inputs += 1; + num_inputs -= 1; + out = &out[BLAKE3_OUT_LEN]; + } +} diff -Nru r-cran-digest-0.6.25/src/digest.c r-cran-digest-0.6.27/src/digest.c --- r-cran-digest-0.6.25/src/digest.c 2019-10-18 11:47:54.000000000 +0000 +++ r-cran-digest-0.6.27/src/digest.c 2020-05-20 11:42:23.000000000 +0000 @@ -36,6 +36,7 @@ #include "zlib.h" #include "xxhash.h" #include "pmurhash.h" +#include "blake3.h" #ifdef _WIN32 #include @@ -214,6 +215,22 @@ sprintf(output, "%08x", val); break; } + case 10: { /* blake3 */ + output_length = BLAKE3_OUT_LEN; + blake3_hasher hasher; + blake3_hasher_init(&hasher); + blake3_hasher_update(&hasher, txt, nChar); + uint8_t val[BLAKE3_OUT_LEN]; + blake3_hasher_finalize(&hasher, val, BLAKE3_OUT_LEN); + if (leaveRaw) { + memcpy(output, val, BLAKE3_OUT_LEN); + } else { + for (size_t i = 0; i < BLAKE3_OUT_LEN; i++) { + sprintf(output + i * 2, "%02x", val[i]); + } + } + break; + } case 101: { /* md5 file case */ int j; md5_context ctx; @@ -443,6 +460,34 @@ sprintf(output, "%08x", val); break; } + case 110: { /* blake3 file case */ + output_length = BLAKE3_OUT_LEN; + unsigned char buf[BUF_SIZE]; + uint8_t val[BLAKE3_OUT_LEN]; + blake3_hasher hasher; + blake3_hasher_init(&hasher); + + if (skip > 0) fseek(fp, skip, SEEK_SET); + if (length>=0) { + while ( ( nChar = fread( buf, 1, sizeof( buf ), fp ) ) > 0 && length>0) { + if (nChar>length) nChar=length; + blake3_hasher_update( &hasher, buf, nChar ); + length -= nChar; + } + } else { + while ( ( nChar = fread( buf, 1, sizeof( buf ), fp ) ) > 0) + blake3_hasher_update( &hasher, buf, nChar ); + } + blake3_hasher_finalize(&hasher, val, BLAKE3_OUT_LEN); + if (leaveRaw) { + memcpy(output, val, BLAKE3_OUT_LEN); + } else { + for (size_t i = 0; i < BLAKE3_OUT_LEN; i++) { + sprintf(output + i * 2, "%02x", val[i]); + } + } + break; + } default: { error("Unsupported algorithm code"); /* should not be reached due to test in R */ /* #nocov */ } diff -Nru r-cran-digest-0.6.25/vignettes/sha1.Rmd r-cran-digest-0.6.27/vignettes/sha1.Rmd --- r-cran-digest-0.6.25/vignettes/sha1.Rmd 2018-01-21 13:50:54.000000000 +0000 +++ r-cran-digest-0.6.27/vignettes/sha1.Rmd 2020-10-17 02:51:09.000000000 +0000 @@ -2,13 +2,26 @@ title: "Calculating SHA1 hashes with digest() and sha1()" author: "Thierry Onkelinx and Dirk Eddelbuettel" date: "`r Sys.Date()`" -output: rmarkdown::html_vignette +output: + minidown::mini_document: + framework: water + keep_md: TRUE + vignette: > %\VignetteIndexEntry{sha1() versus digest()} %\VignetteEngine{knitr::rmarkdown} %\VignetteEncoding{UTF-8} --- +```{r include=FALSE} +knitr::opts_chunk$set(collapse = TRUE, + class.output = NULL, + class.message = NULL, + class.warning = NULL, + class.error = NULL) +``` + + NB: This vignette is work-in-progress and not yet complete. ## Short intro on hashes