Created
March 1, 2016 22:37
-
-
Save nukedbit/a9a06a8d615b94f082d1 to your computer and use it in GitHub Desktop.
Generazione di token da username password
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
public class TokenGenerator | |
{ | |
private const string Alg = "HmacSHA256"; | |
public string Generate(string username, string password, string ip, string userAgent, long ticks) | |
{ | |
string hash = string.Join(":", username, ip, userAgent, ticks.ToString()); | |
string hashLeft = ""; | |
string hashRight = ""; | |
using (HMAC hmac = HMAC.Create(Alg)) | |
{ | |
hmac.Key = Encoding.UTF8.GetBytes(Utils.HashPassword(password)); | |
hmac.ComputeHash(Encoding.UTF8.GetBytes(hash)); | |
hashLeft = Convert.ToBase64String(hmac.Hash); | |
hashRight = string.Join(":", username, ticks.ToString()); | |
} | |
return Convert.ToBase64String(Encoding.UTF8.GetBytes(string.Join(":", hashLeft, hashRight))); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment