TeaEncode: Difference between revisions

From Multi Theft Auto: Wiki
Jump to navigation Jump to search
No edit summary
m (Example updated)
Line 21: Line 21:
<syntaxhighlight lang="lua">
<syntaxhighlight lang="lua">
[lua]
[lua]
function encrypt(cmd, theString, key)
function encryptString( cmd, theString, theKey )
     if theString then
     if ( theString ) then
         if not key then
         if ( not theKey ) then
             key = string.rep(string.char(math.random(string.byte("a"), string.byte("Z"))), 10) -- Generate a random key of 10 characters
             theKey = string.rep( string.char( math.random( string.byte( "a" ), string.byte( "Z" ) ) ), 10 ) -- Generate a random key of 10 characters if no key was passed in as an argument
         end
         end
         local encodedString = teaEncode(theString, key) -- Encode the string with the key
         local encodedString = teaEncode( theString, theKey ) -- Encode the string with the key
         outputChatBox("The encoded string is: " .. encodedString .. " (used key: " .. key .. ")")
         outputChatBox( "The encoded string is: " .. encodedString .. " (used key: " .. key .. ")" )
     else
     else
         outputChatBox("Syntax: /" .. cmd .. " (string) [key].")
         outputChatBox( "Syntax: /" .. cmd .. " [string] <[key]>" )
     end
     end
end
end
addCommandHandler("teaencrypt", encrypt)
addCommandHandler( "teaencrypt", encryptString )
</syntaxhighlight>
</syntaxhighlight>
</section>
</section>

Revision as of 21:54, 4 August 2014

ADDED/UPDATED IN VERSION 1.3.5 r6056:

This functions performs the Tiny Encryption Algorithm on the given string and returns the base64 representation of the encrypted block of data.

Syntax

string teaEncode ( string data, string key )

Required Arguments

  • data: The block of data you want to encrypt
  • key: The key that should be used for encryption (Only first 16 characters are used)

Returns

Returns the base64 representation of the encrypted data if the encryption process was successfully completed, false otherwise.

Example

Click to collapse [-]
Client

This example creates a /teaencrypt command, which performs the TEA on a given string with the specified key, if any.

[lua]
function encryptString( cmd, theString, theKey )
    if ( theString ) then
        if ( not theKey ) then
            theKey = string.rep( string.char( math.random( string.byte( "a" ), string.byte( "Z" ) ) ), 10 ) -- Generate a random key of 10 characters if no key was passed in as an argument
        end
        local encodedString = teaEncode( theString, theKey ) -- Encode the string with the key
        outputChatBox( "The encoded string is: " .. encodedString .. " (used key: " .. key .. ")" )
    else
        outputChatBox( "Syntax: /" .. cmd .. " [string] <[key]>" )
    end
end
addCommandHandler( "teaencrypt", encryptString )

See Also