问题
I'm attempting to generate the Azure documentDB auth header for REST API calls using R instead of the C# and Node.js code snippets provided by Microsoft.
I'm specifically running into issues translating this code from Node.js to R:
var crypto = require("crypto");
var key = new Buffer(masterKey, "base64");
var text = "helloworld";
var body = new Buffer(text, "utf8");
var signature = crypto.createHmac("sha256", key).update(body).digest("base64");
In this case, masterKey can be assumed to be "abcdefghijklmnopqrsTUVWXyz19284745=="
Making a sha256 hash of the master key in R then base64 encoding that result isn't returning the same result. What specific steps should be taken to generate the same? A buffer object or equivalent doesn't seem to exist in R from what I can tell.
回答1:
Please consider the following code snippets in R:
library(digest)
library(base64enc)
masterKey <- "your master key here"
key <- base64decode(masterKey)
text <- "helloworld"
body <- enc2utf8(text)
signature <- base64encode(hmac(key, body, algo = "sha256", raw = T))
print(signature)
来源:https://stackoverflow.com/questions/42147894/azure-documentdb-auth-header-in-r