Skip to content

Commit 42fa915

Browse files
authored
Merge pull request #3 from jdanekrh/jd_endings_hash
NEW --msg-content-hashed argument
2 parents 346cfab + c685608 commit 42fa915

File tree

5 files changed

+97
-44
lines changed

5 files changed

+97
-44
lines changed

ClientLib/Formatter.cs

Lines changed: 32 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
using System;
1818
using System.Collections.Generic;
1919
using System.Collections.ObjectModel;
20+
using System.Linq;
21+
using System.Security.Cryptography;
2022
using System.Text;
2123

2224
using Amqp;
@@ -39,15 +41,16 @@ public class Formatter
3941
/// Print message in upstream format
4042
/// </summary>
4143
/// <param name="msg">msg object</param>
42-
public static void PrintMessage(Message msg)
44+
/// <param name="hashContent"></param>
45+
public static void PrintMessage(Message msg, bool hashContent)
4346
{
4447
Console.WriteLine("Message(");
4548
if (msg.Header != null) Console.WriteLine(msg.Header.ToString());
4649
if (msg.DeliveryAnnotations != null) Console.WriteLine(msg.DeliveryAnnotations.ToString());
4750
if (msg.MessageAnnotations != null) Console.WriteLine(msg.MessageAnnotations.ToString());
4851
if (msg.Properties != null) Console.WriteLine(msg.Properties.ToString());
4952
if (msg.ApplicationProperties != null) Console.WriteLine(msg.ApplicationProperties.ToString());
50-
if (msg.Body != null) Console.WriteLine("body:{0}", msg.Body.ToString());
53+
if (msg.Body != null) Console.WriteLine("body:{0}", hashContent ? Hash(msg.Body.ToString()): msg.Body.ToString());
5154
if (msg.Footer != null) Console.WriteLine(msg.Footer.ToString());
5255
Console.WriteLine(")");
5356
}
@@ -56,7 +59,8 @@ public static void PrintMessage(Message msg)
5659
/// Print message as python dict
5760
/// </summary>
5861
/// <param name="msg">message object</param>
59-
public static void PrintMessageAsDict(Message msg)
62+
/// <param name="hashContent"></param>
63+
public static void PrintMessageAsDict(Message msg, bool hashContent)
6064
{
6165
Dictionary<string, object> msgDict = new Dictionary<string,object>();
6266
msgDict.Add("durable", msg.Header.Durable);
@@ -76,7 +80,7 @@ public static void PrintMessageAsDict(Message msg)
7680
msgDict.Add("group-id", msg.Properties.GroupId);
7781
msgDict.Add("group-sequence", msg.Properties.GroupSequence);
7882
msgDict.Add("reply-to-group-id", msg.Properties.ReplyToGroupId);
79-
msgDict.Add("content", msg.Body);
83+
msgDict.Add("content", hashContent ? Hash(msg.Body) : msg.Body);
8084
msgDict.Add("properties", msg.ApplicationProperties);
8185
msgDict.Add("message-annotations", msg.MessageAnnotations);
8286
Console.WriteLine(FormatMap(msgDict));
@@ -86,7 +90,8 @@ public static void PrintMessageAsDict(Message msg)
8690
/// Print message as python dict (keys are named by AMQP standard)
8791
/// </summary>
8892
/// <param name="msg">message object</param>
89-
public static void PrintMessageAsInterop(Message msg)
93+
/// <param name="hashContent"></param>
94+
public static void PrintMessageAsInterop(Message msg, bool hashContent)
9095
{
9196
Dictionary<string, object> msgDict = new Dictionary<string, object>();
9297
msgDict.Add("durable", msg.Header.Durable);
@@ -107,7 +112,7 @@ public static void PrintMessageAsInterop(Message msg)
107112
msgDict.Add("group-id", msg.Properties.GroupId);
108113
msgDict.Add("group-sequence", msg.Properties.GroupSequence);
109114
msgDict.Add("reply-to-group-id", msg.Properties.ReplyToGroupId);
110-
msgDict.Add("content", msg.Body);
115+
msgDict.Add("content", hashContent ? Hash(msg.Body) : msg.Body);
111116
msgDict.Add("properties", msg.ApplicationProperties);
112117
//msgDict.Add("message-annotations", msg.MessageAnnotations);
113118
Console.WriteLine(FormatMap(msgDict));
@@ -117,7 +122,8 @@ public static void PrintMessageAsInterop(Message msg)
117122
/// Print message as python dict (keys are named by AMQP standard)
118123
/// </summary>
119124
/// <param name="msg">message object</param>
120-
public static void PrintMessageAsJson(Message msg)
125+
/// <param name="hashContent"></param>
126+
public static void PrintMessageAsJson(Message msg, bool hashContent)
121127
{
122128
Dictionary<string, object> msgDict = new Dictionary<string, object>();
123129
msgDict.Add("durable", msg.Header.Durable);
@@ -138,7 +144,7 @@ public static void PrintMessageAsJson(Message msg)
138144
msgDict.Add("group-id", msg.Properties.GroupId);
139145
msgDict.Add("group-sequence", msg.Properties.GroupSequence);
140146
msgDict.Add("reply-to-group-id", msg.Properties.ReplyToGroupId);
141-
msgDict.Add("content", msg.Body);
147+
msgDict.Add("content", hashContent ? Hash(msg.Body) : msg.Body);
142148
msgDict.Add("properties", msg.ApplicationProperties);
143149
Console.WriteLine(JsonConvert.SerializeObject(msgDict));
144150
}
@@ -553,29 +559,41 @@ public static string FormatVariant(object inData)
553559
/// <param name="options">agruments of client</param>
554560
public static void LogMessage(Message msg, SenderReceiverOptions options)
555561
{
562+
var hashContent = options.HashContent;
556563

557564
if (options.LogMsgs == "body")
558565
{
559-
Console.WriteLine(msg.Body);
566+
Console.WriteLine(hashContent ? Hash(msg.Body) : msg.Body);
560567
}
561568
else if (options.LogMsgs.Equals("dict"))
562569
{
563-
Formatter.PrintMessageAsDict(msg);
570+
Formatter.PrintMessageAsDict(msg, hashContent);
564571
}
565572
else if (options.LogMsgs.Equals("upstream"))
566573
{
567-
Formatter.PrintMessage(msg);
574+
Formatter.PrintMessage(msg, hashContent);
568575
}
569576
else if (options.LogMsgs.Equals("interop"))
570577
{
571-
Formatter.PrintMessageAsInterop(msg);
578+
Formatter.PrintMessageAsInterop(msg, hashContent);
572579
}
573580
else if (options.LogMsgs.Equals("json"))
574581
{
575-
Formatter.PrintMessageAsJson(msg);
582+
Formatter.PrintMessageAsJson(msg, hashContent);
576583
}
577584
}
578585

579-
586+
private static object Hash(object msgBody)
587+
{
588+
byte[] hash;
589+
SHA1 sha = new SHA1CryptoServiceProvider();
590+
if (msgBody is byte[])
591+
hash = sha.ComputeHash((byte[]) msgBody);
592+
else if (msgBody is string)
593+
hash = sha.ComputeHash(Encoding.UTF8.GetBytes((string) msgBody));
594+
else
595+
hash = sha.ComputeHash(Encoding.UTF8.GetBytes(FormatVariant(msgBody)));
596+
return String.Concat(hash.Select(b => b.ToString("x2")));
597+
}
580598
}
581599
}

ClientLib/OptionsParser.cs

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,15 @@ public void PrintHelp()
5252
Console.WriteLine("<type_of_client> [opts]");
5353
this.WriteOptionDescriptions(Console.Out);
5454
}
55+
56+
protected static bool ParseBoolOption(string durable)
57+
{
58+
if (durable == "yes" || durable == "true" || durable == "True")
59+
return true;
60+
if (durable == "no" || durable == "false" || durable == "False")
61+
return false;
62+
throw new ArgumentException();
63+
}
5564
}
5665

5766
/// <summary>
@@ -135,6 +144,7 @@ public abstract class BasicOptions : LinkOptions
135144
public string LogMsgs { get; protected set; }
136145
public string LogStats { get; protected set; }
137146
public string LogLib { get; protected set; }
147+
public bool HashContent { get; protected set; }
138148

139149

140150
/// <summary>
@@ -166,6 +176,8 @@ public BasicOptions() : base()
166176
(string logStats) => { this.LogStats = logStats; });
167177
this.Add("log-lib=", "client logging library level [TRANSPORT_FRM]",
168178
(string logLib) => { this.LogLib = logLib; });
179+
this.Add("msg-content-hashed=", "Display SHA-1 hash of message content in logged messages (yes/no)",
180+
(string hashContent) => { this.HashContent = ParseBoolOption(hashContent); });
169181
}
170182
}
171183

@@ -319,14 +331,7 @@ public SenderOptions() : base()
319331
this.Add("property-type=", "specify message property type (overrides auto-cast feature)",
320332
(string propertyType) => { this.PropertyType = propertyType; });
321333
this.Add("msg-durable=", "send durable messages yes/no",
322-
(string durable) => {
323-
if (((durable == "yes") || (durable == "true")) || (durable == "True"))
324-
this.Durable = true;
325-
else if (((durable == "no") || (durable == "false")) || (durable == "False"))
326-
this.Durable = false;
327-
else
328-
throw new ArgumentException();
329-
});
334+
(string durable) => { this.Durable = ParseBoolOption(durable); });
330335
this.Add("msg-ttl=", "message time-to-live (ms)",
331336
(uint ttl) => { this.Ttl = ttl; });
332337
this.Add("msg-priority=", "message priority",

ClientLibNetCore/Formatter.cs

Lines changed: 32 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
using System;
1818
using System.Collections.Generic;
1919
using System.Collections.ObjectModel;
20+
using System.Linq;
21+
using System.Security.Cryptography;
2022
using System.Text;
2123

2224
using Amqp;
@@ -39,15 +41,16 @@ public class Formatter
3941
/// Print message in upstream format
4042
/// </summary>
4143
/// <param name="msg">msg object</param>
42-
public static void PrintMessage(Message msg)
44+
/// <param name="hashContent"></param>
45+
public static void PrintMessage(Message msg, bool hashContent)
4346
{
4447
Console.WriteLine("Message(");
4548
if (msg.Header != null) Console.WriteLine(msg.Header.ToString());
4649
if (msg.DeliveryAnnotations != null) Console.WriteLine(msg.DeliveryAnnotations.ToString());
4750
if (msg.MessageAnnotations != null) Console.WriteLine(msg.MessageAnnotations.ToString());
4851
if (msg.Properties != null) Console.WriteLine(msg.Properties.ToString());
4952
if (msg.ApplicationProperties != null) Console.WriteLine(msg.ApplicationProperties.ToString());
50-
if (msg.Body != null) Console.WriteLine("body:{0}", msg.Body.ToString());
53+
if (msg.Body != null) Console.WriteLine("body:{0}", hashContent ? Hash(msg.Body.ToString()): msg.Body.ToString());
5154
if (msg.Footer != null) Console.WriteLine(msg.Footer.ToString());
5255
Console.WriteLine(")");
5356
}
@@ -56,7 +59,8 @@ public static void PrintMessage(Message msg)
5659
/// Print message as python dict
5760
/// </summary>
5861
/// <param name="msg">message object</param>
59-
public static void PrintMessageAsDict(Message msg)
62+
/// <param name="hashContent"></param>
63+
public static void PrintMessageAsDict(Message msg, bool hashContent)
6064
{
6165
Dictionary<string, object> msgDict = new Dictionary<string,object>();
6266
msgDict.Add("durable", msg.Header.Durable);
@@ -76,7 +80,7 @@ public static void PrintMessageAsDict(Message msg)
7680
msgDict.Add("group-id", msg.Properties.GroupId);
7781
msgDict.Add("group-sequence", msg.Properties.GroupSequence);
7882
msgDict.Add("reply-to-group-id", msg.Properties.ReplyToGroupId);
79-
msgDict.Add("content", msg.Body);
83+
msgDict.Add("content", hashContent ? Hash(msg.Body) : msg.Body);
8084
msgDict.Add("properties", msg.ApplicationProperties);
8185
msgDict.Add("message-annotations", msg.MessageAnnotations);
8286
Console.WriteLine(FormatMap(msgDict));
@@ -86,7 +90,8 @@ public static void PrintMessageAsDict(Message msg)
8690
/// Print message as python dict (keys are named by AMQP standard)
8791
/// </summary>
8892
/// <param name="msg">message object</param>
89-
public static void PrintMessageAsInterop(Message msg)
93+
/// <param name="hashContent"></param>
94+
public static void PrintMessageAsInterop(Message msg, bool hashContent)
9095
{
9196
Dictionary<string, object> msgDict = new Dictionary<string, object>();
9297
msgDict.Add("durable", msg.Header.Durable);
@@ -107,7 +112,7 @@ public static void PrintMessageAsInterop(Message msg)
107112
msgDict.Add("group-id", msg.Properties.GroupId);
108113
msgDict.Add("group-sequence", msg.Properties.GroupSequence);
109114
msgDict.Add("reply-to-group-id", msg.Properties.ReplyToGroupId);
110-
msgDict.Add("content", msg.Body);
115+
msgDict.Add("content", hashContent ? Hash(msg.Body) : msg.Body);
111116
msgDict.Add("properties", msg.ApplicationProperties);
112117
//msgDict.Add("message-annotations", msg.MessageAnnotations);
113118
Console.WriteLine(FormatMap(msgDict));
@@ -117,7 +122,8 @@ public static void PrintMessageAsInterop(Message msg)
117122
/// Print message as python dict (keys are named by AMQP standard)
118123
/// </summary>
119124
/// <param name="msg">message object</param>
120-
public static void PrintMessageAsJson(Message msg)
125+
/// <param name="hashContent"></param>
126+
public static void PrintMessageAsJson(Message msg, bool hashContent)
121127
{
122128
Dictionary<string, object> msgDict = new Dictionary<string, object>();
123129
msgDict.Add("durable", msg.Header.Durable);
@@ -138,7 +144,7 @@ public static void PrintMessageAsJson(Message msg)
138144
msgDict.Add("group-id", msg.Properties.GroupId);
139145
msgDict.Add("group-sequence", msg.Properties.GroupSequence);
140146
msgDict.Add("reply-to-group-id", msg.Properties.ReplyToGroupId);
141-
msgDict.Add("content", msg.Body);
147+
msgDict.Add("content", hashContent ? Hash(msg.Body) : msg.Body);
142148
msgDict.Add("properties", msg.ApplicationProperties);
143149
Console.WriteLine(JsonConvert.SerializeObject(msgDict));
144150
}
@@ -553,29 +559,41 @@ public static string FormatVariant(object inData)
553559
/// <param name="options">agruments of client</param>
554560
public static void LogMessage(Message msg, SenderReceiverOptions options)
555561
{
562+
var hashContent = options.HashContent;
556563

557564
if (options.LogMsgs == "body")
558565
{
559-
Console.WriteLine(msg.Body);
566+
Console.WriteLine(hashContent ? Hash(msg.Body) : msg.Body);
560567
}
561568
else if (options.LogMsgs.Equals("dict"))
562569
{
563-
Formatter.PrintMessageAsDict(msg);
570+
Formatter.PrintMessageAsDict(msg, hashContent);
564571
}
565572
else if (options.LogMsgs.Equals("upstream"))
566573
{
567-
Formatter.PrintMessage(msg);
574+
Formatter.PrintMessage(msg, hashContent);
568575
}
569576
else if (options.LogMsgs.Equals("interop"))
570577
{
571-
Formatter.PrintMessageAsInterop(msg);
578+
Formatter.PrintMessageAsInterop(msg, hashContent);
572579
}
573580
else if (options.LogMsgs.Equals("json"))
574581
{
575-
Formatter.PrintMessageAsJson(msg);
582+
Formatter.PrintMessageAsJson(msg, hashContent);
576583
}
577584
}
578585

579-
586+
private static object Hash(object msgBody)
587+
{
588+
byte[] hash;
589+
SHA1 sha = new SHA1CryptoServiceProvider();
590+
if (msgBody is byte[])
591+
hash = sha.ComputeHash((byte[]) msgBody);
592+
else if (msgBody is string)
593+
hash = sha.ComputeHash(Encoding.UTF8.GetBytes((string) msgBody));
594+
else
595+
hash = sha.ComputeHash(Encoding.UTF8.GetBytes(FormatVariant(msgBody)));
596+
return String.Concat(hash.Select(b => b.ToString("x2")));
597+
}
580598
}
581599
}

ClientLibNetCore/OptionsParser.cs

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,15 @@ public void PrintHelp()
5252
Console.WriteLine("<type_of_client> [opts]");
5353
this.WriteOptionDescriptions(Console.Out);
5454
}
55+
56+
protected static bool ParseBoolOption(string durable)
57+
{
58+
if (durable == "yes" || durable == "true" || durable == "True")
59+
return true;
60+
if (durable == "no" || durable == "false" || durable == "False")
61+
return false;
62+
throw new ArgumentException();
63+
}
5564
}
5665

5766
/// <summary>
@@ -135,6 +144,7 @@ public abstract class BasicOptions : LinkOptions
135144
public string LogMsgs { get; protected set; }
136145
public string LogStats { get; protected set; }
137146
public string LogLib { get; protected set; }
147+
public bool HashContent { get; protected set; }
138148

139149

140150
/// <summary>
@@ -166,6 +176,8 @@ public BasicOptions() : base()
166176
(string logStats) => { this.LogStats = logStats; });
167177
this.Add("log-lib=", "client logging library level [TRANSPORT_FRM]",
168178
(string logLib) => { this.LogLib = logLib; });
179+
this.Add("msg-content-hashed=", "Display SHA-1 hash of message content in logged messages (yes/no)",
180+
(string hashContent) => { this.HashContent = ParseBoolOption(hashContent); });
169181
}
170182
}
171183

@@ -319,14 +331,7 @@ public SenderOptions() : base()
319331
this.Add("property-type=", "specify message property type (overrides auto-cast feature)",
320332
(string propertyType) => { this.PropertyType = propertyType; });
321333
this.Add("msg-durable=", "send durable messages yes/no",
322-
(string durable) => {
323-
if (((durable == "yes") || (durable == "true")) || (durable == "True"))
324-
this.Durable = true;
325-
else if (((durable == "no") || (durable == "false")) || (durable == "False"))
326-
this.Durable = false;
327-
else
328-
throw new ArgumentException();
329-
});
334+
(string durable) => { this.Durable = ParseBoolOption(durable); });
330335
this.Add("msg-ttl=", "message time-to-live (ms)",
331336
(uint ttl) => { this.Ttl = ttl; });
332337
this.Add("msg-priority=", "message priority",

ClientUnitTests/SendReceiveTests.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,13 @@ public void TestSendReceiveStringContent()
9696
Assert.AreEqual(0, this.clientRunner.RunReceiver("--address send_receive_string_example --count 1"));
9797
}
9898

99+
[Test]
100+
public void TestSendReceiveHashedContent()
101+
{
102+
Assert.AreEqual(0, this.clientRunner.RunSender("--address send_receive_hashed_example --count 1 --msg-content aContent --log-msgs json --msg-content-hashed yes"));
103+
Assert.AreEqual(0, this.clientRunner.RunReceiver("--address send_receive_hashed_example --count 1 --log-msgs json --msg-content-hashed yes"));
104+
}
105+
99106
[Test]
100107
public void TestDrainEmptyQueue()
101108
{

0 commit comments

Comments
 (0)