Sample code for 30+ languages & platforms
Pascal (Lazarus/Delphi)

Set the OAEP Hash Algorithm for Encrypted Email

See more Email Object Examples

Demonstrates the Chilkat Email.OaepHash property, which selects the hash algorithm used by OAEP padding when encrypting email with RSAES-OAEP. Valid values are sha1, sha256, sha384, and sha512; the default is sha256. This property is used only when OaepPadding is true, so this example enables OAEP first.

Background: OAEP ("Optimal Asymmetric Encryption Padding") uses a hash function internally as part of the randomized padding it wraps around the data before RSA encryption. The choice of hash affects security and, importantly, interoperability: the sender and recipient must use the same OAEP hash, or decryption fails. That is why this is exposed as an explicit setting rather than left implicit.

Chilkat Pascal (Lazarus/Delphi) Downloads

Pascal (Lazarus/Delphi)
program ChilkatDemo;

// Demonstrates using the Chilkat Pascal wrapper via the C bridge DLL.
// Builds as a console application under Lazarus (FPC) or Delphi.

{$IFDEF FPC}
  {$MODE DELPHI}
{$ENDIF}
{$APPTYPE CONSOLE}

uses
  {$IFDEF UNIX}
  cthreads,
  {$ENDIF}
  SysUtils,
  CkDllLoader,
  Chilkat.Email;

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

procedure RunDemo;
var
  email: TEmail;

begin
  //  Demonstrates the Email.OaepHash property, which selects the hash algorithm used by
  //  OAEP padding when encrypting email with RSAES-OAEP.  Valid values: sha1, sha256,
  //  sha384, sha512.  The default is sha256.  It is used only when OaepPadding is true.

  email := TEmail.Create;
  email.Subject := 'Encrypted with RSAES-OAEP';

  //  OAEP padding must be enabled for the OAEP hash to take effect.
  email.OaepPadding := True;
  email.OaepHash := 'sha256';

  WriteLn('OaepHash = ' + email.OaepHash);


  email.Free;

end;

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

begin

  try
    RunDemo;
  except
    on E: Exception do
      WriteLn('Unhandled exception: ', E.ClassName, ': ', E.Message);
  end;

  WriteLn;
  {$IFDEF MSWINDOWS}
  WriteLn('Press Enter to exit...');
  ReadLn;
  {$ENDIF}
end.