Skip to content

Instantly share code, notes, and snippets.

@nukedbit
Created March 1, 2016 22:37
Show Gist options
  • Save nukedbit/a9a06a8d615b94f082d1 to your computer and use it in GitHub Desktop.
Save nukedbit/a9a06a8d615b94f082d1 to your computer and use it in GitHub Desktop.
Generazione di token da username password
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