-
Notifications
You must be signed in to change notification settings - Fork 86
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
140 additions
and
15 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
using Nexmo.Api.Request; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Security.Cryptography; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
namespace Nexmo.Api.Cryptography | ||
{ | ||
public class SmsSignatureGenerator | ||
{ | ||
public enum Method | ||
{ | ||
md5hash, | ||
md5, | ||
sha1, | ||
sha256, | ||
sha512 | ||
} | ||
public static string GenerateSignature(string query, string securitySecret, Method method) | ||
{ | ||
// security secret provided, sort and sign request | ||
if (method == Method.md5hash) | ||
{ | ||
query += securitySecret; | ||
var hashgen = MD5.Create(); | ||
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(query); | ||
HMAC hmacGen = new HMACMD5(securityBytes); | ||
switch (method) | ||
{ | ||
case Method.md5: | ||
hmacGen = new HMACMD5(securityBytes); | ||
break; | ||
case Method.sha1: | ||
hmacGen = new HMACSHA1(securityBytes); | ||
break; | ||
case Method.sha256: | ||
hmacGen = new HMACSHA256(securityBytes); | ||
break; | ||
case Method.sha512: | ||
hmacGen = new HMACSHA512(securityBytes); | ||
break; | ||
} | ||
var hmac = hmacGen.ComputeHash(input); | ||
var sig = ByteArrayToHexHelper.ByteArrayToHex(hmac).ToUpper(); | ||
return sig; | ||
} | ||
} | ||
} | ||
} |
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
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
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