Reputation: 440
I am trying to reproduce the following golang code in R for creating the signature for the consibio/mbtilesever. The aim is to build apps in R and python which can access mbtileservers uisng a secure connection. While I am able to generate a working version using python, a working version using R is proving tricky. Thanks in advance.
The sample GO code from the link above :
serviceId := "test"
date := "2019-03-08T19:31:12.213831+00:00"
salt := "0EvkK316T-sBLA"
secretKey := "YMIVXikJWAiiR3q-JMz1v2Mfmx3gTXJVNqme5kyaqrY"
key := sha1.New()
key.Write([]byte(salt + secretKey))
hash := hmac.New(sha1.New, key.Sum(nil))
message := fmt.Sprintf("%s:%s", date, serviceId)
hash.Write([]byte(message))
b64hash := base64.RawURLEncoding.EncodeToString(hash.Sum(nil))
fmt.Println(b64hash) // Should output: 2y8vHb9xK6RSxN8EXMeAEUiYtZk
In Python code is (working):
import hashlib
import hmac
import base64
service_id = "test"
date = "2019-03-08T19:31:12.213831+00:00"
salt = "0EvkK316T-sBLA"
secret_key = "YMIVXikJWAiiR3q-JMz1v2Mfmx3gTXJVNqme5kyaqrY"
key = hashlib.sha1()
key.update((salt + secret_key).encode())
hash_obj = hmac.new(key.digest(), (date + ":" + service_id).encode(), hashlib.sha1)
b64_hash = base64.urlsafe_b64encode(hash_obj.digest()).decode()
print(b64_hash) # Should output: 2y8vHb9xK6RSxN8EXMeAEUiYtZk
The R code so far (not-working):
library(openssl)
library(digest)
service_id <- "test"
date <- "2019-03-08T19:31:12.213831+00:00"
salt <- "0EvkK316T-sBLA"
secret_key <- "YMIVXikJWAiiR3q-JMz1v2Mfmx3gTXJVNqme5kyaqrY"
key <- sha1(paste0(salt, secret_key))
hash_obj <- hmac(key, paste0(date, ":", service_id), "sha1")
b64_hash <- base64_encode(hash_obj)
print(b64_hash) # Should output: 2y8vHb9xK6RSxN8EXMeAEUiYtZk
Upvotes: 1
Views: 64
Reputation: 8886
How about this?
library(digest)
library(base64enc)
service_id <- "test"
date <- "2019-03-08T19:31:12.213831+00:00"
salt <- "0EvkK316T-sBLA"
secret_key <- "YMIVXikJWAiiR3q-JMz1v2Mfmx3gTXJVNqme5kyaqrY"
key_digest <- digest::digest(
paste0(salt, secret_key),
algo = "sha1",
serialize = FALSE,
raw = TRUE
)
hmac_binary <- digest::hmac(
key_digest,
paste0(date, ":", service_id),
"sha1",
raw = TRUE
)
base64enc::base64encode(hmac_binary) # Should output: 2y8vHb9xK6RSxN8EXMeAEUiYtZk
#> [1] "2y8vHb9xK6RSxN8EXMeAEUiYtZk="
Created on 2024-04-16 with reprex v2.1.0
Upvotes: 1