问题描述
我目前正在尝试弄清楚如何为AWS中的消息属性计算MD5消息摘要。 我正在按照uri SQS message metadata> 计算邮件属性的MD5邮件摘要
var messageAttributes = new Dictionary<string,MessageAttributeValue>
{
{"UserName",new MessageAttributeValue {DataType ="String",StringValue = "Me"}}
};
我已发送此消息,并且MD5响应为 3a6071d47534e3e07414fea5046fc217
试图弄清楚文档,我认为这应该可以解决问题:
private void CustomCalc()
{
var verifyMessageAttributes = new List<byte>();
verifyMessageAttributes.AddRange(EncodeString("UserName"));
verifyMessageAttributes.AddRange(EncodeString("String"));
verifyMessageAttributes.AddRange(EncodeString("Me"));
var verifyMessageAttributesMd5 = GetMd5Hash(verifyMessageAttributes.ToArray());
}
private List<byte> EncodeString(string data)
{
var result = new List<byte>();
if (BitConverter.IsLittleEndian)
{
result.AddRange(BitConverter.GetBytes(data.Length).Reverse());
}
else
{
result.AddRange(BitConverter.GetBytes(data.Length));
}
result.AddRange(Encoding.UTF8.GetBytes(data));
return result;
}
public static string GetMd5Hash(byte[] input)
{
using (var md5Hash = MD5.Create())
{
// Convert the input string to a byte array and compute the hash.
var dataBytes = md5Hash.ComputeHash(input);
// Create a new string builder to collect the bytes and create a string.
var sBuilder = new StringBuilder();
// Loop through each byte of the hashed data and format each one as a hexadecimal string.
foreach (var dataByte in dataBytes)
{
sBuilder.Append(dataByte.ToString("x2"));
}
// Return the hexadecimal string.
return sBuilder.ToString();
}
}
但是我最终得到了这个 cf886cdabbe5576c0ca9dc51871d10ae 有谁知道我要去哪里错了。我想我现在暂时看不到这并不是很难。
解决方法
您快到了,但缺少一步:
编码值(1个字节)的传输类型(字符串或二进制)。
注意逻辑数据类型String和Number使用String传输 类型。
逻辑数据类型Binary使用二进制传输类型。
对于字符串传输类型,编码为1。
对于二进制传输类型,编码2。
因此,您需要在值前附加1或2,以指示传输类型。就您而言:
var verifyMessageAttributes = new List<byte>();
verifyMessageAttributes.AddRange(EncodeString("UserName"));
verifyMessageAttributes.AddRange(EncodeString("String"));
verifyMessageAttributes.Add(1); // < here
verifyMessageAttributes.AddRange(EncodeString("Me"));
var verifyMessageAttributesMd5 = GetMd5Hash(verifyMessageAttributes.ToArray());
,
这是Node.js中的独立解决方案:
const md5 = require('md5');
const SIZE_LENGTH = 4;
const TRANSPORT_FOR_TYPE_STRING_OR_NUMBER = 1;
const transportType1 = ['String','Number'];
module.exports = (messageAttributes) => {
const buffers = [];
const keys = Object.keys(messageAttributes).sort();
keys.forEach((key) => {
const { DataType,StringValue } = messageAttributes[key];
const nameSize = Buffer.alloc(SIZE_LENGTH);
nameSize.writeUInt32BE(key.length);
const name = Buffer.alloc(key.length);
name.write(key);
const typeSize = Buffer.alloc(SIZE_LENGTH);
typeSize.writeUInt32BE(DataType.length);
const type = Buffer.alloc(DataType.length);
type.write(DataType);
const transport = Buffer.alloc(1);
let valueSize;
let value;
if (transportType1.includes(DataType)) {
transport.writeUInt8(TRANSPORT_FOR_TYPE_STRING_OR_NUMBER);
valueSize = Buffer.alloc(SIZE_LENGTH);
valueSize.writeUInt32BE(StringValue.length);
value = Buffer.alloc(StringValue.length);
value.write(StringValue);
} else {
throw new Error(
'Not implemented: MessageAttributes with type Binary are not supported at the moment.'
);
}
const buffer = Buffer.concat([nameSize,name,typeSize,type,transport,valueSize,value]);
buffers.push(buffer);
});
return md5(Buffer.concat(buffers));
};
在GitHub的sqslite存储库中查看更多信息