Skip to content

Commit

Permalink
enhancing so user doesn't have to generate their own signature string
Browse files Browse the repository at this point in the history
  • Loading branch information
slorello89 committed Oct 10, 2019
1 parent 32265d4 commit 890e26a
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 7 deletions.
8 changes: 3 additions & 5 deletions Nexmo.Api/Cryptography/SmsSignatureGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,20 +20,18 @@ public enum Method
}
public static string GenerateSignature(string query, string securitySecret, Method method)
{
var queryToSign = "&" + query;
queryToSign = queryToSign.Remove(queryToSign.Length - 1);
// security secret provided, sort and sign request
if (method == Method.md5hash)
{
queryToSign += securitySecret;
query += securitySecret;
var hashgen = MD5.Create();
var hash = hashgen.ComputeHash(Encoding.UTF8.GetBytes(queryToSign));
var hash = hashgen.ComputeHash(Encoding.UTF8.GetBytes(query));
return ByteArrayToHexHelper.ByteArrayToHex(hash).ToLower();
}
else
{
var securityBytes = Encoding.UTF8.GetBytes(securitySecret);
var input = Encoding.UTF8.GetBytes(queryToSign);
var input = Encoding.UTF8.GetBytes(query);
HMAC hmacGen = new HMACMD5(securityBytes);
switch (method)
{
Expand Down
4 changes: 3 additions & 1 deletion Nexmo.Api/Request/ApiRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,9 @@ private static StringBuilder BuildQueryString(IDictionary<string, string> parame
var sortedParams = new SortedDictionary<string, string>(parameters);
buildStringFromParams(sortedParams, sb);
buildSignatureStringFromParams(sortedParams, signature_sb);
var signature = SmsSignatureGenerator.GenerateSignature(signature_sb.ToString(), securitySecret, method);
var queryToSign = "&" + signature_sb.ToString();
queryToSign = queryToSign.Remove(queryToSign.Length - 1);
var signature = SmsSignatureGenerator.GenerateSignature(queryToSign, securitySecret, method);
sb.AppendFormat("sig={0}", signature);
return sb;
}
Expand Down
38 changes: 37 additions & 1 deletion Nexmo.Api/SMS.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using Microsoft.AspNetCore.Http;
using Newtonsoft.Json;
using Nexmo.Api.Request;

Expand Down Expand Up @@ -301,7 +306,7 @@ public class SMSInbound
/// The time at UTC±00:00 that Nexmo started to push this inbound message to your webhook endpoint. The message-timestamp is in the following format YYYY-MM-DD HH:MM:SS. For example, 2020-01-01 12:00:00.
/// </summary>
[JsonProperty("message-timestamp")]
public DateTime message_timestamp { get; set; }
public string message_timestamp { get; set; }
/// <summary>
/// A unix timestamp representation of message-timestamp.
/// </summary>
Expand Down Expand Up @@ -359,6 +364,37 @@ public class SMSInbound
/// Signature if Applicable
/// </summary>
public string sig { get; set; }

/// <summary>
/// converts dictionary into properly formatted signature string
/// </summary>
/// <param name="query"></param>
/// <returns></returns>
public static string ConstructSignatureStringFromDictionary(Dictionary<string,string> query)
{
try
{
var sig_sb = new StringBuilder();
var sorted_dict = new SortedDictionary<string, string>(StringComparer.Ordinal);
foreach (var key in query.Keys)
{
sorted_dict.Add(key, query[key].ToString());
}
foreach (var key in sorted_dict.Keys)
{
if (key == "sig")
{
continue;
}
sig_sb.AppendFormat("&{0}={1}", key.Replace('=', '_').Replace('&', '_'), sorted_dict[key].ToString().Replace('=', '_').Replace('&', '_'));
}
return sig_sb.ToString();
}
catch
{
return "";
}
}
}

/// <summary>
Expand Down

0 comments on commit 890e26a

Please sign in to comment.