Club Builder - Foros de discusión Club Builder
Foro de C++Builder en español
 
 FAQFAQ   BuscarBuscar   MiembrosMiembros   Grupos de UsuariosGrupos de Usuarios   RegistrarseRegistrarse 
 PerfilPerfil   Entra para ver tus mensajes privadosEntra para ver tus mensajes privados   EntrarEntrar 

Identificador unico con MD5
Ir a página 1, 2  Siguiente
 
Publicar nuevo tema   Responder al tema    Foros de discusión -> Consultas
Ver tema anterior :: Ver tema siguiente  
Autor Mensaje
Mensaje    Asunto: Identificador unico con MD5 Responder citando

Hola amigos del foro
Necesito una funcion para obtener el identificador unico de un fichero o carpeta y buscando en el foro encontre el siguiente enlace
https://cppbuilder.es/viewtopic.php?t=11912&highlight=md5, pero al ejecutar me da varios errores ya explicado en el mismo tema por mi, aun no he recibido respuesta y por eso me di a la tarea de buscar en internet y encontre codigo de una pagina rusa pero el codigo esta en pascal (delphi), pero no se nada de esto , asi que por favor si alguien del foro es tan amable de ayudar traduciendo el codigo seria muy buen aporte pues como ya dije el codigo del link anterior da errores. aca dejo el codigo.
_________________
Todos somos ignorantes, lo que no todos ignoramos lo mismo...
Volver arriba
Ver perfil de usuario Enviar mensaje privado Enviar email
Mensaje    Asunto: Responder citando

el .pas
Código:
{******************************************************************}
{          MD5 Hashsum Evaluation Unit For Borland Delphi          }
{                                                                  }
{          Copyright © 2002 by Dimka Maslov                        }
{          E-mail:   mail@endimus.com,                             }
{          Web-site: http://www.endimus.com                        }
{                                                                  }
{         Derived from the RSA Data Security, Inc.                 }
{         MD5 Message-Digest Algorithm described in RFC 1321       }
{         http://www.faqs.org/rfcs/rfc1321.html                    }
{******************************************************************}

unit md5;

interface

uses Windows, SysUtils, Classes;

type
{ The TMD5Digest record is the type of results of
  the MD5 hashsum evaluation functions. The contents
  of a record may be used as four 32-bit integer values
  or as an array of 16 bytes }
 PMD5Digest = ^TMD5Digest;
 TMD5Digest = record
  case Integer of
   0: (A, B, C, D: LongInt);
   1: (v: array [0..15] of Byte);
 end;

{ The MD5String function evaluates the MD5 hashsum for
  a string. The S parameter specifies a string to
  evaluate hashsum }
function MD5String(const S: string): TMD5Digest;

{ The MD5File function evaluates the MD5 hashsum for
  a file. The FileName parameter specifies the name
  of a file to evaluate hashsum }
function MD5File(const FileName: string): TMD5Digest;

{ The MD5Stream function evaluates the MD5 hashsum for
  a stream. The Stream parameters specifies the
  TStream descendant class object to evaluate hashsum }
function MD5Stream(const Stream: TStream): TMD5Digest;

{ The MD5Buffer function evaluates the MD5 hashsum for
  any memory buffer. The Buffer parameters specifies a
  buffer to evaluate hashsum. The Size parameter specifies
  the size (in bytes) of a buffer }
function MD5Buffer(const Buffer; Size: Integer): TMD5Digest;

{ The MD5DigestToStr function converts the result of
  a hashsum evaluation function into a string of
  hexadecimal digits }
function MD5DigestToStr(const Digest: TMD5Digest): string;

{ The MD5DigestCompare function compares two
  TMD5Digest record variables. This function returns
  TRUE if parameters are equal or FALSE otherwise }
function MD5DigestCompare(const Digest1, Digest2: TMD5Digest): Boolean;

implementation

{
Copyright (C) 1991-2, RSA Data Security, Inc. Created 1991. All
rights reserved.

License to copy and use this software is granted provided that it
is identified as the "RSA Data Security, Inc. MD5 Message-Digest
Algorithm" in all material mentioning or referencing this software
or this function.

License is also granted to make and use derivative works provided
that such works are identified as "derived from the RSA Data
Security, Inc. MD5 Message-Digest Algorithm" in all material
mentioning or referencing the derived work.

RSA Data Security, Inc. makes no representations concerning either
the merchantability of this software or the suitability of this
software for any particular purpose. It is provided "as is"
without express or implied warranty of any kind.

These notices must be retained in any copies of any part of this
documentation and/or software.
}


type
 UINT4 = LongWord;

 PArray4UINT4 = ^TArray4UINT4;
 TArray4UINT4 = array [0..3] of UINT4;
 PArray2UINT4 = ^TArray2UINT4;
 TArray2UINT4 = array [0..1] of UINT4;
 PArray16Byte = ^TArray16Byte;
 TArray16Byte = array [0..15] of Byte;
 PArray64Byte = ^TArray64Byte;
 TArray64Byte = array [0..63] of Byte;

 PByteArray = ^TByteArray;
 TByteArray = array [0..0] of Byte;

 PUINT4Array = ^TUINT4Array;
 TUINT4Array = array [0..0] of UINT4;

 PMD5Context = ^TMD5Context;
 TMD5Context = record
   state: TArray4UINT4;
   count: TArray2UINT4;
   buffer: TArray64Byte;
 end;

const
  S11 = 7;
  S12 = 12;
  S13 = 17;
  S14 = 22;
  S21 = 5;
  S22 = 9;
  S23 = 14;
  S24 = 20;
  S31 = 4;
  S32 = 11;
  S33 = 16;
  S34 = 23;
  S41 = 6;
  S42 = 10;
  S43 = 15;
  S44 = 21;

var
 Padding : TArray64Byte =
 ($80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);


function _F(x, y, z: UINT4): UINT4;
begin
 Result := (((x) and (y)) or ((not x) and (z)));
end;

function _G(x, y, z: UINT4): UINT4;
begin
 Result := (((x) and (z)) or ((y) and (not z)));
end;

function _H(x, y, z: UINT4): UINT4;
begin
 Result := ((x) xor (y) xor (z));
end;

function _I(x, y, z: UINT4): UINT4;
begin
 Result := ((y) xor ((x) or ( not z)));
end;

function ROTATE_LEFT(x, n: UINT4): UINT4;
begin
 Result := (((x) shl (n)) or ((x) shr (32-(n))));
end;

procedure FF(var a: UINT4; b, c, d, x, s, ac: UINT4);
begin
  a := a + _F(b, c, d) + x + ac;
  a := ROTATE_LEFT (a, s);
  a := a + b;
end;

procedure GG(var a: UINT4; b, c, d, x, s, ac: UINT4);
begin
 a := a + _G(b, c, d) + x + ac;
 a := ROTATE_LEFT(a, s);
 a := a + b;
end;

procedure HH(var a: UINT4; b, c, d, x, s, ac: UINT4);
begin
 a := a + _H(b, c, d) + x + ac;
 a := ROTATE_LEFT(a, s);
 a := a + b;
end;

procedure II(var a: UINT4; b, c, d, x, s, ac: UINT4);
begin
 a := a + _I(b, c, d) + x + ac;
 a := ROTATE_LEFT(a, s);
 a := a + b;
end;

procedure MD5Encode(Output: PByteArray; Input: PUINT4Array; Len: LongWord);
var
 i, j: LongWord;
begin
 j:=0;
 i:=0;
 while j < Len do  begin
  output[j] := Byte(input[i] and $ff);
  output[j+1] := Byte((input[i] shr 8) and $ff);
  output[j+2] := Byte((input[i] shr 16) and $ff);
  output[j+3] := Byte((input[i] shr 24) and $ff);
  Inc(j, 4);
  Inc(i);
 end;
end;

procedure MD5Decode(Output: PUINT4Array; Input: PByteArray; Len: LongWord);
var
 i, j: LongWord;
begin
 j:=0;
 i:=0;
 while j < Len do begin
  Output[i] := UINT4(input[j]) or (UINT4(input[j+1]) shl 8) or
   (UINT4(input[j+2]) shl 16) or ( UINT4(input[j+3]) shl 24);
  Inc(j, 4);
  Inc(i);
 end;
end;

procedure MD5_memcpy(Output: PByteArray; Input: PByteArray; Len: LongWord);
begin
 Move(Input^, Output^, Len);
end;

procedure MD5_memset(Output: PByteArray; Value: Integer; Len: LongWord);
begin
 FillChar(Output^, Len, Byte(Value));
end;

procedure MD5Transform(State: PArray4UINT4; Buffer: PArray64Byte);
var
 a, b, c, d: UINT4;
 x : array[0..15] of UINT4;
begin
 a:=State[0]; b:=State[1]; c:=State[2]; d:=State[3];
 MD5Decode(PUINT4Array(@x), PByteArray(Buffer), 64);

 FF (a, b, c, d, x[ 0], S11, $d76aa478);
 FF (d, a, b, c, x[ 1], S12, $e8c7b756);
 FF (c, d, a, b, x[ 2], S13, $242070db);
 FF (b, c, d, a, x[ 3], S14, $c1bdceee);
 FF (a, b, c, d, x[ 4], S11, $f57c0faf);
 FF (d, a, b, c, x[ 5], S12, $4787c62a);
 FF (c, d, a, b, x[ 6], S13, $a8304613);
 FF (b, c, d, a, x[ 7], S14, $fd469501);
 FF (a, b, c, d, x[ 8], S11, $698098d8);
 FF (d, a, b, c, x[ 9], S12, $8b44f7af);
 FF (c, d, a, b, x[10], S13, $ffff5bb1);
 FF (b, c, d, a, x[11], S14, $895cd7be);
 FF (a, b, c, d, x[12], S11, $6b901122);
 FF (d, a, b, c, x[13], S12, $fd987193);
 FF (c, d, a, b, x[14], S13, $a679438e);
 FF (b, c, d, a, x[15], S14, $49b40821);

 GG (a, b, c, d, x[ 1], S21, $f61e2562);
 GG (d, a, b, c, x[ 6], S22, $c040b340);
 GG (c, d, a, b, x[11], S23, $265e5a51);
 GG (b, c, d, a, x[ 0], S24, $e9b6c7aa);
 GG (a, b, c, d, x[ 5], S21, $d62f105d);
 GG (d, a, b, c, x[10], S22,  $2441453);
 GG (c, d, a, b, x[15], S23, $d8a1e681);
 GG (b, c, d, a, x[ 4], S24, $e7d3fbc8);
 GG (a, b, c, d, x[ 9], S21, $21e1cde6);
 GG (d, a, b, c, x[14], S22, $c33707d6);
 GG (c, d, a, b, x[ 3], S23, $f4d50d87);

 GG (b, c, d, a, x[ 8], S24, $455a14ed);
 GG (a, b, c, d, x[13], S21, $a9e3e905);
 GG (d, a, b, c, x[ 2], S22, $fcefa3f8);
 GG (c, d, a, b, x[ 7], S23, $676f02d9);
 GG (b, c, d, a, x[12], S24, $8d2a4c8a);

 HH (a, b, c, d, x[ 5], S31, $fffa3942);
 HH (d, a, b, c, x[ 8], S32, $8771f681);
 HH (c, d, a, b, x[11], S33, $6d9d6122);
 HH (b, c, d, a, x[14], S34, $fde5380c);
 HH (a, b, c, d, x[ 1], S31, $a4beea44);
 HH (d, a, b, c, x[ 4], S32, $4bdecfa9);
 HH (c, d, a, b, x[ 7], S33, $f6bb4b60);
 HH (b, c, d, a, x[10], S34, $bebfbc70);
 HH (a, b, c, d, x[13], S31, $289b7ec6);
 HH (d, a, b, c, x[ 0], S32, $eaa127fa);
 HH (c, d, a, b, x[ 3], S33, $d4ef3085);
 HH (b, c, d, a, x[ 6], S34,  $4881d05);
 HH (a, b, c, d, x[ 9], S31, $d9d4d039);
 HH (d, a, b, c, x[12], S32, $e6db99e5);
 HH (c, d, a, b, x[15], S33, $1fa27cf8);
 HH (b, c, d, a, x[ 2], S34, $c4ac5665);

 II (a, b, c, d, x[ 0], S41, $f4292244);
 II (d, a, b, c, x[ 7], S42, $432aff97);
 II (c, d, a, b, x[14], S43, $ab9423a7);
 II (b, c, d, a, x[ 5], S44, $fc93a039);
 II (a, b, c, d, x[12], S41, $655b59c3);
 II (d, a, b, c, x[ 3], S42, $8f0ccc92);
 II (c, d, a, b, x[10], S43, $ffeff47d);
 II (b, c, d, a, x[ 1], S44, $85845dd1);
 II (a, b, c, d, x[ 8], S41, $6fa87e4f);
 II (d, a, b, c, x[15], S42, $fe2ce6e0);
 II (c, d, a, b, x[ 6], S43, $a3014314);
 II (b, c, d, a, x[13], S44, $4e0811a1);
 II (a, b, c, d, x[ 4], S41, $f7537e82);
 II (d, a, b, c, x[11], S42, $bd3af235);
 II (c, d, a, b, x[ 2], S43, $2ad7d2bb);
 II (b, c, d, a, x[ 9], S44, $eb86d391);

 Inc(State[0], a);
 Inc(State[1], b);
 Inc(State[2], c);
 Inc(State[3], d);

 MD5_memset (PByteArray(@x), 0, SizeOf (x));
end;


procedure MD5Init(var Context: TMD5Context);
begin
 FillChar(Context, SizeOf(Context), 0);
 Context.state[0] := $67452301;
 Context.state[1] := $efcdab89;
 Context.state[2] := $98badcfe;
 Context.state[3] := $10325476;
end;

procedure MD5Update(var Context: TMD5Context; Input: PByteArray; InputLen: LongWord);
var
 i, index, partLen: LongWord;

begin
 index := LongWord( (context.count[0] shr 3) and $3F);
 Inc(Context.count[0], UINT4(InputLen) shl 3);
 if Context.count[0] <UINT4>= partLen then begin
  MD5_memcpy(PByteArray(@Context.buffer[index]), Input, PartLen);
  MD5Transform(@Context.state, @Context.buffer);
  i := partLen;
  while i + 63 < inputLen do begin
   MD5Transform(@Context.state, PArray64Byte(@Input[i]));
   Inc(i, 64);
  end;
  index := 0;
 end else i:=0;
 MD5_memcpy(PByteArray(@Context.buffer[index]), PByteArray(@Input[i]), inputLen - i);
end;


procedure MD5Final(var Digest: TMD5Digest; var Context: TMD5Context);
var
 bits: array [0..7] of Byte;
 index, padLen: LongWord;
begin
 MD5Encode(PByteArray(@bits), PUINT4Array(@Context.count), 8);
 index := LongWord( (Context.count[0] shr 3) and $3F);
 if index < 56 then padLen := 56 - index else padLen := 120 - index;
 MD5Update(Context, PByteArray(@PADDING), padLen);
 MD5Update(Context, PByteArray(@Bits), 8);
 MD5Encode(PByteArray(@Digest), PUINT4Array(@Context.state), 16);
 MD5_memset(PByteArray(@Context), 0, SizeOf(Context));
end;

function MD5DigestToStr(const Digest: TMD5Digest): string;
var
 i: Integer;
begin
 Result:='';
 for i:=0 to 15 do Result:=Result+IntToHex(Digest.v[i], 2);
end;

function MD5String(const S: string): TMD5Digest;
begin
 Result:=MD5Buffer(PChar(S)^, Length(S));
end;

function MD5File(const FileName: string): TMD5Digest;
var
 F: TFileStream;
begin
 F:=TFileStream.Create(FileName, fmOpenRead);
 try
  Result:=MD5Stream(F);
 finally
  F.Free;
 end;
end;

function MD5Stream(const Stream: TStream): TMD5Digest;
var
 Context: TMD5Context;
 Buffer: array[0..4095] of Byte;
 Size: Integer;
 ReadBytes : Integer;
 TotalBytes : Integer;
 SavePos: Integer;
begin
 MD5Init(Context);
 Size:=Stream.Size;
 SavePos:=Stream.Position;
 TotalBytes:=0;
 try
  Stream.Seek(0, soFromBeginning);
  repeat
   ReadBytes:=Stream.Read(Buffer, SizeOf(Buffer));
   Inc(TotalBytes, ReadBytes);
   MD5Update(Context, @Buffer, ReadBytes);
  until (ReadBytes = 0) or (TotalBytes = Size);
 finally
  Stream.Seek(SavePos, soFromBeginning);
 end;
 MD5Final(Result, Context);
end;

function MD5Buffer(const Buffer; Size: Integer): TMD5Digest;
var
 Context: TMD5Context;
begin
 MD5Init(Context);
 MD5Update(Context, PByteArray(@Buffer), Size);
 MD5Final(Result, Context);
end;

function MD5DigestCompare(const Digest1, Digest2: TMD5Digest): Boolean;
begin
 Result:=False;
 if Digest1.A <> Digest2.A then Exit;
 if Digest1.B <> Digest2.B then Exit;
 if Digest1.C <> Digest2.C then Exit;
 if Digest1.D <> Digest2.D then Exit;
 Result:=True;
end;

end.

_________________
Todos somos ignorantes, lo que no todos ignoramos lo mismo...
Volver arriba
Ver perfil de usuario Enviar mensaje privado Enviar email
Mensaje    Asunto: Responder citando

Algunos enlaces para descargar la libreria MD5

http://phylevn.mexrom.net/index.php/blog/show/MD5_en_Compact_Framework_Net_20.html
- http://s.faraz.mahmood.googlepages.com/MD5.zip
- http://farazmahmood.wordpress.com/projects/md5-implementation-in-c/

En este enlace puedes encontrar alguna información relevante a los sistemas de Criptografía.
http://www.matematicas.net/paraiso/cripto.php?id=cripto#actualizacion

Saludos.

Aunado a ello, existen varias formas de hacerlo pero como esta libreria ya "casi" es de dominio público, aqui hay enlaces donde viene el codigo ya listo para compiladores de borland.

http://wxchecksums.sourceforge.net/doxygen/md5_8cpp-source.html
_________________
Cool


Ultima edición por Cesar C. Salazar Gonzalez el Jue 08 Oct, 2009 5:37 pm, editado 1 vez
Volver arriba
Ver perfil de usuario Enviar mensaje privado Enviar email MSN Messenger
Mensaje    Asunto: Responder citando

.hpp
Código:
#ifndef md5HPP
#define md5HPP

#pragma delphiheader begin
#pragma option push -w-
#pragma option push -Vx
#include <Classes>    // Pascal unit
#include <SysUtils>    // Pascal unit
#include <Windows>    // Pascal unit
#include <SysInit>    // Pascal unit
#include <System>    // Pascal unit

//-- user supplied -----------------------------------------------------------

namespace Md5
{
//-- type declarations -------------------------------------------------------
struct TMD5Digest;
typedef TMD5Digest *PMD5Digest;

#pragma pack(push, 4)
struct TMD5Digest
{
   
    union
    {
        struct
        {
            Byte v[16];
           
        };
        struct
        {
            int A;
            int B;
            int C;
            int D;
           
        };
       
    };
} ;
#pragma pack(pop)

//-- var, const, procedure ---------------------------------------------------
extern PACKAGE AnsiString __fastcall MD5DigestToStr(const TMD5Digest &Digest);
extern PACKAGE TMD5Digest __fastcall MD5String(const AnsiString S);
extern PACKAGE TMD5Digest __fastcall MD5File(const AnsiString FileName);
extern PACKAGE TMD5Digest __fastcall MD5Stream(const Classes::TStream* Stream);
extern PACKAGE TMD5Digest __fastcall MD5Buffer(const void *Buffer, int Size);
extern PACKAGE bool __fastcall MD5DigestCompare(const TMD5Digest &Digest1, const TMD5Digest &Digest2);

}    /* namespace Md5 */
using namespace Md5;
#pragma option pop    // -w-
#pragma option pop    // -Vx

#pragma delphiheader end.
//-- end unit ----------------------------------------------------------------
#endif

Подключаем:
//libreria que hay que agregar
#include "md5.hpp"

Используем:
//modo de uso
AnsiString hash = MD5DigestToStr(MD5String("www.sources.ru"));

_________________
Todos somos ignorantes, lo que no todos ignoramos lo mismo...
Volver arriba
Ver perfil de usuario Enviar mensaje privado Enviar email
Mensaje    Asunto: Responder citando

Cesar gracias por responder al tema , pero y como se implementaria en c++ builder6 porque si mal no entendi el.zip lo que trae es codigo para C#
saludos y gracias
_________________
Todos somos ignorantes, lo que no todos ignoramos lo mismo...
Volver arriba
Ver perfil de usuario Enviar mensaje privado Enviar email
Mensaje    Asunto: Responder citando

Hola amigos del foro, segui investigando sobre MD5 y encontre en una pagina japonesa un codigo que les voy a dejar aca esta en C++ lo compile y organice en bcb6 y corre sin lios, pero es algo asi como para encriptar texto en md5, pero lo que yo deseo (y lo digo por si alguien lo tiene) es con el md5 obtener el hash de un fichero (el identificador unico)

.h
Código:
//---------------------------------------------------------------------------

#ifndef md5H
#define md5H
//---------------------------------------------------------------------------
  //MD5.h 
  /****************************************************************************** 
    *     Copyright   (C)   2000   by   Robert   Hubley.                                                                             * 
    *     All   rights   reserved.                                                                                                             * 
    *                                                                                                                                                         * 
    *     This   software   is   provided   ``AS   IS''   and   any   express   or   implied                         * 
    *     warranties,   including,   but   not   limited   to,   the   implied   warranties   of             * 
    *     merchantability   and   fitness   for   a   particular   purpose,   are   disclaimed.           * 
    *     In   no   event   shall   the   authors   be   liable   for   any   direct,   indirect,                   * 
    *     incidental,   special,   exemplary,   or   consequential   damages   (including,   but     * 
    *     not   limited   to,   procurement   of   substitute   goods   or   services;   loss   of   use,   * 
    *     data,   or   profits;   or   business   interruption)   however   caused   and   on   any           * 
    *     theory   of   liability,   whether   in   contract,   strict   liability,   or   tort               * 
    *     (including   negligence   or   otherwise)   arising   in   any   way   out   of   the   use   of     * 
    *     this   software,   even   if   advised   of   the   possibility   of   such   damage.                   * 
    *                                                                                                                                                         * 
    ****************************************************************************** 
   
    MD5.H   -   header   file   for   MD5C.C 
     
        Port   to   Win32   DLL   by   Robert   Hubley   1/5/2000 
   
        Original   Copyright: 
   
  Copyright   (C)   1991-2,   RSA   Data   Security,   Inc.   Created   1991.   All 
  rights   reserved. 
   
  License   to   copy   and   use   this   software   is   granted   provided   that   it 
  is   identified   as   the   "RSA   Data   Security,   Inc.   MD5   Message-Digest 
  Algorithm"   in   all   material   mentioning   or   referencing   this   software 
  or   this   function. 
   
  License   is   also   granted   to   make   and   use   derivative   works   provided 
  that   such   works   are   identified   as   "derived   from   the   RSA   Data 
  Security,   Inc.   MD5   Message-Digest   Algorithm"   in   all   material 
  mentioning   or   referencing   the   derived   work. 
   
  RSA   Data   Security,   Inc.   makes   no   representations   concerning   either 
  the   merchantability   of   this   software   or   the   suitability   of   this 
  software   for   any   particular   purpose.   It   is   provided   "as   is" 
  without   express   or   implied   warranty   of   any   kind. 
   
  These   notices   must   be   retained   in   any   copies   of   any   part   of   this 
  documentation   and/or   software. 
  */ 
  /****************************************************************************** 
    *     Copyright   (C)   2000   by   Robert   Hubley.                                                                             * 
    *     All   rights   reserved.                                                                                                             * 
    *                                                                                                                                                         * 
    *     This   software   is   provided   ``AS   IS''   and   any   express   or   implied                         * 
    *     warranties,   including,   but   not   limited   to,   the   implied   warranties   of             * 
    *     merchantability   and   fitness   for   a   particular   purpose,   are   disclaimed.           * 
    *     In   no   event   shall   the   authors   be   liable   for   any   direct,   indirect,                   * 
    *     incidental,   special,   exemplary,   or   consequential   damages   (including,   but     * 
    *     not   limited   to,   procurement   of   substitute   goods   or   services;   loss   of   use,   * 
    *     data,   or   profits;   or   business   interruption)   however   caused   and   on   any           * 
    *     theory   of   liability,   whether   in   contract,   strict   liability,   or   tort               * 
    *     (including   negligence   or   otherwise)   arising   in   any   way   out   of   the   use   of     * 
    *     this   software,   even   if   advised   of   the   possibility   of   such   damage.                   * 
    *                                                                                                                                                         * 
    ****************************************************************************** 
  */ 
   
  /****************************************************************************** 
    *       2002-4-18   Modified   by   Liguangyi.             * 
    *       struct   MD5_CTX   ==>   class   MD5_CTX.                     * 
    *       Take   off   the   Globals   Functions                                                                                       * 
    ****************************************************************************** 
  */ 
   
  #ifndef   _LGY_MD5_H
  #define   _LGY_MD5_H 
   
  /*   MD5   Class.   */ 
  class   MD5_CTX   { 
  public: 
  MD5_CTX(); 
  virtual   ~MD5_CTX(); 
  void   MD5Update   (   unsigned   char   *input,   unsigned   int   inputLen);
  void   MD5Final   (unsigned   char   digest[16]); 
   
  private: 
  unsigned   long   int   state[4]; /*   state   (ABCD)   */ 
  unsigned   long   int   count[2]; /*   number   of   bits,   modulo   2^64   (lsb   first)   */ 
  unsigned   char   buffer[64];               /*   input   buffer   */ 
  unsigned   char   PADDING[64]; /*   What?   */ 
   
  private: 
  void   MD5Init   (); 
  void   MD5Transform   (unsigned   long   int   state[4],   unsigned   char   block[64]); 
  void   MD5_memcpy   (unsigned   char*   output,   unsigned   char*   input,unsigned   int   len); 
  void   Encode   (unsigned   char   *output,   unsigned   long   int   *input,unsigned   int   len); 
  void   Decode   (unsigned   long   int   *output,   unsigned   char   *input,   unsigned   int   len); 
  void   MD5_memset   (unsigned   char*   output,int   value,unsigned   int   len); 
  }; 
  #endif _LGY_MD5_H
  #endif





.cpp


Código:
//---------------------------------------------------------------------------


#pragma hdrstop



//---------------------------------------------------------------------------
//MD5.cpp 
  /****************************************************************************** 
  *     Copyright   (C)   2000   by   Robert   Hubley.                                                                             * 
  *     All   rights   reserved.                                                                                                             * 
  *                                                                                                                                                         * 
  *     This   software   is   provided   ``AS   IS''   and   any   express   or   implied                         * 
  *     warranties,   including,   but   not   limited   to,   the   implied   warranties   of             * 
  *     merchantability   and   fitness   for   a   particular   purpose,   are   disclaimed.           * 
  *     In   no   event   shall   the   authors   be   liable   for   any   direct,   indirect,                   * 
  *     incidental,   special,   exemplary,   or   consequential   damages   (including,   but     * 
  *     not   limited   to,   procurement   of   substitute   goods   or   services;   loss   of   use,   * 
  *     data,   or   profits;   or   business   interruption)   however   caused   and   on   any           * 
  *     theory   of   liability,   whether   in   contract,   strict   liability,   or   tort               * 
  *     (including   negligence   or   otherwise)   arising   in   any   way   out   of   the   use   of     * 
  *     this   software,   even   if   advised   of   the   possibility   of   such   damage.                   * 
  *                                                                                                                                                         * 
  ****************************************************************************** 
   
  MD5C.C   -   RSA   Data   Security,   Inc.,   MD5   message-digest   algorithm 
   
  Port   to   Win32   DLL   by   Robert   Hubley   1/5/2000 
   
  Original   Copyright: 
   
  Copyright   (C)   1991-2,   RSA   Data   Security,   Inc.   Created   1991.   All 
  rights   reserved. 
   
  License   to   copy   and   use   this   software   is   granted   provided   that   it 
  is   identified   as   the   "RSA   Data   Security,   Inc.   MD5   Message-Digest 
  Algorithm"   in   all   material   mentioning   or   referencing   this   software 
  or   this   function. 
       
  License   is   also   granted   to   make   and   use   derivative   works   provided 
  that   such   works   are   identified   as   "derived   from   the   RSA   Data 
  Security,   Inc.   MD5   Message-Digest   Algorithm"   in   all   material 
  mentioning   or   referencing   the   derived   work. 
   
  RSA   Data   Security,   Inc.   makes   no   representations   concerning   either 
  the   merchantability   of   this   software   or   the   suitability   of   this 
  software   for   any   particular   purpose.   It   is   provided   "as   is" 
  without   express   or   implied   warranty   of   any   kind. 
       
  These   notices   must   be   retained   in   any   copies   of   any   part   of   this 
  documentation   and/or   software. 
  */ 
   
  #include   "md5.h" 
   
  /*   Constants   for   MD5Transform   routine. 
  */ 
  #define   S11   7 
  #define   S12   12 
  #define   S13   17 
  #define   S14   22 
  #define   S21   5 
  #define   S22   9 
  #define   S23   14 
  #define   S24   20 
  #define   S31   4 
  #define   S32   11 
  #define   S33   16 
  #define   S34   23 
  #define   S41   6 
  #define   S42   10 
  #define   S43   15 
  #define   S44   21 
   
  /*   F,   G,   H   and   I   are   basic   MD5   functions. 
  */ 
  #define   F(x,   y,   z)   (((x)   &   (y))   |   ((~x)   &   (z))) 
  #define   G(x,   y,   z)   (((x)   &   (z))   |   ((y)   &   (~z))) 
  #define   H(x,   y,   z)   ((x)   ^   (y)   ^   (z)) 
  #define   I(x,   y,   z)   ((y)   ^   ((x)   |   (~z))) 
   
  /*   ROTATE_LEFT   rotates   x   left   n   bits. 
  */ 
  #define   ROTATE_LEFT(x,   n)   (((x)   <<n>>   (32-(n)))) 
   
  /*   FF,   GG,   HH,   and   II   transformations   for   rounds   1,   2,   3,   and   4. 
  Rotation   is   separate   from   addition   to   prevent   recomputation. 
  */ 
  #define   FF(a,   b,   c,   d,   x,   s,   ac)   {   (a)   +=   F   ((b),   (c),   (d))   +   (x)   +   (unsigned   long   int)(ac);   (a)   =   ROTATE_LEFT   ((a),   (s));   (a)   +=   (b);   } 
  #define   GG(a,   b,   c,   d,   x,   s,   ac)   {   (a)   +=   G   ((b),   (c),   (d))   +   (x)   +   (unsigned   long   int)(ac);   (a)   =   ROTATE_LEFT   ((a),   (s));   (a)   +=   (b);   } 
  #define   HH(a,   b,   c,   d,   x,   s,   ac)   {   (a)   +=   H   ((b),   (c),   (d))   +   (x)   +   (unsigned   long   int)(ac);   (a)   =   ROTATE_LEFT   ((a),   (s));   (a)   +=   (b);   } 
  #define   II(a,   b,   c,   d,   x,   s,   ac)   {   (a)   +=   I   ((b),   (c),   (d))   +   (x)   +   (unsigned   long   int)(ac);   (a)   =   ROTATE_LEFT   ((a),   (s));   (a)   +=   (b);   } 
   
   
  /*   MD5   initialization.   Begins   an   MD5   operation,   writing   a   new   context. 
  */ 
   
  MD5_CTX::MD5_CTX() 
  { 
  MD5Init   (); 
  } 
   
  MD5_CTX::~MD5_CTX() 
  { 
  } 
   
  void   MD5_CTX::MD5Init   () 
  { 
  this->count[0]   =   this->count[1]   =   0; 
  /*   Load   magic   initialization   constants.*/ 
  this->state[0]   =   0x67452301; 
  this->state[1]   =   0xefcdab89; 
  this->state[2]   =   0x98badcfe; 
  this->state[3]   =   0x10325476; 
  /*   Add   by   Liguangyi   */ 
  MD5_memset(PADDING,   0,   sizeof(PADDING)); 
  *PADDING=0x80; 
  //PADDING   =   { 
  // 0x80,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0, 
  // 0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0, 
  // 0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0 }; 
  } 
   
  /*   MD5   block   update   operation.   Continues   an   MD5   message-digest 
  operation,   processing   another   message   block,   and   updating   the 
  context. 
  */ 
  void   MD5_CTX::MD5Update   (unsigned   char   *input,unsigned   int   inputLen) 
  { 
  unsigned   int   i,   index,   partLen; 
   
  /*   Compute   number   of   bytes   mod   64   */ 
  index   =   (unsigned   int)((this->count[0]   >>   3)   &   0x3F); 
   
  /*   Update   number   of   bits   */ 
  if   ((this->count[0]   +=   ((unsigned   long   int)inputLen   <<   3)) 
  <   ((unsigned   long   int)inputLen   <<3>count[1]++; 
  this->count[1]   +=   ((unsigned   long   int)inputLen   >>   29); 
   
  partLen   =   64   -   index; 
   
  /*   Transform   as   many   times   as   possible. 
  */ 
  if   (inputLen   >=   partLen)   { 
  MD5_memcpy((unsigned   char*)&this->buffer[index],   
  (unsigned   char*)input,   partLen); 
  MD5Transform   (this->state,   this->buffer); 
   
  for   (i   =   partLen;   i   +   63   <inputLen>state,   &input[i]); 
   
  index   =   0; 
  } 
  else 
  i   =   0; 
   
  /*   Buffer   remaining   input   */ 
  MD5_memcpy   ((unsigned   char*)&this->buffer[index],   (unsigned   char*)&input[i],   inputLen-i); 
  }

  //MD5.cpp   ??? 
  /*   MD5   finalization.   Ends   an   MD5   message-digest   operation,   writing   the 
  the   message   digest   and   zeroizing   the   context. 
  */ 
  void   MD5_CTX::MD5Final   (unsigned   char   digest[16]) 
  { 
  unsigned   char   bits[8]; 
  unsigned   int   index,   padLen; 
   
  /*   Save   number   of   bits   */ 
  Encode   (bits,   this->count,   8); 
   
  /*   Pad   out   to   56   mod   64. 
  */ 
  index   =   (unsigned   int)((this->count[0]   >>   3)   &   0x3f); 
  padLen   =   (index   <56>state,   16); 
   
  /*   Zeroize   sensitive   information. 
  */ 
  MD5_memset   ((unsigned   char*)this,   0,   sizeof   (*this)); 
  this->MD5Init(); 
  } 
   
  /*   MD5   basic   transformation.   Transforms   state   based   on   block. 
  */ 
  void   MD5_CTX::MD5Transform   (unsigned   long   int   state[4],   unsigned   char   block[64]) 
  { 
  unsigned   long   int   a   =   state[0],   b   =   state[1],   c   =   state[2],   d   =   state[3],   x[16]; 
   
  Decode   (x,   block,   64); 
   
  /*   Round   1   */ 
  FF   (a,   b,   c,   d,   x[   0],   S11,   0xd76aa478);   /*   1   */ 
  FF   (d,   a,   b,   c,   x[   1],   S12,   0xe8c7b756);   /*   2   */ 
  FF   (c,   d,   a,   b,   x[   2],   S13,   0x242070db);   /*   3   */ 
  FF   (b,   c,   d,   a,   x[   3],   S14,   0xc1bdceee);   /*   4   */ 
  FF   (a,   b,   c,   d,   x[   4],   S11,   0xf57c0faf);   /*   5   */ 
  FF   (d,   a,   b,   c,   x[   5],   S12,   0x4787c62a);   /*   6   */ 
  FF   (c,   d,   a,   b,   x[   6],   S13,   0xa8304613);   /*   7   */ 
  FF   (b,   c,   d,   a,   x[   7],   S14,   0xfd469501);   /*   8   */ 
  FF   (a,   b,   c,   d,   x[   8],   S11,   0x698098d8);   /*   9   */ 
  FF   (d,   a,   b,   c,   x[   9],   S12,   0x8b44f7af);   /*   10   */ 
  FF   (c,   d,   a,   b,   x[10],   S13,   0xffff5bb1);   /*   11   */ 
  FF   (b,   c,   d,   a,   x[11],   S14,   0x895cd7be);   /*   12   */ 
  FF   (a,   b,   c,   d,   x[12],   S11,   0x6b901122);   /*   13   */ 
  FF   (d,   a,   b,   c,   x[13],   S12,   0xfd987193);   /*   14   */ 
  FF   (c,   d,   a,   b,   x[14],   S13,   0xa679438e);   /*   15   */ 
  FF   (b,   c,   d,   a,   x[15],   S14,   0x49b40821);   /*   16   */ 
   
  /*   Round   2   */ 
  GG   (a,   b,   c,   d,   x[   1],   S21,   0xf61e2562);   /*   17   */ 
  GG   (d,   a,   b,   c,   x[   6],   S22,   0xc040b340);   /*   18   */ 
  GG   (c,   d,   a,   b,   x[11],   S23,   0x265e5a51);   /*   19   */ 
  GG   (b,   c,   d,   a,   x[   0],   S24,   0xe9b6c7aa);   /*   20   */ 
  GG   (a,   b,   c,   d,   x[   5],   S21,   0xd62f105d);   /*   21   */ 
  GG   (d,   a,   b,   c,   x[10],   S22,     0x2441453);   /*   22   */ 
  GG   (c,   d,   a,   b,   x[15],   S23,   0xd8a1e681);   /*   23   */ 
  GG   (b,   c,   d,   a,   x[   4],   S24,   0xe7d3fbc8);   /*   24   */ 
  GG   (a,   b,   c,   d,   x[   9],   S21,   0x21e1cde6);   /*   25   */ 
  GG   (d,   a,   b,   c,   x[14],   S22,   0xc33707d6);   /*   26   */ 
  GG   (c,   d,   a,   b,   x[   3],   S23,   0xf4d50d87);   /*   27   */ 
  GG   (b,   c,   d,   a,   x[   8],   S24,   0x455a14ed);   /*   28   */ 
  GG   (a,   b,   c,   d,   x[13],   S21,   0xa9e3e905);   /*   29   */ 
  GG   (d,   a,   b,   c,   x[   2],   S22,   0xfcefa3f8);   /*   30   */ 
  GG   (c,   d,   a,   b,   x[   7],   S23,   0x676f02d9);   /*   31   */ 
  GG   (b,   c,   d,   a,   x[12],   S24,   0x8d2a4c8a);   /*   32   */ 
   
  /*   Round   3   */ 
  HH   (a,   b,   c,   d,   x[   5],   S31,   0xfffa3942);   /*   33   */ 
  HH   (d,   a,   b,   c,   x[   8],   S32,   0x8771f681);   /*   34   */ 
  HH   (c,   d,   a,   b,   x[11],   S33,   0x6d9d6122);   /*   35   */ 
  HH   (b,   c,   d,   a,   x[14],   S34,   0xfde5380c);   /*   36   */ 
  HH   (a,   b,   c,   d,   x[   1],   S31,   0xa4beea44);   /*   37   */ 
  HH   (d,   a,   b,   c,   x[   4],   S32,   0x4bdecfa9);   /*   38   */ 
  HH   (c,   d,   a,   b,   x[   7],   S33,   0xf6bb4b60);   /*   39   */ 
  HH   (b,   c,   d,   a,   x[10],   S34,   0xbebfbc70);   /*   40   */ 
  HH   (a,   b,   c,   d,   x[13],   S31,   0x289b7ec6);   /*   41   */ 
  HH   (d,   a,   b,   c,   x[   0],   S32,   0xeaa127fa);   /*   42   */ 
  HH   (c,   d,   a,   b,   x[   3],   S33,   0xd4ef3085);   /*   43   */ 
  HH   (b,   c,   d,   a,   x[   6],   S34,     0x4881d05);   /*   44   */ 
  HH   (a,   b,   c,   d,   x[   9],   S31,   0xd9d4d039);   /*   45   */ 
  HH   (d,   a,   b,   c,   x[12],   S32,   0xe6db99e5);   /*   46   */ 
  HH   (c,   d,   a,   b,   x[15],   S33,   0x1fa27cf8);   /*   47   */ 
  HH   (b,   c,   d,   a,   x[   2],   S34,   0xc4ac5665);   /*   48   */ 
   
  /*   Round   4   */ 
  II   (a,   b,   c,   d,   x[   0],   S41,   0xf4292244);   /*   49   */ 
  II   (d,   a,   b,   c,   x[   7],   S42,   0x432aff97);   /*   50   */ 
  II   (c,   d,   a,   b,   x[14],   S43,   0xab9423a7);   /*   51   */ 
  II   (b,   c,   d,   a,   x[   5],   S44,   0xfc93a039);   /*   52   */ 
  II   (a,   b,   c,   d,   x[12],   S41,   0x655b59c3);   /*   53   */ 
  II   (d,   a,   b,   c,   x[   3],   S42,   0x8f0ccc92);   /*   54   */ 
  II   (c,   d,   a,   b,   x[10],   S43,   0xffeff47d);   /*   55   */ 
  II   (b,   c,   d,   a,   x[   1],   S44,   0x85845dd1);   /*   56   */ 
  II   (a,   b,   c,   d,   x[   8],   S41,   0x6fa87e4f);   /*   57   */ 
  II   (d,   a,   b,   c,   x[15],   S42,   0xfe2ce6e0);   /*   58   */ 
  II   (c,   d,   a,   b,   x[   6],   S43,   0xa3014314);   /*   59   */ 
  II   (b,   c,   d,   a,   x[13],   S44,   0x4e0811a1);   /*   60   */ 
  II   (a,   b,   c,   d,   x[   4],   S41,   0xf7537e82);   /*   61   */ 
  II   (d,   a,   b,   c,   x[11],   S42,   0xbd3af235);   /*   62   */ 
  II   (c,   d,   a,   b,   x[   2],   S43,   0x2ad7d2bb);   /*   63   */ 
  II   (b,   c,   d,   a,   x[   9],   S44,   0xeb86d391);   /*   64   */ 
   
  state[0]   +=   a; 
  state[1]   +=   b; 
  state[2]   +=   c; 
  state[3]   +=   d; 
   
  /*   Zeroize   sensitive   information. 
  */ 
  MD5_memset   ((unsigned   char*)x,   0,   sizeof   (x)); 
  } 
   
  /*   Encodes   input   (unsigned   long   int)   into   output   (unsigned   char).   Assumes   len   is 
  a   multiple   of   4. 
  */ 
  void   MD5_CTX::Encode   (unsigned   char   *output,   unsigned   long   int   *input,unsigned   int   len) 
  { 
  unsigned   int   i,   j; 
   
  for   (i   =   0,   j   =   0;   j   <len>>   8)   &   0xff); 
  output[j+2]   =   (unsigned   char)((input[i]   >>   16)   &   0xff); 
  output[j+3]   =   (unsigned   char)((input[i]   >>   24)   &   0xff); 
  } 
  } 
   
  /*   Decodes   input   (unsigned   char)   into   output   (unsigned   long   int).   Assumes   len   is 
  a   multiple   of   4. 
  */ 
  void   MD5_CTX::Decode   (unsigned   long   int   *output,   unsigned   char   *input,   unsigned   int   len) 
  { 
  unsigned   int   i,   j; 
   
  for   (i   =   0,   j   =   0;   j   <   len;   i++,   j   +=   4) 
  output[i]   =   ((unsigned   long   int)input[j])   |   (((unsigned   long   int)input[j+1])   <<   8)   | 
  (((unsigned   long   int)input[j+2])   <<   16)   |   (((unsigned   long   int)input[j+3])   <<   24); 
  } 
   
  /*   Note:   Replace   "for   loop"   with   standard   memcpy   if   possible. 
  */ 
   
  void   MD5_CTX::MD5_memcpy   (unsigned   char*   output,   unsigned   char*   input,unsigned   int   len) 
  { 
  unsigned   int   i; 
   
  for   (i   =   0;   i   <   len;   i++) 
  output[i]   =   input[i]; 
  } 
   
  /*   Note:   Replace   "for   loop"   with   standard   memset   if   possible. 
  */ 
  void   MD5_CTX::MD5_memset   (unsigned   char*   output,int   value,unsigned   int   len) 
  { 
  unsigned   int   i; 
   
  for   (i   =   0;   i   <   len;   i++) 
  ((char   *)output)[i]   =   (char)value; 
  }   

#pragma package(smart_init)



modo de utilizar

Código:


//---------------------------------------------------------------------------

#include <vcl>
#pragma hdrstop

#include "Unit1.h"
#include "md5.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
        : TForm(Owner)
{
}
//---------------------------------------------------------------------------

void __fastcall TForm1::Button1Click(TObject *Sender)
{
if(od->Execute())
edtSrcText->Text = od->FileName;
char   pBuf[1024];
      memset(pBuf,0,1024); 
      strcpy(pBuf,edtSrcText->Text.c_str()); 
      int iTotal=strlen(pBuf);
      MD5_CTX   md5T;
      unsigned   char   digest[16]; 
      md5T.MD5Update   (pBuf,   iTotal);
      md5T.MD5Final(digest); 
      String   sHex=IntToHex(*((__int64*)digest),8); 
      edtMD5->Text=String(sHex);       
}
//---------------------------------------------------------------------------
 


espero les sirva Wink
_________________
Todos somos ignorantes, lo que no todos ignoramos lo mismo...
Volver arriba
Ver perfil de usuario Enviar mensaje privado Enviar email
Mensaje    Asunto: Responder citando

Aun con problemas con el md5, con el codigo que postee solo sabemso si una cadena de texto ha sido modificada, perolo que realmente deseo es saber si x fichero ha sido modificado, tanto un .exe, .rar, etc, asi sabremso si hay errores , infiltraciones , modificaciones, etc (interesante verdad?)
pues eso lo que deseo y se que se puede hacer, el norte que puedo dar (por supuesto no lo se hacer) debe ser pasandole un fichero o buffer del fichero a la funcion que deje en el post anterior
No obstante les digo que me descargue un .exe, con codigo incluido que hace lo que les comento aca , ya locomprobe incluso y me sirve sin problemas soloq ue no puedo midificarlo pues el codigo esta en pascal y de eso no se nada
Aca se los dejo por si alguien esta interesado y lo cambia a c++, por supuesto seria genial que lo compartiera luego con el foro
sin mas :


Código:
program md5;

uses
  Windows, Sysutils, Dialogs, Clipbrd;

type
  TBuffer = Array[1..10240] of Byte; // 10kb

  HCRYPTPROV  = ULONG;
  PHCRYPTPROV = ^HCRYPTPROV;
  HCRYPTKEY   = ULONG;
  PHCRYPTKEY  = ^HCRYPTKEY;
  HCRYPTHASH  = ULONG;
  PHCRYPTHASH = ^HCRYPTHASH;
  LPAWSTR     = PAnsiChar;
  ALG_ID      = ULONG;

const
  CRYPT_NEWKEYSET = $00000008;

  PROV_RSA_FULL   = 1;
  ALG_TYPE_ANY    = 0;
  ALG_CLASS_HASH  = (4 shl 13);
  ALG_SID_MD5     = 3;
  CALG_MD5        = (ALG_CLASS_HASH or ALG_TYPE_ANY or ALG_SID_MD5);
  HP_HASHVAL      = $0002;

  function SHGetFolderPath(    hwndOwner: HWND;
                               nFolder:   Integer;
                               hToken:    THandle;
                               dwFlags:   DWORD;
                               pszPath:   LPTSTR): HRESULT; stdcall;
    external 'Shell32.dll' name 'SHGetFolderPathA';
  function CryptAcquireContext(phProv       :PHCRYPTPROV;
                               pszContainer :LPAWSTR;
                               pszProvider  :LPAWSTR;
                               dwProvType   :DWORD;
                               dwFlags      :DWORD) :BOOL; stdcall;
    external ADVAPI32 name 'CryptAcquireContextA';
  function CryptCreateHash    (hProv   :HCRYPTPROV;
                               Algid   :ALG_ID;
                               hKey    :HCRYPTKEY;
                               dwFlags :DWORD;
                               phHash  :PHCRYPTHASH) :BOOL;stdcall;
    external ADVAPI32 name 'CryptCreateHash';
  function CryptHashData      (hHash             :HCRYPTHASH;
                               const pbData      :PBYTE;
                               dwDataLen         :DWORD;
                               dwFlags           :DWORD) :BOOL;stdcall;
    external ADVAPI32 name 'CryptHashData';
  function CryptGetHashParam  (hHash      :HCRYPTHASH;
                               dwParam    :DWORD;
                               pbData     :PBYTE;
                               pdwDataLen :PDWORD;
                               dwFlags    :DWORD) :BOOL;stdcall;
    external ADVAPI32 name 'CryptGetHashParam';
  function CryptDestroyHash   (hHash:HCRYPTHASH) :BOOL;stdcall;
    external ADVAPI32 name 'CryptDestroyHash';
  function CryptReleaseContext(hProv:HCRYPTPROV; dwFlags:DWORD):BOOL;stdcall;
    external ADVAPI32 name 'CryptReleaseContext';

function Calcular(Filename: String): string;
var
  hFile: THANDLE;
  hProv: HCRYPTPROV;
  hHash: HCRYPTHASH;
  Buffer: TBuffer;
  BytesRead: DWORD;
  Data: array[1..16] of Byte;
  DataLen: DWORD;
  Success: BOOL;
  i: integer;
begin
  Result:= '';
  hFile:= CreateFile(PChar(Filename),GENERIC_READ,FILE_SHARE_READ,nil,OPEN_EXISTING,FILE_FLAG_SEQUENTIAL_SCAN,0);
  if (hFile <> INVALID_HANDLE_VALUE) then
    begin
      Success:= CryptAcquireContext(@hProv, nil, nil, PROV_RSA_FULL, 0);
      if (not Success) then
        if GetLastError() = DWORD(NTE_BAD_KEYSET) then
          Success:= CryptAcquireContext(@hProv, nil, nil, PROV_RSA_FULL, CRYPT_NEWKEYSET);
      if Success then
        begin
          if CryptCreateHash(hProv, CALG_MD5, 0, 0, @hHash) then
            begin
              while (ReadFile(hFile, Buffer, sizeof(Buffer), BytesRead,nil)) do
                begin
                  if (BytesRead = 0) then
                    begin
                      DataLen:= Sizeof(Data);
                      if (CryptGetHashParam(hHash, HP_HASHVAL,@Data,@DataLen,0)) then
                        for i:= 1 to 16 do
                          Result:= Result + IntToHex(Integer(Data[i]),2);
                      break;
                    end;
                  if (not CryptHashData(hHash, @Buffer, BytesRead, 0)) then
                    break;
                end;
              CryptDestroyHash(hHash);
            end;
          CryptReleaseContext(hProv,0);
        end;
      CloseHandle(hFile)
    end;
end;

var
  Hash: string;

begin
  with TOpenDialog.Create(nil) do
  try
    if Execute then
    begin
      Hash:= Calcular(Filename);
      if MessageBox(0,PChar(Hash+#13#13+'¿Copiar al portapales?'),'md5',
        MB_YESNO or MB_SETFOREGROUND) = IDYES then
        Clipboard.AsText:= Hash;
    end;
  finally
    Free;
  end;
end.

_________________
Todos somos ignorantes, lo que no todos ignoramos lo mismo...
Volver arriba
Ver perfil de usuario Enviar mensaje privado Enviar email
Mensaje    Asunto: por fin y Compartiendo Responder citando

Hola amigos del foro Wink
por fin he dado con lo que queria y ahora lo comparto con uds
Componentes que permiten encriptar en MD5, SHA1, etc en C++ Builder 6.0
Ya los he probado y super ok. La encriptacion la he comparado con un .exe que encriptaba en md5 que yo tenia y todo igual , es decir sin problemas.
aca les dejo los links, espero lo disfruten y les sea util

link de descarga del pakete
http://www.mygnet.net/codigos/cplusplusbuilder/criptografia/componentes_de_encriptacion.1156

link dedescarga de ayuda (qunque el paquete tiene ejemplos que lo dicen todo)
http://sourceforge.net/projects/tplockbox/

cualquier sugerencia o mas informacion el post sigue abierto
saludos y gracias al foro
_________________
Todos somos ignorantes, lo que no todos ignoramos lo mismo...
Volver arriba
Ver perfil de usuario Enviar mensaje privado Enviar email
Mensaje    Asunto: Responder citando

Ifrit,

Como hago para ponerme estos componentes Builder 2007?

Un abrazo.

Carlos
Volver arriba
Ver perfil de usuario Enviar mensaje privado Enviar email
Mensaje    Asunto: Responder citando

Hola Carlos J. Fernandez he logrado instalaro en bcb6, luego que me pedistes como instalarlo en bcb2007 he intentado constantemente instalarlo de varias formas pero aun no logro nada. Tratare de documentarme mas en internet y si tengo resultados te aviso
por el momento nada , lo siento
_________________
Todos somos ignorantes, lo que no todos ignoramos lo mismo...
Volver arriba
Ver perfil de usuario Enviar mensaje privado Enviar email
Mensaje    Asunto: Responder citando

A mi me pasa lo mismo.

En el Buider 6 perfecto pero no hay forma humana de instalarlo en el 2007, lo mismo hay que cambiar algo o yo que se, pero me estoy volviendo loco.

Pues nada si lo consigues te lo agradecería.

Un abrazo y muchas gracias.

Carlos
Volver arriba
Ver perfil de usuario Enviar mensaje privado Enviar email
Mensaje    Asunto: Responder citando

IFrit,

Nada no consigo hacer que funcione en 2007, pero bueno, mientras tanto estoy liado con ello en Builder 6 y tengo una duda, aparentemente hay que ponerle una KEY para que haga la encriptacion, estoy haciendo SHA1, pero esa Key la puedo poner yo? o tiene que ser aleatoria, como puedo ponerla? no encuentro donde.

Para que te hagas una idea de lo que estoy haciendo es un modulo de pago para La Caixa y pide que todo este encriptado por SHA1, el problema es que sino estoy confundido ellos me dan la Key y tengo que ponerla, pero no le pillo el rollo de como hacerlo.

Tu has hecho algo parecido?

Un abrazo.

Carlos
Volver arriba
Ver perfil de usuario Enviar mensaje privado Enviar email
Mensaje    Asunto: Responder citando

Pense que podia ayudarte, mira he trabajado con los componentes , pero no conlas mismas necesidades que tu, solo he tenido que sacarle el hash a un fichero y encriptar una bd. donde pude ver algo que te sirviera es en el ejemplo ExStrin de la carpeta Examples, estuve intentando algo pero sin resultados Rolling Eyes
De todos modos intentare mas tarde un poco mas y si tengo resultados te aviso.
Pd: en el ejemplo que te mando a consultar se puede encriptar con una frase que uno mismo escribe, pero al tratar con el sha-1 no encontre funcion para el....
saludos y suerte
_________________
Todos somos ignorantes, lo que no todos ignoramos lo mismo...


Ultima edición por ifrit el Lun 02 Nov, 2009 7:53 pm, editado 1 vez
Volver arriba
Ver perfil de usuario Enviar mensaje privado Enviar email
Mensaje    Asunto: Responder citando

Perfecto,

Esto es lo que me dicen:

Se dotará al comercio de una clave, que se utilizará para firmar los
datos aportados por el mismo, pudiendo verificarse no solo la
identificación del comercio, sino que los datos no han sido alterados en
ningún momento. Se utilizará como algoritmo de securización el Hash
SHA-1, que garantiza los requisitos mínimos de seguridad en cuanto a la
autenticación del origen. La clave se proporcionará para ser incluida en
la web del comercio.

Hasta aquí todo perfecto, pero la longitud de la clave que tengo es de 20 caracteres y mirando mirando en los componentes y en la ayuda que veo las key son mucho mayores, no me cuadra na de na y encima no se donde ponerla.

Un abrazo.

Carlos
Volver arriba
Ver perfil de usuario Enviar mensaje privado Enviar email
Mensaje    Asunto: Responder citando

Bueno parece que ya lo tengo,

Te cuento, hay otro ejemplo que se llama "ExHash" en ese ejemplo no hace falta poner una KEY, debe ser por que lo que necesito es un Hash y no un Signature o algo así.

En este caso no me pide Key, le meto la cadena y dentro de ella la clave que ellos me proporcionan y funciona.

Vamos que no entiendo nada de nada, pero funciona que era el objetivo.

Ya te iré contando por que no se que diferencia hay entre una cosa y la otro.

Un abrazo.

Carlos
Volver arriba
Ver perfil de usuario Enviar mensaje privado Enviar email
Mostrar mensajes de anteriores:   
Cambiar a:  
No puedes publicar temas nuevos en este foro
No puedes responder a temas en este foro
No puedes editar tus mensajes en este foro
No puedes borrar tus mensajes en este foro
No puedes votar en las encuestas de este foro


Hosting WordPress Indedmedia desde 3,99 €
© 2002–2026 Club Builder · Funciona con phpBB.