MD5 и SHA256 совпадают в обоих случаях для приведенного выше кода. Я все еще пытаюсь решить проблему с хешированием, которая связана с тем же вопросом.
# we read some dummy data that will be hashed
#PAYLOAD='teststring'
read -r -d '' PAYLOAD << EOF
{
"type": "MatchType",
"membershipId": "1234567890",
"givenName": "FirstName",
"surname": "LastName"
}
EOF
HMAC_KEY='ADf3TSCGjNd4Zj29'
# the verb
VERB='POST'
# the full resource URL
RESOURCE='/v1/member/profile/validate'
# a generated timestamp used to prevent replay
TIMESTAMP=$(date -u +'%Y-%m-%dT%H:%M:%SZ')
CONTENT_TYPE='application/json'
# generate the content length
CONTENT_LENGTH=$( echo -n "${PAYLOAD}" | wc -c )
CONTENT_DIGEST=$(echo -n "${PAYLOAD}" | openssl dgst -md5 -binary | openssl enc -base64 -A)
printf "\nPayload MD5 hash input string\n" >> log.txt;
echo -n "${PAYLOAD}" >> log.txt;
printf "\nMD5 Base64Encoded string\n" >> log.txt;
echo -n "${PAYLOAD}" | openssl dgst -md5 -binary | openssl enc -base64 -A >> log.txt
printf "\nHMAC hash input string\n" >> log.txt;
printf '%s\n%s\n%s\n%s\n%s' "${VERB}""${TIMESTAMP}""${RESOURCE}""${CONTENT_TYPE}""${CONTENT_DIGEST}" >> log.txt;
printf "\nHMAC Base64Encoded string\n" >> log.txt;
printf '%s\n%s\n%s\n%s\n%s' "${_VERB}""${_TIMESTAMP}""${_RESOURCE}""${_CONTENT_TYPE}""${_CONTENT_DIGEST}" | openssl dgst -binary -sha256 -hmac "${HMAC_KEY}" | openssl enc -base64 -A >> log.txt;
Это выводит
Полезная нагрузка MD5, строка ввода хеша
{
"type": "MatchType",
"membersId": "1234567890",
"GivenName": "FirstName",
"фамилия"
}
MD5 Base64Кодированная строка
MkY18c4MmVoS2r66DSNgRQ ==
HMAC строка ввода хэша
POST 2019-06-01T14: 04: 47Z / v1 / член / профиль / validateapplication / jsonMkY18c4MmVoS2r66DSNgRQ ==
HMAC Base64Кодированная строка
BlfQhEyX5tRDl7bpDS0PVmFUm4QBn3r7KazOIfG / 1JQ =
string hmacKey = "ADf3TSCGjNd4Zj29";
string stringjsonData = "{\"type\": \"MatchType\",\"membershipId\": \"1234567890\",\"givenName\": \"FirstName\",\"surname\": \"LastName\"}";
//string stringjsonData = "teststring";
int jsonStringLength = stringjsonData.Length;
string verb = "POST";
string datetime = "2019-06-01T14:04:47Z";
string url = "/v1/member/profile validate";
string contenttype = "application/json";
System.Text.UTF8Encoding encoding = new System.Text.UTF8Encoding();
byte[] keyByte = encoding.GetBytes(hmacKey);
MD5 hmacmd5 = MD5.Create();
HMACSHA256 hmacsha256 = new HMACSHA256(keyByte);
byte[] utf8EncodedMD5DataBytes = encoding.GetBytes(stringjsonData);
byte[] hmacmd5HashBytes = hmacmd5.ComputeHash(utf8EncodedMD5DataBytes);
string base64md5HashString = Convert.ToBase64String(hmacmd5HashBytes);
string sha256Input = verb + "\n " + datetime + "\n " + url + "\n " + contenttype + "\n " + base64md5HashString;
byte[] utf8EncodedSHA256DataBytes = encoding.GetBytes(sha256Input);
byte[] hmasha256HashBytes = hmacsha256.ComputeHash(utf8EncodedSHA256DataBytes);
string base64sha256HashString = Convert.ToBase64String(hmasha256HashBytes);
Console.ReadLine();
Хеш, сгенерированный из кода c #:
MD5 Base64Кодированная строка RrKDQmKSdDnByqlFiMcTOA ==
SHA256 Base64Кодированная строка J9S5IrnFuzwZrCqAVxcaL30xDCrZnyBpu9NHGFpHSQw =
Обратите внимание, что я использую точную дату из выходных данных скрипта в качестве даты в программе на C #.