encodeString | Multi Theft Auto: Wiki Skip to content

encodeString

Client-side
Server-side
Shared
Needs checking

This function was partially migrated from the old wiki. Please review manually:

  • Missing section: Options for each algorithm
  • Missing section: Returns for each algorithm

Manual Review Required

Please finish this page using the corresponding Old Wiki article. Go to Contribution guidelines for more information.


This function encodes a string using the specified algorithm. The counterpart of this function is decodeString.

Tip

If it doesn't matter which algorithm you use for the encoding, keep in mind that aes128 uses hardware acceleration so it may outperform the tea algorithm on most processors.

Syntax

string encodeString ( string algorithm, string input, table options, function callback )
Required Arguments
  • algorithm: The algorithm to use.
  • input: The input to encode.
  • options: A table with options and other necessary data for the algorithm, as detailed below.
  • callback: providing a callback will run this function asynchronously, the arguments to the callback are the same as the returned values below.

Returns

  • string: value

Code Examples

shared

Adds an/encodecommand in which you can provide an algorithm, key and data to encode. Below is the example provided as both server-side and client-side variations.

addCommandHandler("encode", function(player, _, algorithm, key, ...)
if not algorithm or not key then
outputChatBox("Invalid algorithm and/or key.", player, 255, 0, 0)
return
end
local text = table.concat({...}, " ")
if type(text) ~= "string" or text == "" then
outputChatBox("Please specify text in the command.", player, 255, 0, 0)
return
end
local encoded = encodeString(algorithm, text, { key = key })
if not encoded then
outputChatBox("Failed to encode. Make sure that all arguments are valid.", player, 255, 0, 0)
return
end
outputChatBox("The result of " .. algorithm .. " encoding is: " .. encoded, player)
end)