jeroen / openssl

OpenSSL bindings for R
Other
63 stars 21 forks source link

Character Hash for Connection Objects #14

Closed phillc73 closed 8 years ago

phillc73 commented 8 years ago

I'm currently writing an R package for the Backblaze B2 API (https://github.com/phillc73/backblazer)

When uploading a file, B2 expects an SHA1 hash of the file content (https://www.backblaze.com/b2/docs/b2_upload_file.html).

As per hash.R man page: "When passing a connection object, the contents will be stream-hashed which minimizes the amount of required memory. This is recommended for hashing files from disk or network."

B2 will not validate the uploaded file with a stream-hash, instead requiring a character hash string.

Is there any reason why there isn't an option to generate a character hash from connection objects? Or rather perhaps a better question might be, is there a straightforward way to convert a stream-hash to a character hash (besides judicious use of sub)?

I should also note that using the example on the B2 docs page, and generating a hash directly via terminal and opensssl does in fact return the correct character hash.

openssl dgst -sha1 $FILE_TO_UPLOAD

Ideally I'd like to replicate this exact functionality within R.

Any thoughts on this most appreciated.

jeroen commented 8 years ago

I don't understand the problem. Does this example help? The hash should match the one from here

download.file("http://cran.r-project.org/bin/windows/base/old/3.1.1/R-3.1.1-win.exe", "R-3.1.1-win.exe")
rawhash <- md5(file("R-3.1.1-win.exe"))
stringhash <- paste(rawhash, collapse = "")
print(stringhash)
phillc73 commented 8 years ago

There isn't a problem. Just user error.

The issue was that I was confused by my console output on Linux. e.g.

> library(openssl)
> rawhash <- md5(file("R-3.1.1-win.exe"))
> rawhash
md5 0b:48:29:e8:92:10:eb:6d:13:71:24:8c:d0:97:d1:fc 
> stringhash <- paste(rawhash, collapse = "")
> print(stringhash)
[1] "0b4829e89210eb6d1371248cd097d1fc"

I didn't think to simply collapse the rawhash. I was rather looking at using sub and friends to remove colons and white space.

jeroen commented 8 years ago

Ah ok, it's just a raw vector with a pretty print method.