Hashing

This application demonstrates how to use the TRwBrypt Hashing algorithms

Example code:


procedure TFrmMain.btnHashClick(Sender: TObject);
var
  Encoding: TRwBinEncoding;
  Provider: TRwBCryptHashProvider;
  Digest1: TRwBytes;
  Digest2: TBytes;
  Data: RawByteString;
  Source: TRwBytes;
begin
  if rbtnBase64.Checked then
    Encoding := beBase64
  else
    Encoding := beHexEncoded;

  // Use TRwBCrypt to get easy access to all available BCrypt methods
  edtSha1.Text := TRwBCrypt.Hash.SHA1.HashString(edtSource.Text, True).Encode(Encoding);

  // It's also posibble to use the TRwBCryptHashProvider directly
  // to get access to all hash methods
  edtSha256.Text := TRwBCryptHashProvider.Hash(calgSHA1).HashString(edtSource.Text, True).Encode(Encoding);

  // It's also possible to instantiate a provider
  // and use the Hash methods of the provider
  Provider := TRwBCryptHashProvider.Create(BCRYPT_DEFAULT_PROVIDER);
  edtSha384.Text := Provider.SHA384.HashString(edtSource.Text, True).Encode(Encoding);

  // A single instantiated provider can be used to create multiple hashes
  // This saves time creating a provider for every hash operation (-> is better performance)
  edtSha512.Text := Provider.SHA512.HashString(edtSource.Text, True).Encode(Encoding);

  // The Digest (=Hash Result) is a TRwBytes
  Digest1 := Provider.MD2.HashString(edtSource.Text, True);
  edtMD2.Text := Digest1.Encode(Encoding);

  // Note that the TRwBytes can be used as a TBytes
  Digest2 := Provider.MD4.HashString(edtSource.Text, True);
  edtMD4.Text := TRwNetEncoding.GetEncoding(Encoding).EncodeBytesToString(Digest2);

  // Bytes Hashing
  Data := TRwEncoding.ConvertW(edtSource.Text, CP_UTF8);
  Source := BytesOf(Data); // Note: Source is TRwBytes but takes an assignment of TBytes
  edtMD5.Text := Provider.MD5.HashBytes(Source).Encode(Encoding);

  // HMAC requires a password
  edtHMAC.Text := TRwBCrypt.Hash.HMAC(calgSHA1, 'password').HashString(edtSource.Text).Encode(Encoding);
end;


Back to Examples