Skip to content

Commit 4ad07e4

Browse files
committed
NEW --msg-content-hashed argument
1 parent b6082e4 commit 4ad07e4

File tree

4 files changed

+70
-28
lines changed

4 files changed

+70
-28
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: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,7 @@ public abstract class BasicOptions : LinkOptions
144144
public string LogMsgs { get; protected set; }
145145
public string LogStats { get; protected set; }
146146
public string LogLib { get; protected set; }
147+
public bool HashContent { get; protected set; }
147148

148149

149150
/// <summary>
@@ -175,6 +176,8 @@ public BasicOptions() : base()
175176
(string logStats) => { this.LogStats = logStats; });
176177
this.Add("log-lib=", "client logging library level [TRANSPORT_FRM]",
177178
(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); });
178181
}
179182
}
180183

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: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,7 @@ public abstract class BasicOptions : LinkOptions
144144
public string LogMsgs { get; protected set; }
145145
public string LogStats { get; protected set; }
146146
public string LogLib { get; protected set; }
147+
public bool HashContent { get; protected set; }
147148

148149

149150
/// <summary>
@@ -175,6 +176,8 @@ public BasicOptions() : base()
175176
(string logStats) => { this.LogStats = logStats; });
176177
this.Add("log-lib=", "client logging library level [TRANSPORT_FRM]",
177178
(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); });
178181
}
179182
}
180183

0 commit comments

Comments
 (0)