base64 decode - nul in string

一个人想着一个人 提交于 2020-01-04 13:41:32

问题


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=='

回答1:


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)



回答2:


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.



来源:https://stackoverflow.com/questions/14964142/base64-decode-nul-in-string

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