Azure DocumentDB auth header in R

流过昼夜 提交于 2019-12-25 16:08:12

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!