Hex to base64 encode
Table of Contents
Description
The hextobase64 function encodes binary data represented as a hexadecimal string into Base64 format. It can be used for encoding encrypted data for transmission, formatting payloads, and ensuring compatibility with APIs that require Base64-encoded data.
Syntax
<variable> = <data>.hextobase64();
(or)
<variable> = hextobase64(<data>);
where,
| Parameters | Data type | Description |
|---|---|---|
| <variable> | TEXT | Holds the encoded base64 data returned by the hextobase64 function. |
| <data> | TEXT | Specifies the input hexadecimal string that will be encoded. |
Note:
- The input string must contain:
- only valid hexadecimal characters (0-9, a-f, A-F).
- an even number of characters for Base64 encoding.
- Whitespace is not allowed and will result in parsing error on saving the Deluge code.
Example 1: Verify file integrity before uploading to cloud storage
The following script generates a SHA-256 hash of a file and encodes the hash in Base64 format before the file is uploaded to cloud storage. The cloud storage service uses the Base64-encoded hash to verify the file's integrity.
// Fetch the file to be uploadedfile_data = invokeUrl[url:"https://www.w3.org/WAI/ER/tests/xhtml/testfiles/resources/pdf/dummy.pdf"type: GET];// Hash the file content using SHA-256 (returns hex string hash)hex_hash = zoho.encryption.sha256(file_data);// Convert the hash to Base64-encoded checksum as expected by the cloud storage servicechecksum = hex_hash.hexToBase64();info checksum;
where,
file_dataFile content fetched from the URL
hex_hashSHA-256 hash of the file in hex string format
checksumBase64-encoded hash that must be sent to the cloud storage service for verification
Example 2: Generate a Base64 hash for API authentication
The following script generates a SHA-256 hash of transaction details such as the transaction ID and timestamp. The hexadecimal hash is stored for audit purposes, then converted to Base64 which is sent to the API for transaction verification.
// Transaction data to be hasheddata = "transactionId=TXN98765×tamp=1718600000";// Hash the data using SHA-256 which returns hex stringsha_hex = zoho.encryption.sha256(data);// Log the hex for audit trailinfo "SHA-256 Hex: " + sha_hex;// Convert to Base64 for the API request authenticationsha_base64 = sha_hex.hexToBase64();info "SHA-256 Base64: " + sha_base64;
where,
dataVariable that holds the input string with transaction details which needs to be hashed
sha_hexSHA-256 encrypted hex output of the input data, stored for audit purposes
sha_base64Base64 format of the hex string, to be sent in the API header for authentication of the transaction request.