Dll: HashMD5

Plugins y todo lo relacionado para Autoplay Media Studio.
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#
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.
Descarga:
HIDE: ON
Hidebb Message Hidden Description



Re_UpLoaded On dropbox.com Server .... by abood 14/11/2012
Gracias, voy a ver que tal :) 
Man grax por el aporte pero aun no entiendo que hace
sSantoss escribió:Man grax por el aporte pero aun no entiendo que hace
Mas Información sobre MD5 = http://es.wikipedia.org/wiki/MD5

bueno esta dll tiene tres funciones:
GetMD5HashfromString
VerifyMD5HashfromString
GetMD5HashfromFile

y básicamente esta dll es para saber el MD5 de un archivo o string y esto sirve para saber si un archivo o string han sido modificados o dañados...
Que tal va esta DLL de velocidad???
pues bien no me he puesto a hacer pruebas de tiempo...
Lo mismo me da una vena de hacer un actualizador y utilizo tu DLL para la comprobación de que las descargas sean correctas.

Por cierto, gracias ;)
la idea era esa, estaba pensado en hacer algo con CRC32 pero alfinal con el lio del AMSSpecialist Tools y algunas cosas mas que tengo entre manos no me he podido poner, la verdad es que estoy ahora dandole vastante al c# ave si me pongo y saco algo un poco mas gordo!!

Un saludo.
Yo estaba empezando con el Hello Word del C++ pero he tenido una serie de problemas familiares y he dejado todo un poco apartado...
probando.... gracias!
oh, different crypto.md5fromx ?
thanks
cheere, gracias lo chekeare , aunque se me esta ocurriendo algun parecido...deja lo cheko
pero en el AMS no este codigo que hace eso q ase tu DLL?

Lo puse en "Button" en "On Click

saca_codigo_md5 = Crypto.MD5DigestFromFile(_SourceFolder.."\\foto2.png");
Paragraph.SetText("Paragraph1", saca_codigo_md5);


y me da "1dd53d1b15474cdb2183faf524281472"

y luego compararlo sacando el md5 de otro archivo.. y mas bla bla blaç??

ME TOY EQUIBOCANDO, si es asi por favor corrijanme :) :hypno:
bravo
Gracias

:cerrado: :cerrado: :cerrado:
gracias kolegaaa muy wenoo
Thanks
gracias
gracias