This repository has been archived by the owner on Sep 10, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 61
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Issue #17: Sending signed messages back to jupyter
- Loading branch information
Showing
13 changed files
with
269 additions
and
84 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
| ||
namespace iCSharp.Kernel.Helpers | ||
{ | ||
using iCSharp.Messages; | ||
using NetMQ; | ||
|
||
public interface IMessageSender | ||
{ | ||
bool Send(Message message, NetMQSocket socket); | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
| ||
namespace iCSharp.Kernel.Helpers | ||
{ | ||
using System; | ||
using iCSharp.Messages; | ||
|
||
public interface ISignatureValidator | ||
{ | ||
/// <summary> | ||
/// Creates the signature. | ||
/// </summary> | ||
/// <returns>The signature.</returns> | ||
/// <param name="message">Message.</param> | ||
string CreateSignature(Message message); | ||
|
||
/// <summary> | ||
/// Determines whether this instance is valid signature the specified message. | ||
/// </summary> | ||
/// <returns><c>true</c> if this instance is valid signature the specified message; otherwise, <c>false</c>.</returns> | ||
/// <param name="message">Message.</param> | ||
bool IsValidSignature(Message message); | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
| ||
|
||
namespace iCSharp.Kernel | ||
namespace iCSharp.Kernel.Helpers | ||
{ | ||
using System; | ||
using iCSharp.Messages; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
| ||
namespace iCSharp.Kernel.Helpers | ||
{ | ||
using Common.Serializer; | ||
using iCSharp.Messages; | ||
using NetMQ; | ||
|
||
public class MessageSender : IMessageSender | ||
{ | ||
private const string Delimeter = "<IDS|MSG>"; | ||
|
||
private readonly ISignatureValidator _signatureValidator; | ||
|
||
public MessageSender(ISignatureValidator signatureValidator) | ||
{ | ||
this._signatureValidator = signatureValidator; | ||
} | ||
|
||
public bool Send(Message message, NetMQSocket socket) | ||
{ | ||
string hmac = this._signatureValidator.CreateSignature (message); | ||
|
||
Send(message.UUID, socket); | ||
Send(Delimeter, socket); | ||
Send(hmac, socket); | ||
Send(JsonSerializer.Serialize(message.Header), socket); | ||
Send(JsonSerializer.Serialize(message.ParentHeader), socket); | ||
Send(JsonSerializer.Serialize(message.MetaData), socket); | ||
Send(message.Content, socket, false); | ||
|
||
return true; | ||
} | ||
|
||
private void Send(string message, NetMQSocket socket, bool sendMore = true) | ||
{ | ||
socket.Send(message, false, sendMore); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
| ||
namespace iCSharp.Kernel.Helpers | ||
{ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Security.Cryptography; | ||
using System.Text; | ||
using Common.Logging; | ||
using Common.Serializer; | ||
using iCSharp.Messages; | ||
|
||
|
||
public class SignatureValidator : ISignatureValidator | ||
{ | ||
private readonly ILog _logger; | ||
|
||
private readonly HMAC _signatureGenerator; | ||
|
||
private readonly Encoding _encoder; | ||
/// <summary> | ||
/// Initializes a new instance of the <see cref="iCSharp.Kernel.Helpers.Sha256SignatureValidator"/> class. | ||
/// </summary> | ||
/// <param name="key">Shared key used to initialize the digest.</param> | ||
public SignatureValidator (ILog logger, string key, string algorithm) | ||
{ | ||
this._logger = logger; | ||
this._encoder = new UTF8Encoding (); | ||
|
||
this._signatureGenerator = HMAC.Create (algorithm); | ||
this._signatureGenerator.Key = this._encoder.GetBytes (key); | ||
} | ||
|
||
#region ISignatureValidator implementation | ||
|
||
/// <summary> | ||
/// Creates the signature. | ||
/// </summary> | ||
/// <returns>The signature.</returns> | ||
/// <param name="message">Message.</param> | ||
public string CreateSignature (Message message) | ||
{ | ||
this._signatureGenerator.Initialize (); | ||
|
||
List<string> messages = this.GetMessagesToAddForDigest (message); | ||
|
||
// For all items update the signature | ||
foreach (string item in messages) { | ||
byte[] sourceBytes = this._encoder.GetBytes (item); | ||
this._signatureGenerator.TransformBlock (sourceBytes, 0, sourceBytes.Length, null, 0); | ||
} | ||
|
||
this._signatureGenerator.TransformFinalBlock (new byte[0], 0, 0); | ||
|
||
// Calculate the digest and remove - | ||
return BitConverter.ToString(this._signatureGenerator.Hash).Replace("-","").ToLower(); | ||
} | ||
|
||
/// <summary> | ||
/// Determines whether this instance is valid signature the specified message. | ||
/// </summary> | ||
/// <returns>true</returns> | ||
/// <c>false</c> | ||
/// <param name="message">Message.</param> | ||
public bool IsValidSignature (Message message) | ||
{ | ||
string calculatedSignature = this.CreateSignature (message); | ||
this._logger.Info (string.Format ("Expected Signature: {0}", calculatedSignature)); | ||
return string.Equals (message.HMac, calculatedSignature, StringComparison.OrdinalIgnoreCase); | ||
} | ||
|
||
#endregion | ||
|
||
/// <summary> | ||
/// Gets the messages to add for digest. | ||
/// </summary> | ||
/// <returns>The messages to add for digest.</returns> | ||
/// <param name="message">Message.</param> | ||
private List<string> GetMessagesToAddForDigest(Message message) | ||
{ | ||
if (message == null) | ||
{ | ||
return new List<string> (); | ||
} | ||
|
||
return new List<string>(){ | ||
JsonSerializer.Serialize(message.Header), | ||
JsonSerializer.Serialize(message.ParentHeader), | ||
JsonSerializer.Serialize(message.MetaData), | ||
message.Content | ||
}; | ||
} | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.