Encode
This function encodes data according to the parameters passed in.
Encode(algo, inputFormat, outputFormat, data)
Encode(algo, inputFormat, outputFormat, data, keyFormat, key)Examples
-
Example of calling the Encode function.
javascriptfunction main() { Log(Encode("raw", "raw", "hex", "example", "raw", "123")) // Output: 6578616d706c65 Log(Encode("raw", "raw", "hex", "example")) // Output: 6578616d706c65 Log(Encode("sha256", "raw", "hex", "example", "raw", "123")) // Output: 698d54f0494528a759f19c8e87a9f99e75a5881b9267ee3926bcf62c992d84ba Log(Encode("sha256", "raw", "hex", "example", "", "123")) // Output: 50d858e0985ecc7f60418aaf0cc5ab587f42c2570a884095a9e8ccacd0f6545c Log(Encode("sha256", "raw", "hex", "example", null, "123")) // Output: 50d858e0985ecc7f60418aaf0cc5ab587f42c2570a884095a9e8ccacd0f6545c Log(Encode("sha256", "raw", "hex", "example", "string", "123")) // Output: 698d54f0494528a759f19c8e87a9f99e75a5881b9267ee3926bcf62c992d84ba Log(Encode("raw", "raw", "hex", "123")) // Output: 313233 Log(Encode("raw", "raw", "base64", "123")) // Output: MTIz Log(Encode("sha256", "raw", "hex", "example", "hex", "313233")) // Output: 698d54f0494528a759f19c8e87a9f99e75a5881b9267ee3926bcf62c992d84ba Log(Encode("sha256", "raw", "hex", "example", "base64", "MTIz")) // Output: 698d54f0494528a759f19c8e87a9f99e75a5881b9267ee3926bcf62c992d84ba }pythondef main(): Log(Encode("raw", "raw", "hex", "example", "raw", "123")) # Output: 6578616d706c65 Log(Encode("raw", "raw", "hex", "example", "", "")) # Output: 6578616d706c65 Log(Encode("sha256", "raw", "hex", "example", "raw", "123")) # Output: 698d54f0494528a759f19c8e87a9f99e75a5881b9267ee3926bcf62c992d84ba Log(Encode("sha256", "raw", "hex", "example", "", "123")) # Output: 50d858e0985ecc7f60418aaf0cc5ab587f42c2570a884095a9e8ccacd0f6545c Log(Encode("sha256", "raw", "hex", "example", "string", "123")) # Output: 698d54f0494528a759f19c8e87a9f99e75a5881b9267ee3926bcf62c992d84ba Log(Encode("raw", "raw", "hex", "123", "", "")) # Output: 313233 Log(Encode("raw", "raw", "base64", "123", "", "")) # Output: MTIz Log(Encode("sha256", "raw", "hex", "example", "hex", "313233")) # Output: 698d54f0494528a759f19c8e87a9f99e75a5881b9267ee3926bcf62c992d84ba Log(Encode("sha256", "raw", "hex", "example", "base64", "MTIz")) # Output: 698d54f0494528a759f19c8e87a9f99e75a5881b9267ee3926bcf62c992d84barustfn main() { // In Rust, all 6 parameters of the Encode() function are required; when not encrypting, simply // pass empty strings for keyFormat and key Log!(Encode("raw", "raw", "hex", "example", "raw", "123")); // Output: 6578616d706c65 Log!(Encode("raw", "raw", "hex", "example", "", "")); // Output: 6578616d706c65 Log!(Encode("sha256", "raw", "hex", "example", "raw", "123")); // Output: 698d54f0494528a759f19c8e87a9f99e75a5881b9267ee3926bcf62c992d84ba Log!(Encode("sha256", "raw", "hex", "example", "", "123")); // Output: 50d858e0985ecc7f60418aaf0cc5ab587f42c2570a884095a9e8ccacd0f6545c Log!(Encode("sha256", "raw", "hex", "example", "string", "123")); // Output: 698d54f0494528a759f19c8e87a9f99e75a5881b9267ee3926bcf62c992d84ba Log!(Encode("raw", "raw", "hex", "123", "", "")); // Output: 313233 Log!(Encode("raw", "raw", "base64", "123", "", "")); // Output: MTIz Log!(Encode("sha256", "raw", "hex", "example", "hex", "313233")); // Output: 698d54f0494528a759f19c8e87a9f99e75a5881b9267ee3926bcf62c992d84ba Log!(Encode("sha256", "raw", "hex", "example", "base64", "MTIz")); // Output: 698d54f0494528a759f19c8e87a9f99e75a5881b9267ee3926bcf62c992d84ba } -
The parameter
algoalso supports the following values: "text.encoder.utf8", "text.decoder.utf8", "text.encoder.gbk", "text.decoder.gbk", which are used to encode and decode strings.javascriptfunction main(){ var ret1 = Encode("text.encoder.utf8", "raw", "hex", "\u4f60\u597d") Log(ret1) // Output: e4bda0e5a5bd var ret2 = Encode("text.decoder.utf8", "hex", "string", ret1) Log(ret2) var ret3 = Encode("text.encoder.gbk", "raw", "hex", "\u4f60\u597d") Log(ret3) // Output: c4e3bac3 var ret4 = Encode("text.decoder.gbk", "hex", "string", ret3) Log(ret4) }pythondef main(): ret1 = Encode("text.encoder.utf8", "raw", "hex", "\u4f60\u597d", "", "") Log(ret1) # Output: e4bda0e5a5bd ret2 = Encode("text.decoder.utf8", "hex", "string", ret1, "", "") Log(ret2) ret3 = Encode("text.encoder.gbk", "raw", "hex", "\u4f60\u597d", "", "") Log(ret3) # Output: c4e3bac3 ret4 = Encode("text.decoder.gbk", "hex", "string", ret3, "", "") Log(ret4)rustfn main() { // In Rust, all 6 parameters of the Encode() function are required; when not encrypting, pass // empty strings for keyFormat and key let ret1 = Encode("text.encoder.utf8", "raw", "hex", "\u{4f60}\u{597d}", "", ""); Log!(ret1); // Output: e4bda0e5a5bd let ret2 = Encode("text.decoder.utf8", "hex", "string", &ret1, "", ""); Log!(ret2); let ret3 = Encode("text.encoder.gbk", "raw", "hex", "\u{4f60}\u{597d}", "", ""); Log!(ret3); // Output: c4e3bac3 let ret4 = Encode("text.decoder.gbk", "hex", "string", &ret3, "", ""); Log!(ret4); }
Returns
| Type | Description |
string | Encoded and encrypted data. |
Arguments
| Name | Type | Required | Description |
algo | string | Yes | The algorithm for encoding, hashing, encryption or compression, for example |
inputFormat | string | Yes | Used to specify the data format of the |
outputFormat | string | Yes | Used to specify the output data format. The |
data | string | Yes | Data to be processed. |
keyFormat | string | No | Used to specify the data format of the |
key | string | No | Key used for When the When the |
See Also
Remarks
The Encode() function is only supported in live trading. If the key and keyFormat parameters are not passed in, no key encryption is performed.
Values of algo:
| Category | Values |
|---|---|
| No algorithm | raw |
| Signing | sign, signTx |
| Hash (MD, SHA-1, SHA-2) | md4, md5, sha1, sha224, sha256, sha384, sha512, sha512.384, sha512.256, sha512.224 |
| Hash (SHA-3, Keccak) | keccak256, sha3.224, sha3.256, sha3.384, sha3.512, sha3.keccak256, sha3.keccak512 |
| Hash (others) | ripemd160, blake2b.256, blake2b.512, blake2s.128, blake2s.256 |
| String encoding and decoding | text.encoder.utf8, text.decoder.utf8, text.encoder.gbk, text.decoder.gbk |
| Other character sets | text.encoder.<charset>, text.decoder.<charset>, for example text.decoder.big5 |
| Ed25519 | ed25519, ed25519.seed; a hash algorithm can be given, for example ed25519.md5, ed25519.sha512 |
| AES encryption and decryption | aes.encrypt, aes.decrypt, with an optional mode suffix .cbc (default), .cfb, .ecb, for example aes.decrypt.ecb |
| Compression and decompression | gzip.deflate, gzip.inflate, zlib.deflate, zlib.inflate, raw.deflate, raw.inflate |
The AES key is passed in key and must be 16, 24 or 32 bytes long (AES-128, AES-192, AES-256); the CBC and CFB modes use the first 16 bytes of the key as the IV, with PKCS7 padding.