Bien señores, basicamente mi clase implementa MD5, Base64 y una para generar ID encriptados únicos. Veamos la clase:
using System;
using System.Collections.Generic;
using System.Text;
using System.Security.Cryptography;
using System.IO;
namespace WebApplication1.Helper
{
public static class HashHelper
{
public static string MD5(string word)
{
MD5 md5 = MD5CryptoServiceProvider.Create();
ASCIIEncoding encoding = new ASCIIEncoding();
byte[] stream = null;
StringBuilder sb = new StringBuilder();
stream = md5.ComputeHash(encoding.GetBytes(word));
for (int i = 0; i < stream.Length; i++) sb.AppendFormat("{0:x2}", stream[i]);
return sb.ToString();
}
public static string Token()
{
long i = 1;
foreach (byte b in Guid.NewGuid().ToByteArray()) i *= ((int)b + 1);
return MD5(string.Format("{0:x}", i - DateTime.Now.Ticks));
}
public static string Base64Encode(string word)
{
byte[] byt = System.Text.Encoding.UTF8.GetBytes(word);
return Convert.ToBase64String(byt);
}
public static string Base64Decode(string word)
{
byte[] b = Convert.FromBase64String(word);
return System.Text.Encoding.UTF8.GetString(b);
}
public static string SHA1(string str)
{
SHA1 sha1 = SHA1Managed.Create();
ASCIIEncoding encoding = new ASCIIEncoding();
byte[] stream = null;
StringBuilder sb = new StringBuilder();
stream = sha1.ComputeHash(encoding.GetBytes(str));
for (int i = 0; i < stream.Length; i++) sb.AppendFormat("{0:x2}", stream[i]);
return sb.ToString();
}
public static string SHA256(string str)
{
SHA256 sha256 = SHA256Managed.Create();
ASCIIEncoding encoding = new ASCIIEncoding();
byte[] stream = null;
StringBuilder sb = new StringBuilder();
stream = sha256.ComputeHash(encoding.GetBytes(str));
for (int i = 0; i < stream.Length; i++) sb.AppendFormat("{0:x2}", stream[i]);
return sb.ToString();
}
public static string SHA384(string str)
{
SHA384 sha384 = SHA384Managed.Create();
ASCIIEncoding encoding = new ASCIIEncoding();
byte[] stream = null;
StringBuilder sb = new StringBuilder();
stream = sha384.ComputeHash(encoding.GetBytes(str));
for (int i = 0; i < stream.Length; i++) sb.AppendFormat("{0:x2}", stream[i]);
return sb.ToString();
}
public static string SHA512(string str)
{
SHA512 sha512 = SHA512Managed.Create();
ASCIIEncoding encoding = new ASCIIEncoding();
byte[] stream = null;
StringBuilder sb = new StringBuilder();
stream = sha512.ComputeHash(encoding.GetBytes(str));
for (int i = 0; i < stream.Length; i++) sb.AppendFormat("{0:x2}", stream[i]);
return sb.ToString();
}
}
}
Métodos que implementa
-
MD5: recibe un string que nos permite convertirlo a MD5. Este algoritmo, en teoría no se puede desencriptar.
-
token: nos generá un valor único encriptado en MD5, cada vez que llames al método, este va a generar un nuevo ID.
-
Base64Encode: este permite encriptar a Base64 cualquier cadena ingresada, pero se puede desencriptar usando el método Base64Decode.
-
SHA1: recibe un string que nos permite convertirlo a SHA1.
-
SHA256: recibe un string que nos permite convertirlo a SHA256.
-
SHA384: recibe un string que nos permite convertirlo a SHA384.
-
SHA512: recibe un string que nos permite convertirlo a SHA512.
PD: si tienes más algoritmos, comentalos para ir agregándolos.