- Documentation
- Shared functions
- encodeString
Function
encodeString
Encrypts, encodes or compresses text. Works on the server and on the client. Algorithms: tea (raw TEA bytes, no base64), aes128 (the key must be exactly 16 characters; it also returns a random iv needed to decrypt), rsa (a public key from generateKeyPair), base64 (variant url), base32 (the Crypto++ alphabet, variant hex = RFC 4648 base32hex), zlib (format gzip, zlib or raw, level 0-9, strategy). With base64, base32 and zlib the options table can be left out. With a callback the result goes to it in the next frame and the function returns true.
Syntax
string encodeString(string algorithm, string input [, table options, function callback])Arguments
algorithmstring"tea","aes128","rsa","base64","base32"or"zlib".inputstring- The data.
optionstable, optionaltea/aes128/rsa:{ key = "..." };base64:{ variant = "url" };base32:{ variant = "hex" };zlib:{ format = "gzip"|"zlib"|"raw" or windowBits, compression = 0-9, strategy = "default"|"filtered"|"huffman"|"rle"|"fixed" }(default gzip, 9).callbackfunction, optional- The function gets the result (with
aes128also theiv).
Returns
The result (with aes128 also the iv) or false and a warning.
Example
local key = "0123456789abcdef"
local data, iv = encodeString("aes128", "tajne dane", { key = key })
local plain = decodeString("aes128", data, { key = key, iv = iv })
-- duze dane przed wyslaniem na serwer
local packed = encodeString("zlib", toJSON(stats), { format = "zlib", compression = 6 })
triggerServerEvent("stats:save", localPlayer, packed)
See also
decodeStringDecrypts, decodes or unpacks data fromencodeString, also data made on another server. Withaes128you pass the key and theiv, withrsathe private key; withzlibthe format can be left out: gzip, zlib and raw are recognised by their header. Characters outside thebase64andbase32alphabets are skipped. Works on the server and on the client.teaEncodeEncrypts text with the TEA cipher and returns base64, compatible with the standard TEA used by existing scripts, so previously encrypted text can be read here. Only the first 16 bytes of the key count. TEA is not a strong cipher; useencodeStringwithaes128for serious data.generateKeyPairCreates an RSA key pair: private (PKCS#8) and public (X.509), both as binary DER data. To store them in a file or a database turn them into text, e.g. withbase64Encode.encodeString/decodeStringalso accept PEM keys. Size 512-8192 bits (default 2048; on the client a multiple of 64). With a callback the keys go to it.