17
17
using System ;
18
18
using System . Collections . Generic ;
19
19
using System . Collections . ObjectModel ;
20
+ using System . Linq ;
21
+ using System . Security . Cryptography ;
20
22
using System . Text ;
21
23
22
24
using Amqp ;
@@ -39,15 +41,16 @@ public class Formatter
39
41
/// Print message in upstream format
40
42
/// </summary>
41
43
/// <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 )
43
46
{
44
47
Console . WriteLine ( "Message(" ) ;
45
48
if ( msg . Header != null ) Console . WriteLine ( msg . Header . ToString ( ) ) ;
46
49
if ( msg . DeliveryAnnotations != null ) Console . WriteLine ( msg . DeliveryAnnotations . ToString ( ) ) ;
47
50
if ( msg . MessageAnnotations != null ) Console . WriteLine ( msg . MessageAnnotations . ToString ( ) ) ;
48
51
if ( msg . Properties != null ) Console . WriteLine ( msg . Properties . ToString ( ) ) ;
49
52
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 ( ) ) ;
51
54
if ( msg . Footer != null ) Console . WriteLine ( msg . Footer . ToString ( ) ) ;
52
55
Console . WriteLine ( ")" ) ;
53
56
}
@@ -56,7 +59,8 @@ public static void PrintMessage(Message msg)
56
59
/// Print message as python dict
57
60
/// </summary>
58
61
/// <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 )
60
64
{
61
65
Dictionary < string , object > msgDict = new Dictionary < string , object > ( ) ;
62
66
msgDict . Add ( "durable" , msg . Header . Durable ) ;
@@ -76,7 +80,7 @@ public static void PrintMessageAsDict(Message msg)
76
80
msgDict . Add ( "group-id" , msg . Properties . GroupId ) ;
77
81
msgDict . Add ( "group-sequence" , msg . Properties . GroupSequence ) ;
78
82
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 ) ;
80
84
msgDict . Add ( "properties" , msg . ApplicationProperties ) ;
81
85
msgDict . Add ( "message-annotations" , msg . MessageAnnotations ) ;
82
86
Console . WriteLine ( FormatMap ( msgDict ) ) ;
@@ -86,7 +90,8 @@ public static void PrintMessageAsDict(Message msg)
86
90
/// Print message as python dict (keys are named by AMQP standard)
87
91
/// </summary>
88
92
/// <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 )
90
95
{
91
96
Dictionary < string , object > msgDict = new Dictionary < string , object > ( ) ;
92
97
msgDict . Add ( "durable" , msg . Header . Durable ) ;
@@ -107,7 +112,7 @@ public static void PrintMessageAsInterop(Message msg)
107
112
msgDict . Add ( "group-id" , msg . Properties . GroupId ) ;
108
113
msgDict . Add ( "group-sequence" , msg . Properties . GroupSequence ) ;
109
114
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 ) ;
111
116
msgDict . Add ( "properties" , msg . ApplicationProperties ) ;
112
117
//msgDict.Add("message-annotations", msg.MessageAnnotations);
113
118
Console . WriteLine ( FormatMap ( msgDict ) ) ;
@@ -117,7 +122,8 @@ public static void PrintMessageAsInterop(Message msg)
117
122
/// Print message as python dict (keys are named by AMQP standard)
118
123
/// </summary>
119
124
/// <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 )
121
127
{
122
128
Dictionary < string , object > msgDict = new Dictionary < string , object > ( ) ;
123
129
msgDict . Add ( "durable" , msg . Header . Durable ) ;
@@ -138,7 +144,7 @@ public static void PrintMessageAsJson(Message msg)
138
144
msgDict . Add ( "group-id" , msg . Properties . GroupId ) ;
139
145
msgDict . Add ( "group-sequence" , msg . Properties . GroupSequence ) ;
140
146
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 ) ;
142
148
msgDict . Add ( "properties" , msg . ApplicationProperties ) ;
143
149
Console . WriteLine ( JsonConvert . SerializeObject ( msgDict ) ) ;
144
150
}
@@ -553,29 +559,41 @@ public static string FormatVariant(object inData)
553
559
/// <param name="options">agruments of client</param>
554
560
public static void LogMessage ( Message msg , SenderReceiverOptions options )
555
561
{
562
+ var hashContent = options . HashContent ;
556
563
557
564
if ( options . LogMsgs == "body" )
558
565
{
559
- Console . WriteLine ( msg . Body ) ;
566
+ Console . WriteLine ( hashContent ? Hash ( msg . Body ) : msg . Body ) ;
560
567
}
561
568
else if ( options . LogMsgs . Equals ( "dict" ) )
562
569
{
563
- Formatter . PrintMessageAsDict ( msg ) ;
570
+ Formatter . PrintMessageAsDict ( msg , hashContent ) ;
564
571
}
565
572
else if ( options . LogMsgs . Equals ( "upstream" ) )
566
573
{
567
- Formatter . PrintMessage ( msg ) ;
574
+ Formatter . PrintMessage ( msg , hashContent ) ;
568
575
}
569
576
else if ( options . LogMsgs . Equals ( "interop" ) )
570
577
{
571
- Formatter . PrintMessageAsInterop ( msg ) ;
578
+ Formatter . PrintMessageAsInterop ( msg , hashContent ) ;
572
579
}
573
580
else if ( options . LogMsgs . Equals ( "json" ) )
574
581
{
575
- Formatter . PrintMessageAsJson ( msg ) ;
582
+ Formatter . PrintMessageAsJson ( msg , hashContent ) ;
576
583
}
577
584
}
578
585
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
+ }
580
598
}
581
599
}
0 commit comments