[DLL] HashAlgorithm 1.0.0.1
Publicado: 22 Mar 2013 14:21
Información:
Buenas amigos de AMSSpecialist despues de mucho tiempo sin tocar Visual Studio he retomado algunos proyectos que tenia a medio hacer, en este caso os traigo una versión evolucionada de la dll HashMD5 pero con algunas funciones mas. Esta DLL funciona perfectamente a excepcion del algoritmo SHA que me trae de cabeza con sus putos randoms... tengo que mirarla bien, espero que os guste y la disfrutéis....
Funciones:
Código::
Enlaces relacionados:
Dll: HashAlgorithm (Basada en HashMD5)
Descarga:
Buenas amigos de AMSSpecialist despues de mucho tiempo sin tocar Visual Studio he retomado algunos proyectos que tenia a medio hacer, en este caso os traigo una versión evolucionada de la dll HashMD5 pero con algunas funciones mas. Esta DLL funciona perfectamente a excepcion del algoritmo SHA que me trae de cabeza con sus putos randoms... tengo que mirarla bien, espero que os guste y la disfrutéis....
Funciones:
- GetMD5String
- GetMD5File
- GetCRC32String
- GetCRC32File
- VerifyMD5String
- VerifyMD5File
- VerifyCRC32String
- VerifyCRC32File
Código::
using System;
using System.IO;
using System.Text;
using System.Collections.Generic;
using System.Security.Cryptography;
using RGiesecke.DllExport;
using CRC32CS;
using SHACS;
namespace CeoneHashAlgorithm
{
internal static class UnmanagedExports
{
/////////////////////////////////////////////
// G E T //
/////////////////////////////////////////////
// Function to get MD5Hash from string
[DllExport("GetMD5String", CallingConvention = System.Runtime.InteropServices.CallingConvention.StdCall)]
static string GetMD5String(string input)
{
MD5CryptoServiceProvider md5Hasher = new MD5CryptoServiceProvider();
byte[] data = md5Hasher.ComputeHash(Encoding.Default.GetBytes(input));
StringBuilder sBuilder = new StringBuilder();
for (int i = 0; i < data.Length; i++)
{
sBuilder.Append(data.ToString("x2"));
}
return sBuilder.ToString();
}
// Function to get MD5Hash from FilePath
[DllExport("GetMD5File", CallingConvention = System.Runtime.InteropServices.CallingConvention.StdCall)]
static string GetMD5File(string filepath)
{
StreamReader sr = new StreamReader(filepath);
MD5CryptoServiceProvider md5Hasher = new MD5CryptoServiceProvider();
byte[] data = md5Hasher.ComputeHash(Encoding.Default.GetBytes(sr.ReadToEnd()));
StringBuilder sBuilder = new StringBuilder();
for (int i = 0; i < data.Length; i++)
{
sBuilder.Append(data.ToString("x2"));
}
sr.Close();
return sBuilder.ToString();
}
// Function to get CRC32hash from FilePath
[DllExport("GetCRC32String", CallingConvention = System.Runtime.InteropServices.CallingConvention.StdCall)]
static string GetCRC32String(string stringValue, int format)
{
Crc32 crc32 = new Crc32();
String hash = String.Empty;
byte[] plainTextBytes = Encoding.UTF8.GetBytes(stringValue);
if (format == 1)
{
foreach (byte b in crc32.ComputeHash(plainTextBytes)) hash += b.ToString("x2").ToLower();
}
else if (format == 2)
{
foreach (byte b in crc32.ComputeHash(plainTextBytes)) hash += b.ToString("x2").ToUpper();
}
return hash;
}
// Function to get CRC32hash from FilePath
[DllExport("GetCRC32File", CallingConvention = System.Runtime.InteropServices.CallingConvention.StdCall)]
static string GetCRC32File(string stringValue, int format)
{
Crc32 crc32 = new Crc32();
String hash = String.Empty;
if (format == 1)
{
FileStream fs = File.Open(stringValue, FileMode.Open);
foreach (byte b in crc32.ComputeHash(fs)) hash += b.ToString("x2").ToLower();
fs.Close();
}
else if (format == 2)
{
FileStream fs = File.Open(stringValue, FileMode.Open);
foreach (byte b in crc32.ComputeHash(fs)) hash += b.ToString("x2").ToUpper();
fs.Close();
}
return hash;
}
// Function to get SHAHash from String
[DllExport("GetSHAString", CallingConvention = System.Runtime.InteropServices.CallingConvention.StdCall)]
static string GetSHAString(string plainText, string hashAlgorithm)
{
string plainTextBytes = "myP@5sw0rd";
//hashAlgorithm = "SHA1","SHA256","SHA384" or "SHA512"
string hash = SimpleHash.ComputeHash(plainTextBytes, hashAlgorithm, null);
return hash.ToString();
}
// Function to get SHAHash from FilePath
[DllExport("GetSHAFile", CallingConvention = System.Runtime.InteropServices.CallingConvention.StdCall)]
static string GetSHAFile(string filepath, string hashAlgorithm)
{
using (StreamReader sr = new StreamReader(filepath))
{
//hashAlgorithm = "SHA1","SHA256","SHA384" or "SHA512"
string hash = SimpleHash.ComputeHash(sr.ReadToEnd(), hashAlgorithm, null);
sr.Close();
return hash.ToString();
}
}
/////////////////////////////////////////////
// V E R I F Y //
/////////////////////////////////////////////
// Function verify MD5Hash from string
[DllExport("VerifyMD5String", CallingConvention = System.Runtime.InteropServices.CallingConvention.StdCall)]
static int VerifyMD5String(string input, string hash)
{
MD5CryptoServiceProvider md5Hasher = new MD5CryptoServiceProvider();
byte[] data = md5Hasher.ComputeHash(Encoding.Default.GetBytes(input));
StringBuilder sBuilder = new StringBuilder();
for (int i = 0; i < data.Length; i++)
{
sBuilder.Append(data.ToString("x2"));
}
string hashOfInput = sBuilder.ToString();
StringComparer comparer = StringComparer.OrdinalIgnoreCase;
if (hash == hashOfInput)
{
return 1;
}
else
{
return 0;
}
}
[DllExport("VerifyMD5File", CallingConvention = System.Runtime.InteropServices.CallingConvention.StdCall)]
static int VerifyMD5File(string filepath, string hashvalue)
{
MD5CryptoServiceProvider md5Hasher = new MD5CryptoServiceProvider();
FileStream fs = File.Open(filepath, FileMode.Open);
byte[] data = md5Hasher.ComputeHash(fs);
StringBuilder sBuilder = new StringBuilder();
for (int i = 0; i < data.Length; i++)
{
sBuilder.Append(data.ToString("x2"));
}
fs.Close();
string hashOfInput = sBuilder.ToString();
if (hashOfInput == hashvalue)
{
return 1;
}
else
{
return 0;
}
}
[DllExport("VerifyCRC32String", CallingConvention = System.Runtime.InteropServices.CallingConvention.StdCall)]
static int VerifyCRC32String(string stringValue, string vhash, int format)
{
Crc32 crc32 = new Crc32();
String hash = String.Empty;
byte[] rawBytes = System.Text.ASCIIEncoding.ASCII.GetBytes(stringValue);
if (format == 1)
{
foreach (byte b in crc32.ComputeHash(rawBytes)) hash += b.ToString("x2").ToLower();
}
else if (format == 2)
{
foreach (byte b in crc32.ComputeHash(rawBytes)) hash += b.ToString("x2").ToUpper();
}
if (hash == vhash)
{
return 1;
}
else
{
return 0;
}
}
[DllExport("VerifyCRC32File", CallingConvention = System.Runtime.InteropServices.CallingConvention.StdCall)]
static int VerifyCRC32File(string filepath, string vhash, int format)
{
Crc32 crc32 = new Crc32();
String hash = String.Empty;
FileStream fs = File.Open(filepath, FileMode.Open);
if (format == 1)
{
foreach (byte b in crc32.ComputeHash(fs)) hash += b.ToString("x2").ToLower();
fs.Close();
}
else if (format == 2)
{
foreach (byte b in crc32.ComputeHash(fs)) hash += b.ToString("x2").ToUpper();
fs.Close();
}
if (hash == vhash)
{
return 1;
}
else
{
return 0;
}
}
// Function to verify SHAHash from String
[DllExport("VerifySHAString", CallingConvention = System.Runtime.InteropServices.CallingConvention.StdCall)]
static int VerifySHAString(string plainText, string vhash, string hashAlgorithm)
{
//hashAlgorithm = "SHA1","SHA256","SHA384" or "SHA512"
string hash = SimpleHash.ComputeHash(plainText, hashAlgorithm, null);
if (hash == vhash)
{
return 1;
}
else
{
return 0;
}
}
// Function to Verify SHAHash from FilePath
[DllExport("VerifySHAFile", CallingConvention = System.Runtime.InteropServices.CallingConvention.StdCall)]
static int VerifySHAFile(string filepathA, string vhash, string hashAlgorithm)
{
StreamReader sr = new StreamReader(filepathA);
//hashAlgorithm = "SHA1","SHA256","SHA384" or "SHA512"
string hash = SimpleHash.ComputeHash(sr.ReadToEnd(), hashAlgorithm, null);
sr.Close();
if (hash == vhash)
{
return 1;
}
else
{
return 0;
}
}
}
}
Enlaces relacionados:
Dll: HashAlgorithm (Basada en HashMD5)
Descarga: