Pascal (Lazarus/Delphi)
Pascal (Lazarus/Delphi)
Set the FTP Password from a SecureString
See more FTP Examples
Demonstrates the Chilkat Ftp2.SetSecurePassword method, which sets the login password from a SecureString. The only argument is the SecureString. It is equivalent to setting the Password property but avoids holding the password as an ordinary immutable string.
Background: An ordinary string password can linger in memory (and in memory dumps) because strings are immutable and copied freely. A
SecureString keeps the value encrypted under a session key and clears it deterministically, reducing that exposure — a sensible upgrade for a credential the process holds for the life of the connection. Populate it from a runtime source rather than a hard-coded literal.Chilkat Pascal (Lazarus/Delphi) Downloads
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.Ftp2,
Chilkat.SecureString;
// ---------------------------------------------------------------------------
procedure RunDemo;
var
success: Boolean;
ftp: TFtp2;
password: string;
securePassword: TSecureString;
begin
success := False;
// Demonstrates the Ftp2.SetSecurePassword method, which sets the login password from a
// SecureString. The only argument is the SecureString. This avoids holding the password as an
// ordinary immutable string.
ftp := TFtp2.Create;
ftp.Hostname := 'ftp.example.com';
ftp.Username := 'myFtpLogin';
// Normally you would not hard-code the password in source. You should instead obtain it
// from an interactive prompt, environment variable, or a secrets vault.
password := 'myPassword';
// Place the password into a SecureString, which keeps it encrypted in memory.
securePassword := TSecureString.Create;
securePassword.Append(password);
// Setting the secure password is equivalent to setting the Password property, but more
// protective.
success := ftp.SetSecurePassword(securePassword);
if (success = False) then
begin
WriteLn(ftp.LastErrorText);
Exit;
end;
success := ftp.Connect();
if (success = False) then
begin
WriteLn(ftp.LastErrorText);
Exit;
end;
WriteLn('Connected using a secure password.');
success := ftp.Disconnect();
if (success = False) then
begin
WriteLn(ftp.LastErrorText);
Exit;
end;
ftp.Free;
securePassword.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.