Delphi-PRAXiS

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   Neuen Beitrag zur Code-Library hinzufügen (https://www.delphipraxis.net/33-neuen-beitrag-zur-code-library-hinzufuegen/)
-   -   Mini-SHA1 (https://www.delphipraxis.net/155688-mini-sha1.html)

himitsu 2. Jun 2010 13:50

Mini-SHA1
 
Vielleicht hat es schonmal jemand gesehn (hab's ja in einigen Projekten verbaut).
Delphi-Quellcode:
type
  SHA_CTX = packed Record
    Unknown: Array[0.. 5] of LongWord;
    State:  Array[0.. 4] of LongWord;
    Count:  UInt64;
    Buffer: Array[0..63] of Byte;
  End;
  pSHA_CTX = ^SHA_CTX;
  SHA_RES = Array[0..4] of LongWord;

Procedure SHA_CTX.Init(Var Context: SHA_CTX); StdCall;
  External 'advapi32.dll' Name 'A_SHAInit';

Procedure SHA_CTX.Update(Var Context: SHA_CTX; Input: Pointer; inLen: LongWord); StdCall;
  External 'advapi32.dll' Name 'A_SHAUpdate';

Procedure SHA_CTX.GetResult(Var Context: SHA_CTX; Out Result: SHA_RES); StdCall;
  External 'advapi32.dll' Name 'A_SHAFinal';
Und da ich Records so sehr mag, hatte ich's jetzt nochmal etwas überarbeitet :cheer:
Delphi-Quellcode:
type
{$ALIGN 4}
  TSHA1Res = Array[0..4] of LongWord;
  TSHA1 = Packed Record
    Procedure Init;                                     StdCall;
    Procedure Update (Input: Pointer; inLen: LongWord); StdCall;
    Procedure Finalize(Result: TSHA1Res);               StdCall;
    Function doFinalize:     TSHA1Res;                Inline;

    Class Function toBase64(Res: TSHA1Res): String;                    Static;
    Class Function Calc   (Input: Pointer; inLen: LongWord): TSHA1Res; Static;
    Class Function CalcX  (Input: Pointer; inLen: LongWord): String;  Static;
  Public
    Unknown: Array[0.. 5] of LongWord;
    State:  Array[0.. 4] of LongWord;
    Count:  UInt64;
    Buffer: Array[0..63] of Byte;
  End;
  {$ALIGN 8}

Procedure TSHA1.Init{Var Context: SHA_CTX}; StdCall;
  External 'advapi32.dll' Name 'A_SHAInit';

Procedure TSHA1.Update{Var Context: SHA_CTX; Input: Pointer; inLen: LongWord}; StdCall;
  External 'advapi32.dll' Name 'A_SHAUpdate';

Procedure TSHA1.Finalize{Var Context: SHA_CTX; Out Result: SHA_RES}; StdCall;
  External 'advapi32.dll' Name 'A_SHAFinal';

Function TSHA1.doFinalize: TSHA1Res;
  Begin
    Finalize(Result);
  End;

Class Function TSHA1.toBase64(Res: TSHA1Res): String;
  Const Base64: Array[0..63] of Char = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';

  Var R: packed Record Res: TSHA1Res; Fill: AnsiChar; End;
    A:  packed Array[0..20] of Byte absolute R;
    i:  Integer;

  Begin
    R.Res := Res;
    R.Fill := '=';
    SetLength(Result, 28);
    For i := 0 to 6 do Begin
      Result[i * 4 + 1] := Base64[                         (A[i * 3 + 0] shr 2) and 63];
      Result[i * 4 + 2] := Base64[((A[i * 3 + 0] shl 4) or (A[i * 3 + 1] shr 4)) and 63];
      Result[i * 4 + 3] := Base64[((A[i * 3 + 1] shl 2) or (A[i * 3 + 2] shr 6)) and 63];
      Result[i * 4 + 4] := Base64[  A[i * 3 + 2]                                and 63];
    End;
    Result[28] := '=';
  End;

Class Function TSHA1.Calc(Input: Pointer; inLen: LongWord): TSHA1Res;
  Var X: TSHA1;

  Begin
    X.Init;
    X.Update(Input, inLen);
    X.Finalize(Result);
  End;

Class Function TSHA1.CalcX(Input: Pointer; inLen: LongWord): String;
  Begin
     Result := toBase64(Calc(Input, inLen));
  End;
Delphi-Quellcode:
var
  SHA1: TSHA1;
  Result: TSHA1Res

SHA1.Init;
SHA1.Update(P, len);
SHA1.Finalize(Result);
// irgendwas mit Result machen
Delphi-Quellcode:
ShowMessage(TSHA1.CalcX(P, len));


Alle Zeitangaben in WEZ +1. Es ist jetzt 20:18 Uhr.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024 by Thomas Breitkreuz