Página 1 de 2

Dll: HashMD5

Publicado: 24 Abr 2011 12:37
por Ceone
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

Re: Dll: HashMD5

Publicado: 13 May 2011 18:42
por LSVargas
Gracias, voy a ver que tal :) 

Re: Dll: HashMD5

Publicado: 15 May 2011 02:33
por sSantoss
Man grax por el aporte pero aun no entiendo que hace

Re: Dll: HashMD5

Publicado: 15 May 2011 09:50
por Ceone
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...

Re: Dll: HashMD5

Publicado: 17 May 2011 09:57
por Daniel_Lechu
Que tal va esta DLL de velocidad???

Re: Dll: HashMD5

Publicado: 17 May 2011 10:22
por Ceone
pues bien no me he puesto a hacer pruebas de tiempo...

Re: Dll: HashMD5

Publicado: 17 May 2011 10:38
por Daniel_Lechu
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 ;)

Re: Dll: HashMD5

Publicado: 17 May 2011 10:42
por Ceone
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.

Re: Dll: HashMD5

Publicado: 17 May 2011 11:14
por Daniel_Lechu
Yo estaba empezando con el Hello Word del C++ pero he tenido una serie de problemas familiares y he dejado todo un poco apartado...

Re: Dll: HashMD5

Publicado: 03 Jun 2011 16:41
por krsk9999
probando.... gracias!

Re: Dll: HashMD5

Publicado: 04 Jul 2011 13:42
por nghethihieu
oh, different crypto.md5fromx ?

Re: Dll: HashMD5

Publicado: 11 Ago 2011 04:09
por dangngocnguyenit
thanks

Re: Dll: HashMD5

Publicado: 16 Ago 2011 09:59
por danielpz
cheere, gracias lo chekeare , aunque se me esta ocurriendo algun parecido...deja lo cheko

Re: Dll: HashMD5

Publicado: 16 Ago 2011 10:06
por danielpz
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:

Re: Dll: HashMD5

Publicado: 19 Sep 2011 21:16
por bumbo
bravo

Re: Dll: HashMD5

Publicado: 20 Sep 2011 22:22
por abood1987
Gracias

:cerrado: :cerrado: :cerrado:

Re: Dll: HashMD5

Publicado: 25 Nov 2011 11:07
por Bartal
gracias kolegaaa muy wenoo

Re: Dll: HashMD5

Publicado: 16 Mar 2012 15:37
por BlueMe
Thanks

Re: Dll: HashMD5

Publicado: 18 Mar 2012 07:15
por xxsolracxx
gracias

Re: Dll: HashMD5

Publicado: 06 May 2012 13:14
por Arseniy02
gracias