Question

I'm trying to create function that will run private API calls on some sites. All those sites are using authentication by signature of the API call as follows in php:

base64_encode( hash_hmac('sha512', $post_data, base64_decode($secret), true) )

Using package digest and RCurl I can easily write as:

base64Encode( hmac(key = base64Decode(secret), object = post_data, algo = 'sha512', raw = TRUE) )

I'm fighting whole day with the hmac key parameter input:

key = base64Decode(secret)

The issue is that hmac function accept only string value but base64Decode might return something more than just string:

str(base64Decode(secret))
chr "îă?ľÂÜĄ\vŽĺ\022""| __truncated__

This __truncated__ is the key issue here. So the next thing I've tried was take a raw output from decode function and rawToChar on it:

str(base64Decode(secret,mode='raw'))
raw [1:64] ee e3 3f be ...

rawToChar(base64Decode(secret,mode='raw'))

Error in rawToChar(base64Decode(secret,mode='raw')) : embedded nul in string: 'îă?ľÂÜĄ\vŽĺ\022\0!^\026Č‹¶©wŚˇ0Î\035ë\026\r\001ňKÍ„Rř\003j„7¤Ň\nťä_\004m@ß\0Ă"c\0271˝ZnĚ55’v'

As we can see now, there is a mystery nul thing somewhere in my (not even yet) string. I don't care much about the nuls, I just need to pass this piece of data as input for hmac.

  1. Is there any possibility to handle string with nul, just for pushing it farther as input upper function?
  2. Is there any possibility to have hmac function key parameter accept raw object like this?

I tried also base64enc package with no success. I tried many many different conversions but everything is going back this simple 'nul in string'.

These sites are rather new and this authentication process looks like a standard API auth among them. There should be some way to handle that process in R.

If anyone want to test,

secret <- '7uM/vsLcpQuOmOUSACFeFsiLtql3jKEwzh3rFg0B8kvNhFL4A2qEN6TSCp3kXwRtQN8AwyJjFzG9Wm7MNTWSdg=='
Was it helpful?

Solution

Answer for the question 2. is:

hmac function already accept raw object instead of string.

This is the solution:

hmac(key = base64Decode(secret, mode='raw'),
 object = post_data,
 algo = 'sha512',
 raw = TRUE)

OTHER TIPS

I'm really not sure that th truncated is the problem. When you apply str to your output, you ask it to display its structure. That's what it does :

chr "îă?ľÂÜĄ\vŽĺ\022""| __truncated__

chr means that the object is of class character (a string). str then displays the beginning of the string, put truncates its output. Hence the truncated displayed by str. If you want to display the complete string, just type its name or use print :

secret <- '7uM/vsLcpQuOmOUSACFeFsiLtql3jKEwzh3rFg0B8kvNhFL4A2qEN6TSCp3kXwRtQN8AwyJjFzG9Wm7MNTWSdg=='
str(base64Decode(secret))
# chr "\xee\xe3?\xbe\xc2ܥ\v\x8e\x98\xe5\022"

So I suppose that the problem is not with the base64Decode function, but elsewhere in your script.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top