Hola amigos de AMSSpecialist.info, quiero presentaros la segunda DLL que he creado, es una dll muy simple y limitada en cierto aspectos, pero bueno es lo que diríamos practicas con Visual Studio. Como el nombre bien describe esta DLL es para conseguir el hash MD5 de un string o archivo y verificarlo. Se detectáis algún error o tenéis alguna duda reportarme-lo en este post gracias!!!
c#
Lua
Descarga:
Re_UpLoaded On dropbox.com Server .... by abood 14/11/2012
c#
using System;
using System.Collections.Generic;
using System.Security.Cryptography;
using System.Text;
using System.IO;
using RGiesecke.DllExport;
namespace Hash
{
internal static class UnmanagedExports
{
// Function to get MD5Hash from string
[DllExport("GetMD5HashfromString", CallingConvention = System.Runtime.InteropServices.CallingConvention.StdCall)]
static string GetMD5HashfromString(string input)
{
// Create a new instance of the MD5CryptoServiceProvider object.
MD5CryptoServiceProvider md5Hasher = new MD5CryptoServiceProvider();
// Convert the input string to a byte array and compute the hash.
byte[] data = md5Hasher.ComputeHash(Encoding.Default.GetBytes(input));
// Create a new Stringbuilder to collect the bytes
// and create a string.
StringBuilder sBuilder = new StringBuilder();
// Loop through each byte of the hashed data
// and format each one as a hexadecimal string.
for (int i = 0; i < data.Length; i++)
{
sBuilder.Append(data.ToString("x2"));
}
// Return the hexadecimal string.
return sBuilder.ToString();
}
// Function verify MD5Hash from string
[DllExport("VerifyMD5HashfromString", CallingConvention = System.Runtime.InteropServices.CallingConvention.StdCall)]
static int VerifyMD5HashfromString(string input, string hash)
{
// Create a new instance of the MD5CryptoServiceProvider object.
MD5CryptoServiceProvider md5Hasher = new MD5CryptoServiceProvider();
// Convert the input string to a byte array and compute the hash.
byte[] data = md5Hasher.ComputeHash(Encoding.Default.GetBytes(input));
// Create a new Stringbuilder to collect the bytes
// and create a string.
StringBuilder sBuilder = new StringBuilder();
// Loop through each byte of the hashed data
// and format each one as a hexadecimal string.
for (int i = 0; i < data.Length; i++)
{
sBuilder.Append(data.ToString("x2"));
}
// Return the hexadecimal string.
string hashOfInput = sBuilder.ToString();
// Create a StringComparer an comare the hashes.
StringComparer comparer = StringComparer.OrdinalIgnoreCase;
if (0 == comparer.Compare(hashOfInput, hash))
{
return 1;
}
else
{
return 0;
}
}
// Function to get MD5Hash from FilePath
[DllExport("GetMD5HashfromFile", CallingConvention = System.Runtime.InteropServices.CallingConvention.StdCall)]
static string GetMD5HashfromFile(string filepath)
{
using (StreamReader sr = new StreamReader(filepath))
{
// Create a new instance of the MD5CryptoServiceProvider object.
MD5CryptoServiceProvider md5Hasher = new MD5CryptoServiceProvider();
// Convert the input string to a byte array and compute the hash.
byte[] data = md5Hasher.ComputeHash(Encoding.Default.GetBytes(sr.ReadToEnd()));
// Create a new Stringbuilder to collect the bytes
// and create a string.
StringBuilder sBuilder = new StringBuilder();
// Loop through each byte of the hashed data
// and format each one as a hexadecimal string.
for (int i = 0; i < data.Length; i++)
{
sBuilder.Append(data.ToString("x2"));
}
// Return the hexadecimal string.
return sBuilder.ToString();
}
}
}
}
Lua
---------------------------------------------
-------------------------GetMD5HashfromString
---------------------------------------------
str = [[Lorem ipsum dolor sit amet, consectetur adipiscing elit.
Aenean erat ligula, vehicula at placerat sed, aliquam quis arcu.
Praesent ut dui massa, condimentum placerat ante.
Sed fermentum felis id justo ornare dapibus. Cras auctor nisi id magna elementum scelerisque.
Suspendisse at eros ut lectus lacinia lacinia rutrum sed magna.
Praesent pharetra elit in ligula semper eu tempor augue venenatis.
Suspendisse rutrum elementum condimentum. Nulla facilisi.
Nulla pharetra enim eu arcu mattis volutpat. Etiam in ante nec erat suscipit dictum.
Integer quis arcu quis massa porta lobortis. Aliquam at tincidunt tortor.
Suspendisse mi tellus, sagittis in dignissim eu, semper vel sapien. Ut cursus porttitor felis.]]
result = DLL.CallFunction("AutoPlay\\Docs\\hash.dll", "GetMD5HashfromString", "\"" .. str .. "\"", DLL_RETURN_TYPE_STRING, DLL_CALL_STDCALL);
Dialog.Message("", result);
---------------------------------------------
----------------------VerifyMD5HashfromString
---------------------------------------------
str = [[Lorem ipsum dolor sit amet, consectetur adipiscing elit.
Aenean erat ligula, vehicula at placerat sed, aliquam quis arcu.
Praesent ut dui massa, condimentum placerat ante.
Sed fermentum felis id justo ornare dapibus. Cras auctor nisi id magna elementum scelerisque.
Suspendisse at eros ut lectus lacinia lacinia rutrum sed magna.
Praesent pharetra elit in ligula semper eu tempor augue venenatis.
Suspendisse rutrum elementum condimentum. Nulla facilisi.
Nulla pharetra enim eu arcu mattis volutpat. Etiam in ante nec erat suscipit dictum.
Integer quis arcu quis massa porta lobortis. Aliquam at tincidunt tortor.
Suspendisse mi tellus, sagittis in dignissim eu, semper vel sapien. Ut cursus porttitor felis.]]
hash = "50264edf34f7a46344700d94e8896df0"
result = DLL.CallFunction("AutoPlay\\Docs\\hash.dll", "VerifyMD5HashfromString", "\"" .. str .. "\""..",\""..hash .."\"", DLL_RETURN_TYPE_INTEGER, DLL_CALL_STDCALL);
Dialog.Message("", result); -- 0 = false, 1 = true.
---------------------------------------------
---------------------------GetMD5HashfromFile
---------------------------------------------
FilePath = _SourceFolder.."\\Autoplay\\Docs\\sampler.txt"
result = DLL.CallFunction("AutoPlay\\Docs\\hash.dll", "GetMD5HashfromFile", "\"" .. FilePath .. "\"", DLL_RETURN_TYPE_STRING, DLL_CALL_STDCALL);
Dialog.Message("", result);
---------------------------------------------
------------------------VerifyMD5HashfromFile
---------------------------------------------
str = TextFile.ReadToString("AutoPlay\\Docs\\sampler.txt");
hash = "5fb8d7e37b829a80b1216dd1ba43cbdb"
result = DLL.CallFunction("AutoPlay\\Docs\\hash.dll", "VerifyMD5HashfromString", "\"" .. str .. "\""..",\""..hash .."\"", DLL_RETURN_TYPE_INTEGER, DLL_CALL_STDCALL);
Dialog.Message("", result); -- 0 = false, 1 = true.
Re_UpLoaded On dropbox.com Server .... by abood 14/11/2012