PureBasic
PureBasic
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 PureBasic Downloads
IncludeFile "CkFtp2.pb"
IncludeFile "CkSecureString.pb"
Procedure ChilkatExample()
success.i = 0
; 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.i = CkFtp2::ckCreate()
If ftp.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
CkFtp2::setCkHostname(ftp, "ftp.example.com")
CkFtp2::setCkUsername(ftp, "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.s = "myPassword"
; Place the password into a SecureString, which keeps it encrypted in memory.
securePassword.i = CkSecureString::ckCreate()
If securePassword.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
CkSecureString::ckAppend(securePassword,password)
; Setting the secure password is equivalent to setting the Password property, but more
; protective.
success = CkFtp2::ckSetSecurePassword(ftp,securePassword)
If success = 0
Debug CkFtp2::ckLastErrorText(ftp)
CkFtp2::ckDispose(ftp)
CkSecureString::ckDispose(securePassword)
ProcedureReturn
EndIf
success = CkFtp2::ckConnect(ftp)
If success = 0
Debug CkFtp2::ckLastErrorText(ftp)
CkFtp2::ckDispose(ftp)
CkSecureString::ckDispose(securePassword)
ProcedureReturn
EndIf
Debug "Connected using a secure password."
success = CkFtp2::ckDisconnect(ftp)
If success = 0
Debug CkFtp2::ckLastErrorText(ftp)
CkFtp2::ckDispose(ftp)
CkSecureString::ckDispose(securePassword)
ProcedureReturn
EndIf
CkFtp2::ckDispose(ftp)
CkSecureString::ckDispose(securePassword)
ProcedureReturn
EndProcedure