1

I'm trying to do a hash so that I can authenticate against the google api.

If I run base64.urlsafe_b64decode(client_secret)? where client_secret is my key and then use that in R in digest:hmac, everything works.

However, I can't find the b64decode?

Regarding comment below:

I admit I'm somewhat of a beginner, but here is an example. Note that I made up the secret_key here as obviously I can't share the real one publicly.

In Python:

base64.urlsafe_b64decode("49ugdj9v_3290k3r902qkf9=")
Out[35]: '\xe3\xdb\xa0v?o\xff}\xbd\xd2M\xeb\xf7M\xaa\x91\xff'

Note: The result above is exactly the string that I want.

In R:

RCurl::base64("49ugdj9v_3290k3r902qkf9=", encode = FALSE)
[1] "\xe3۠v?o\003}\xbd\xd2M\xeb\xf7M\xaa\x91\xff"

Notice that the result is not the same. Also, I tried doing:

> URLencode(RCurl::base64("49ugdj9v_3290k3r902qkf9=", encode = FALSE))
[1] "NA"
Warning message:
In strsplit(URL, "") : input string 1 is invalid in this locale

If I try a different base64 encoder in R, I get the same thing:

rawToChar(base64decode("49ugdj9v_3290k3r902qkf9="))
[1] "\xe3۠v?o\xdfot\x93z\xfd\xd3j\xa4\177"

Anyway, I hope this helps explain my trouble. If anyone has a solution, that would be much appreciated.

user1357015
  • 11,168
  • 22
  • 66
  • 111
  • @hrbrmstr: yep, i've tried that -- it doesn't give the correct value... – user1357015 Aug 18 '15 at 01:35
  • Why don't you include a clear [reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). Include sample input data and the desired output for that input. Do you just not know how to do any sort of base64 encoding? Show the R code you've tried running. – MrFlick Aug 18 '15 at 04:49
  • @MrFlick: As requested, I've made an example above. Thank you for looking. – user1357015 Aug 18 '15 at 05:02
  • Unfortunately, I can not. I am following the example here: https://developers.google.com/maps/documentation/business/webservices/auth#signature_examples I tried looking at some of the others but I can't seem to get the function to work. – user1357015 Aug 18 '15 at 05:21

1 Answers1

3

When you do a "url-safe" encode, you convert / to _ and + to -. So in order to properly decode the the values, you need to undo these translations first.

urlsafebase64becode <- function(x, ...) {
     RCurl::base64(gsub("_","/", gsub("-","+",x,fixed=TRUE),fixed=TRUE), encode=FALSE, ...)
}

urlsafebase64becode("49ugdj9v_3290k3r902qkf=")
urlsafebase64becode("49ugdj9v_3290k3r902qkf=", mode="raw")
#  [1] e3 db a0 76 3f 6f ff 7d bd d2 4d eb f7 4d

which seems to match the Python

' '.join(x.encode('hex') for x in base64.urlsafe_b64decode("49ugdj9v_3290k3r902qkf9="))
'e3 db a0 76 3f 6f ff 7d bd d2 4d eb f7 4d aa 91 ff'
MrFlick
  • 195,160
  • 17
  • 277
  • 295
  • Thanks for this! Seems to work -- I wish this existed in an R package directly... – user1357015 Aug 18 '15 at 05:51
  • In case anyone needs the encode function, I provide it here for completeness: `urlsafebase64encode <- function(x, ...) { gsub("+","-",gsub("/","_",RCurl::base64(x,encode =TRUE), fixed = TRUE), fixed = TRUE) }` – user1357015 Aug 18 '15 at 18:02