HMAC SHA256 Powershell 转换
HMAC SHA256 Powershell convert
为什么下面的 powershell 脚本:
$privateKey = "843c1f887b"
$requestData = "1543448572|d.lastname@firm.com|Firstname Lastname"
function signRequest {
$hmacsha = New-Object System.Security.Cryptography.HMACSHA256
$hmacsha.key = [Text.Encoding]::ASCII.GetBytes($privateKey)
$signature = $hmacsha.ComputeHash([Text.Encoding]::ASCII.GetBytes($requestData))
$signature = [Convert]::ToBase64String($signature)
$outi = $signature
return $signature
}
转换为散列:
FipK51tOtzb2m2yFQAf5IK6BNthClnqE24luMzYMPuo=
和其他具有相同输入的在线 hmac sha256 生成器:
162a4ae75b4eb736f69b6c854007f920ae8136d842967a84db896e33360c3eea
对我在脚本中做错了什么有什么建议吗?
谢谢!
您的代码生成了正确的 HMAC,您只是对其进行了 base64 编码,而不是像所有其他工具一样输出十六进制字符串。
更改此行
$signature = [Convert]::ToBase64String($signature)
至
$signature = [System.BitConverter]::ToString($signature).Replace('-','').ToLower()
解释:
[BitConverter]::ToString($signature)
生成十六进制字符串 (16-2A-4A-E7...
)
String.Replace('-','')
删除 -
(162A4AE7...
)
String.ToLower()
小写最后的字符串 (162a4ae7...
)
为什么下面的 powershell 脚本:
$privateKey = "843c1f887b"
$requestData = "1543448572|d.lastname@firm.com|Firstname Lastname"
function signRequest {
$hmacsha = New-Object System.Security.Cryptography.HMACSHA256
$hmacsha.key = [Text.Encoding]::ASCII.GetBytes($privateKey)
$signature = $hmacsha.ComputeHash([Text.Encoding]::ASCII.GetBytes($requestData))
$signature = [Convert]::ToBase64String($signature)
$outi = $signature
return $signature
}
转换为散列:
FipK51tOtzb2m2yFQAf5IK6BNthClnqE24luMzYMPuo=
和其他具有相同输入的在线 hmac sha256 生成器:
162a4ae75b4eb736f69b6c854007f920ae8136d842967a84db896e33360c3eea
对我在脚本中做错了什么有什么建议吗? 谢谢!
您的代码生成了正确的 HMAC,您只是对其进行了 base64 编码,而不是像所有其他工具一样输出十六进制字符串。
更改此行
$signature = [Convert]::ToBase64String($signature)
至
$signature = [System.BitConverter]::ToString($signature).Replace('-','').ToLower()
解释:
[BitConverter]::ToString($signature)
生成十六进制字符串 (16-2A-4A-E7...
)String.Replace('-','')
删除-
(162A4AE7...
)String.ToLower()
小写最后的字符串 (162a4ae7...
)