Générer un hash SHA‑1
Par Delphi Source - March 21, 2026 · Vues: 20 · Catégories: Snippets · Tags: #Indy #Hash

Générer un hash SHA‑1

Calculer le hash SHA‑1 d’une chaîne de caractères.

C’est une opération courante lorsqu’on veut vérifier l’intégrité d’une donnée, créer une empreinte unique ou sécuriser un identifiant.

uses
  IdHashSHA1, IdGlobal;

function GetSHA1Hash(const InputString: string): string;
var
  SHA1: TIdHashSHA1;
  HashBytes: TIdBytes;
  I: Integer;
begin
  SHA1 := TIdHashSHA1.Create;
  try
    HashBytes := SHA1.HashString(InputString, IndyTextEncoding_8Bit);
    Result := '';
    for I := 0 to Length(HashBytes) - 1 do
      Result := Result + IntToHex(HashBytes[I], 2);
  finally
    SHA1.Free;
  end;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  ShowMessage('SHA-1 : ' + GetSHA1Hash('Bonjour'));
end:


Un petit clic (J'aime) qui fait plaisir !