GenerateKeyPair: Difference between revisions

From Multi Theft Auto: Wiki
Jump to navigation Jump to search
(Created page with "__NOTOC__ {{Shared function}} {{Added feature/item|1.6.0|1.5.9|21055|This function creates a new public key and private key for encrypting data}} {{Needs Example}} Category:Incomplete ==Syntax== <syntaxhighlight lang="lua"> string, string GenerateKeyPair ( string algorithm, table options [, function callback ] ) </syntaxhighlight> ===Required Arguments=== *'''algorithm:''' The algorithm to use: ** ''RSA'': use the RSA public-key algorithm *'''options:''' table...")
 
(Key pair generation example)
 
Line 2: Line 2:
{{Shared function}}
{{Shared function}}
{{Added feature/item|1.6.0|1.5.9|21055|This function creates a new public key and private key for encrypting data}}
{{Added feature/item|1.6.0|1.5.9|21055|This function creates a new public key and private key for encrypting data}}
{{Needs Example}}
[[Category:Incomplete]]


==Syntax==  
==Syntax==  
<syntaxhighlight lang="lua">
<syntaxhighlight lang="lua">
string, string GenerateKeyPair ( string algorithm, table options [, function callback ] )
string, string generateKeyPair ( string algorithm, table options [, function callback ] )
</syntaxhighlight>  
</syntaxhighlight>  


Line 27: Line 24:


==Example==
==Example==
Adds an ''/generatekeypair'' command in which you can provide a key size to generate a key pair for encryption.
<section name="Server" class="server" show="true">
<syntaxhighlight lang="lua">
addCommandHandler("generatekeypair",
    function(player, _, algorithm, keysize)
        if algorithm then
            if keysize and tonumber(keysize) then
                local privateKey, publicKey = generateKeyPair( algorithm, { size = keysize } )
                if not privateKey then
                    outputChatBox("Failed to generate key pair. Make sure that all arguments are valid.", player, 255, 0, 0)
                else
                    outputConsole("Private Key: " .. base64Encode(privateKey),player)
                    outputConsole("Public Key: " .. base64Encode(publicKey),player)
                    outputChatBox("The key pair was successfully generated. You can find the result in the console.", player, 0, 255, 0)
                end
            else
                outputChatBox("Please specify key size in the command.", player, 255, 0, 0)
            end
        else
            outputChatBox("Please specify algorithm in the command.", player, 255, 0, 0)
        end
    end
)
</syntaxhighlight>
</section>
==See Also==
{{Utility functions}}

Latest revision as of 14:18, 25 September 2022

This function creates a new public key and private key for encrypting data

Syntax

string, string generateKeyPair ( string algorithm, table options [, function callback ] )

Required Arguments

  • algorithm: The algorithm to use:
    • RSA: use the RSA public-key algorithm
  • options: table with options for the hashing algorithm, as detailed below.

Optional Arguments

  • callback: providing a callback will run this function asynchronously, the arguments to the callback are the same as the returned values below.

Options for each hashing algorithm

  • RSA:
    • size (int), default: 0 . Size of a key.

Returns

Returns 2 strings if successful: private key and public key. Otherwise returns false

Example

Adds an /generatekeypair command in which you can provide a key size to generate a key pair for encryption.

Click to collapse [-]
Server
addCommandHandler("generatekeypair", 
    function(player, _, algorithm, keysize)
        if algorithm then
            if keysize and tonumber(keysize) then
                local privateKey, publicKey = generateKeyPair( algorithm, { size = keysize } )
                if not privateKey then
                    outputChatBox("Failed to generate key pair. Make sure that all arguments are valid.", player, 255, 0, 0)
                else
                    outputConsole("Private Key: " .. base64Encode(privateKey),player)
                    outputConsole("Public Key: " .. base64Encode(publicKey),player)
                    outputChatBox("The key pair was successfully generated. You can find the result in the console.", player, 0, 255, 0)
                end
            else
                outputChatBox("Please specify key size in the command.", player, 255, 0, 0)
            end
        else
            outputChatBox("Please specify algorithm in the command.", player, 255, 0, 0)
        end
    end
)

See Also