SecureString Lua Reference Documentation
chilkat.SecureString
Current Version: 11.7.0
Markdown version of this page for AI tools (v 11.7.0) · all AI docs
Chilkat.SecureString
Store sensitive text in a form that is decrypted only when needed for
access, modification, comparison, or hashing.
Append clear-text strings,
Set
Set
Retrieve the current hash value in an encoding such as hex or Base64, or
verify a supplied encoded hash with
Compare two secure strings with Store, append, compare, hash, verify, and securely access sensitive string values.
Chilkat.SecureString holds sensitive text in an encrypted
in-memory form while still allowing controlled operations on the value. It
can append clear-text strings, append text from a
StringBuilder, append another SecureString, load
text from a file, return the clear-text value when needed, compare secure
strings, mark the value read-only, maintain a hash of the current value, and
verify a supplied hash against that maintained value.
Encrypted in-memory value
Append safely
StringBuilder content, or another
SecureString; the value is then re-encrypted.
Read-only protection
ReadOnly to prevent later modification of the secure
string.
Hash maintenance
MaintainHash to a supported hash algorithm such as
sha256, sha512, sha1, or
md5.
Hash output and verification
VerifyHash.
Compare and access
SecStrEquals, or call
Access when the clear-text value is required.
SecureString, load or append the sensitive text, set
MaintainHash if later verification is needed, and optionally set
ReadOnly after the final value is established. Use
Access only at the point where the clear-text string is actually
needed, and check LastErrorText if an append, file load, hash,
or verification operation fails.
VerifyHash depends on MaintainHash having already
been set so the object maintains an internal hash for the current secure
string value.
Object Creation
local chilkat = require("chilkat")
local secure_string = chilkat.SecureString.new()Creates the object (chilkat.SecureString() does the same). Lua's garbage collector frees it when it is no longer referenced; secure_string:dispose() frees it at once, and in Lua 5.4 and later local secure_string <close> = chilkat.SecureString.new() frees it when the block ends. Calling a method of a disposed object raises an error.
Errors
A method that fails returns nil followed by the error text, as io.open does: local body, err = secure_string:method(...), where err is the object's last_error_text. Wrap the call in assert() to raise an error with that text instead. Methods that answer a question return true or false, numbers are returned as they are (check last_method_success where the description says a value means failure), and property reads and writes never fail for Chilkat reasons.
Misuse raises a Lua error: a wrong argument type or a missing argument (bad argument #1 to ...), an unknown property name, a write to a read-only property, an out-of-range integer, or an object of the wrong class.
Names are Chilkat's in snake_case: QuickGetStr is quick_get_str, LastErrorText is last_error_text. Properties are fields (secure_string.verbose_logging = true) and methods use the colon (secure_string:method(...)). Strings are UTF-8; binary data is a Lua string of bytes. Indexes are 0-based, as in every Chilkat API. Unlocking: call chilkat.unlock_bundle("...") once at startup.
Properties
DebugLogFilePath
local strVal = secure_string.debug_log_file_path
secure_string.debug_log_file_path = strVal
If set to a file path, this property logs the LastErrorText of each Chilkat method or property call to the specified file. This logging helps identify the context and history of Chilkat calls leading up to any crash or hang, aiding in debugging.
Enabling the VerboseLogging property provides more detailed information. This property is mainly used for debugging rare instances where a Chilkat method call causes a hang or crash, which should generally not happen.
Possible causes of hangs include:
- A timeout property set to 0, indicating an infinite timeout.
- A hang occurring within an event callback in the application code.
- An internal bug in the Chilkat code causing the hang.
LastErrorHtml
local strVal = secure_string.last_error_html
Provides HTML-formatted information about the last called method or property. If a method call fails or behaves unexpectedly, check this property for details. Note that information is available regardless of the method call's success.
topLastErrorText
local strVal = secure_string.last_error_text
Provides plain text information about the last called method or property. If a method call fails or behaves unexpectedly, check this property for details. Note that information is available regardless of the method call's success.
LastErrorXml
local strVal = secure_string.last_error_xml
Provides XML-formatted information about the last called method or property. If a method call fails or behaves unexpectedly, check this property for details. Note that information is available regardless of the method call's success.
topLastMethodSuccess
local boolVal = secure_string.last_method_success
secure_string.last_method_success = boolVal
Indicates the success or failure of the most recent method call: true means success, false means failure. This property remains unchanged by property setters or getters. This method is present to address challenges in checking for null or Nothing returns in certain programming languages. Note: This property does not apply to methods that return integer values or to boolean-returning methods where the boolean does not indicate success or failure.
MaintainHash
local strVal = secure_string.maintain_hash
secure_string.maintain_hash = strVal
If set to the name of a hash algorithm, then a hash of the current string value is maintained. This allows for the hash to be verified via the VerifyHash method. Possible hash algorithm names are sha1, sha256, sha384, sha512, md5, md2, ripemd160, ripemd128,ripemd256, and ripemd320.
ReadOnly
local boolVal = secure_string.read_only
secure_string.read_only = boolVal
Can be set to true to make this secure string read-only (cannot be modified).
VerboseLogging
local boolVal = secure_string.verbose_logging
secure_string.verbose_logging = boolVal
If set to true, then the contents of LastErrorText (or LastErrorXml, or LastErrorHtml) may contain more verbose information. The default value is false. Verbose logging should only be used for debugging. The potentially large quantity of logged information may adversely affect peformance.
Version
local strVal = secure_string.version
Methods
Access
local str, err = secure_string:access()
Returns the clear-text string value.
Returns the string, or nil, err on failure (or when nothing is found). The second return value, err, is the object's last_error_text; wrap the call in assert() to raise an error instead.
Append
-- ok is true, or nil and err on failure
local ok, err = secure_string:append(str)
Appends a clear-text string to this secure string. The in-memory data will be decrypted, the string will be appended, and then it will be re-encrypted. Can return false if the string has been marked as read-only via the ReadOnly property.
Returns true on success, nil, err on failure. The second return value, err, is the object's last_error_text; wrap the call in assert() to raise an error instead.
AppendSb
-- ok is true, or nil and err on failure
local ok, err = secure_string:append_sb(sb)
Appends a clear-text string contained in a StringBuilder to this secure string. The in-memory data will be decrypted, the string will be appended, and then it will be re-encrypted. Can return false if the string has been marked as read-only via the ReadOnly property.
Returns true on success, nil, err on failure. The second return value, err, is the object's last_error_text; wrap the call in assert() to raise an error instead.
AppendSecure
-- ok is true, or nil and err on failure
local ok, err = secure_string:append_secure(sec_str)
Appends the contents of a secure string to this secure string. The in-memory data will be decrypted, the secure string will be appended, and then it will be re-encrypted. Can return false if this string has been marked as read-only via the ReadOnly property.
Returns true on success, nil, err on failure. The second return value, err, is the object's last_error_text; wrap the call in assert() to raise an error instead.
HashVal
-- str is a string, or nil and err on failure
local str, err = secure_string:hash_val(encoding)
Returns the hash value for the current value of this secure string. The encoding specifies the encoding to be used. It can be any of the binary encoding algorithms, such as base64, hex, and many more listed at Chilkat Binary Encodings
Returns the string, or nil, err on failure (or when nothing is found). The second return value, err, is the object's last_error_text; wrap the call in assert() to raise an error instead.
LoadFile
-- charset is a string
-- ok is true, or nil and err on failure
local ok, err = secure_string:load_file(path, charset)
Loads the contents of a file into this secure string. The current contents of this object are replaced with the new text from the file.
Returns true on success, nil, err on failure. The second return value, err, is the object's last_error_text; wrap the call in assert() to raise an error instead.
SecStrEquals
-- ok is true, or nil and err on failure
local ok, err = secure_string:sec_str_equals(sec_str)
Returns true if the sec_str equals the contents of this secure string.
Returns true on success, nil, err on failure. The second return value, err, is the object's last_error_text; wrap the call in assert() to raise an error instead.
VerifyHash
-- encoding is a string
-- ok is true, or nil and err on failure
local ok, err = secure_string:verify_hash(hash_val, encoding)
Verifies the hash_val against the hash value stored for the current value of this secure string. The MaintainHash property must've previously been set for this secure string to maintain an internal hash. The encoding specifies the encoding of the hash_val. It can be any of the binary encoding algorithms, such as base64, hex, and many more listed at Chilkat Binary Encodings
Returns true on success, nil, err on failure. The second return value, err, is the object's last_error_text; wrap the call in assert() to raise an error instead.