Last active
September 6, 2016 18:15
-
-
Save rymoore99/2a5125aefe8c5fbc4181 to your computer and use it in GitHub Desktop.
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
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
using System.Threading.Tasks; | |
using System.Security.Cryptography; | |
using System.Web.Script.Serialization; | |
public class MuutHelper | |
{ | |
#region Instance Fields | |
private readonly string _apiKey; | |
private readonly string _secretKey; | |
private readonly string _timeStamp; | |
private string _message; | |
private string _userAvatar; | |
private string _userDisplayName; | |
private string _userEmail; | |
private string _userID; | |
private bool _userIsAdmin; | |
#endregion | |
#region Constructors | |
public MuutHelper(string apiKey, string secretKey) | |
{ | |
_timeStamp = (DateTime.UtcNow - new DateTime(1970, 1, 1)).TotalSeconds.ToString(); | |
_apiKey = apiKey; | |
_secretKey = secretKey; | |
_userAvatar = string.Empty; | |
_userDisplayName = string.Empty; | |
_userID = string.Empty; | |
_userIsAdmin = false; | |
this.GenerateMessage(); | |
} | |
#endregion | |
#region Properties | |
public string ApiKey | |
{ | |
get | |
{ | |
return _apiKey; | |
} | |
} | |
public string Message | |
{ | |
get | |
{ | |
return _message; | |
} | |
} | |
public string Signature | |
{ | |
get | |
{ | |
var encoder = new ASCIIEncoding(); | |
var messageBytes = encoder.GetBytes(_secretKey + " " + _message + " " + _timeStamp); | |
var sha1 = new SHA1Managed(); | |
var hashValue = sha1.ComputeHash(messageBytes); | |
return hashValue.Aggregate("", (current, b) => current + String.Format("{0:x2}", b)); | |
} | |
} | |
public string TimeStamp | |
{ | |
get | |
{ | |
return _timeStamp; | |
} | |
} | |
#endregion | |
#region Methods and Operators | |
#region public | |
public void SetUser(string id, string displayname, string email, string avatar, bool isAdmin) | |
{ | |
_userID = id; | |
_userDisplayName = displayname; | |
_userAvatar = avatar; | |
_userIsAdmin = isAdmin; | |
_userEmail = email; | |
this.GenerateMessage(); | |
} | |
#endregion | |
#region private | |
private void GenerateMessage() | |
{ | |
var serializer = new JavaScriptSerializer(); | |
var usr = serializer.Serialize( | |
new | |
{ | |
user = new | |
{ | |
displayname = _userDisplayName, | |
id = _userID, | |
email = _userEmail, | |
avatar = _userAvatar, | |
is_admin = _userIsAdmin | |
} | |
}); | |
var encodedbytes = ASCIIEncoding.ASCII.GetBytes(usr); | |
_message = Convert.ToBase64String(encodedbytes); | |
} | |
#endregion | |
#endregion | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment