Python 自定义函数到 R 的转换
Python to R conversion of a custom function
下面给出了两个 python 函数
import sys, os, base64, datetime, hashlib, hmac
def sign(key, msg):
return hmac.new(key, msg.encode("utf-8"), hashlib.sha256).digest()
def getSignatureKey(key, datestamp, regionName, serviceName):
kDate = sign(('AWS4' + key).encode('utf-8'), datestamp)
kRegion = sign(kDate, regionName)
kService = sign(kRegion, serviceName)
kSigning = sign(kService, 'aws4_request')
return kSigning
我想将它们转换为 R 函数
这是我试过的
library(digest)
sign=function(key, msg)
{
return (digest(hmac(key,enc2utf8(msg),"sha256", raw=F)))
}
getSignatureKey=function(key, datestamp, regionName, serviceName)
{
kDate = sign(enc2utf8(paste0('AWS4' , key)), datestamp)
kRegion = sign(kDate, regionName)
kService = sign(kRegion, serviceName)
kSigning = sign(kService, 'aws4_request')
return (kSigning)
}
给定以下输入
key = 'wJalrXUtnFEMI/K7MDENG+bPxRfiCYEXAMPLEKEY'
datestamp = '20120215'
regionName = 'us-east-1'
serviceName = 'iam'
预期输出如下(根据 python 函数)
kSecret = '41575334774a616c725855746e46454d492f4b374d44454e472b62507852666943594558414d504c454b4559'
kDate = '969fbb94feb542b71ede6f87fe4d5fa29c789342b0f407474670f0c2489e0a0d'
kRegion = '69daa0209cd9c5ff5c8ced464a696fd4252e981430b10e3d3fd8e2f197d7a70c'
kService = 'f72cfd46f26bc4643f06a11eabb6c0ba18780c19a8da0c31ace671265e3c87fa'
kSigning = 'f4780e2d9f65fa895f9c67b32ce1baf0b0d8a43505a000a1a9e090d414db404d'
不幸的是,我无法在 R 中生成上述值。
我想了解R函数中的错误是什么。
这里有更多详细信息,以防您想了解这是从哪里来的 (https://docs.aws.amazon.com/general/latest/gr/signature-v4-examples.html#signature-v4-examples-other)
我想通了。
对于第一个函数,它不需要 digest 并保留参数 raw=T.
sign=function(key, msg)
{
return (hmac(key,msg,"sha256", raw=T))
}
下面给出了第二个函数,添加了一行将字节向量转换为字符串
getSignatureKey=function(key, date_stamp, regionName, serviceName)
{
kDate = sign(paste0('AWS4' , key), date_stamp)
kRegion = sign(kDate, regionName)
kService = sign(kRegion, serviceName)
kSigning = sign(kService, 'aws4_request')
kSigning=paste(kSigning,collapse="")
return (kSigning)
}
出于某种原因保留参数,hmac 函数中的 raw=F 搞乱了函数 2 中的后续签名操作。因此保留 raw=T 并使用 paste 最终使其成为字符串。
下面给出了两个 python 函数
import sys, os, base64, datetime, hashlib, hmac
def sign(key, msg):
return hmac.new(key, msg.encode("utf-8"), hashlib.sha256).digest()
def getSignatureKey(key, datestamp, regionName, serviceName):
kDate = sign(('AWS4' + key).encode('utf-8'), datestamp)
kRegion = sign(kDate, regionName)
kService = sign(kRegion, serviceName)
kSigning = sign(kService, 'aws4_request')
return kSigning
我想将它们转换为 R 函数
这是我试过的
library(digest)
sign=function(key, msg)
{
return (digest(hmac(key,enc2utf8(msg),"sha256", raw=F)))
}
getSignatureKey=function(key, datestamp, regionName, serviceName)
{
kDate = sign(enc2utf8(paste0('AWS4' , key)), datestamp)
kRegion = sign(kDate, regionName)
kService = sign(kRegion, serviceName)
kSigning = sign(kService, 'aws4_request')
return (kSigning)
}
给定以下输入
key = 'wJalrXUtnFEMI/K7MDENG+bPxRfiCYEXAMPLEKEY'
datestamp = '20120215'
regionName = 'us-east-1'
serviceName = 'iam'
预期输出如下(根据 python 函数)
kSecret = '41575334774a616c725855746e46454d492f4b374d44454e472b62507852666943594558414d504c454b4559'
kDate = '969fbb94feb542b71ede6f87fe4d5fa29c789342b0f407474670f0c2489e0a0d'
kRegion = '69daa0209cd9c5ff5c8ced464a696fd4252e981430b10e3d3fd8e2f197d7a70c'
kService = 'f72cfd46f26bc4643f06a11eabb6c0ba18780c19a8da0c31ace671265e3c87fa'
kSigning = 'f4780e2d9f65fa895f9c67b32ce1baf0b0d8a43505a000a1a9e090d414db404d'
不幸的是,我无法在 R 中生成上述值。
我想了解R函数中的错误是什么。
这里有更多详细信息,以防您想了解这是从哪里来的 (https://docs.aws.amazon.com/general/latest/gr/signature-v4-examples.html#signature-v4-examples-other)
我想通了。
对于第一个函数,它不需要 digest 并保留参数 raw=T.
sign=function(key, msg)
{
return (hmac(key,msg,"sha256", raw=T))
}
下面给出了第二个函数,添加了一行将字节向量转换为字符串
getSignatureKey=function(key, date_stamp, regionName, serviceName)
{
kDate = sign(paste0('AWS4' , key), date_stamp)
kRegion = sign(kDate, regionName)
kService = sign(kRegion, serviceName)
kSigning = sign(kService, 'aws4_request')
kSigning=paste(kSigning,collapse="")
return (kSigning)
}
出于某种原因保留参数,hmac 函数中的 raw=F 搞乱了函数 2 中的后续签名操作。因此保留 raw=T 并使用 paste 最终使其成为字符串。