Error with HMAC and newlines in R package "digest"

I am trying to implement R code to interact with Amazon Alexa Web Information Service. The authentication system requires hashing the HMAC text against the private key, but it seems that the problem is with the digest package when the text contains newlines.

Minimal example in R:

library(digest)
hmac("foo", "Hello", algo="sha256")

returns fa687477a49ebadb72eb1103db6128061437a2501db7ee7f0cbbb79ceaa2fcfcand

hmac("foo", "Hello\nGoodbye", algo="sha256")

returns eaf58b106ffdbb4af976b6b87e14d231e090f7bc144f0b56f06980c827444288.

If I check http://www.freeformatter.com/hmac-generator.html , the first case gives the same hash, and the second gives 967b28392b2ddc871bb69417e916fa619c935840cc2b9507ecf4da3f748bd1ba.

Am I missing something obvious?

Thanks in advance!

+4
source share
2 answers

! , - Windows, . , "967b",

hmac("foo","Hello\r\nGoodbye",algo="sha256")
# [1] "967b28392b2ddc871bb69417e916fa619c935840cc2b9507ecf4da3f748bd1ba"

, Mac Linux-. , \n . ,

hmac("foo","Hello\x0D\x0AGoodbye",algo="sha256")

, .

+2

, MrFlick , . Perl hmac_sha256_base64() R.

, R. hmac() , base64, . , , base64() RCurl , . .

():

require(digest)
require(RCurl)

> hmac("foo", "Hello", algo="sha256")
[1] "fa687477a49ebadb72eb1103db6128061437a2501db7ee7f0cbbb79ceaa2fcfc"

> base64(hmac("foo", "Hello", algo="sha256"))
[1] "ZmE2ODc0NzdhNDllYmFkYjcyZWIxMTAzZGI2MTI4MDYxNDM3YTI1MDFkYjdlZTdmMGNiYmI3OWNlYWEyZmNmYw=="
attr(,"class")
[1] "base64"

():

> hmac("foo", "Hello", algo="sha256", raw = T)
[1] fa 68 74 77 a4 9e ba db 72 eb 11 03 db 61 28 06 14 37 a2 50 1d b7 ee 7f 0c bb b7 9c ea a2 fc fc

> base64(hmac("foo", "Hello", algo="sha256", raw = T))
[1] "+mh0d6Seutty6xED22EoBhQ3olAdt+5/DLu3nOqi/Pw="
attr(,"class")
[1] "base64"

, ?

Perl, ( ) Amazon Web Services.: -)

+10

Source: https://habr.com/ru/post/1546765/


All Articles