user1357015
user1357015

Reputation: 11696

What is the R equivalent of python base64.urlsafe_b64decode(client_secret)?

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.

Upvotes: 1

Views: 370

Answers (1)

MrFlick
MrFlick

Reputation: 206401

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'

Upvotes: 3

Related Questions