Synergy/DE 11.1.1c Off-Line Documentation Now Available
February 10, 2020SDI Developer Build 2704 Now Available
February 21, 2020In Synergy/DE 11.1, we added a new class, Synergex.SynergyDE.DataEncoding, for performing base64 encoding and decoding. Base64 encoding allows binary data to be transferred or stored when only textual data can be used. Previously, you could use some Synergy DBL routines available in CodeExchange to perform this operation; now, you can use the 12 methods in the new DataEncoding class to encode and decode binary data.
Base64 encoding takes three bytes of binary data and encodes it into four bytes of printable ASCII, which includes the letters A through Z (both upper- and lowercase), the numbers 0 through 9, and the plus sign and forward slash characters. When the number of bytes is not an even multiple of three, the encoded output is padded with one or two equal signs.
When encoding data, make sure to allow enough space for the result. The encoded data’s length depends on the length of the raw data. The formula for calculating the length of the encoded data is
((length + 2) / 3) * 4
The formula for calculating the length of the decoded data is
((encoded_length / 4) * 3) – (# of trailing equal signs)
The DataEncoding class includes the following methods:
- Encoded_alpha = ToBase64(alpha)
- Encoded_alpha = ToBase64(alpha, offset, length)
- Encoded_string = ToBase64(string)
- Encoded_string = ToBase64(string, offset, length)
- Decoded_alpha = FromBase64(encoded_alpha)
- Decoded_string = FromBase64(encoded_string)
The example below uses the ToBase64 method to encode an alpha:
import Synergex.SynergyDE
record
inp, a28, '{"alg":"HS246","type":"JWT"}'
b64, a40
.proc
b64 = Synergex.SynergyDE.DataEncoding.ToBase64(inp)
The following example uses the FromBase64 method to decode an alpha:
import Synergex.SynergyDE
record
out, a28
b64, a40, 'eyJhbGciOiJIUzI0NiIsInR5cGUiOiJKV1QifQ=='
.proc
out = Synergex.SynergyDE.DataEncoding.FromBase64(b64)
Base64 encoding/decoding can be useful for parameters to a URL. But problems can arise because base64 encoding includes the forward slash and plus sign, symbols that have special meaning in a URL. Instead, to avoid any conflicts, you can use base64 URL encoding, which replaces the plus sign with a dash and the forward slash with an underscore. Therefore, we’ve also added the following URL-specific methods for base64 encoding and decoding in Synergy/DE 11.1:
- URL_encoded_alpha = ToBase64URL(alpha)
- URL_encoded_alpha = ToBase64URL(alpha, offset, length)
- URL_encoded_string = ToBase64URL(string)
- URL_encoded_string = ToBase64URL(string, offset, length)
- Decoded_alpha = FromBase64URL(URL_encoded_alpha)
- Decoded_string = FromBase64URL(URL_encoded_string)
We hope these new methods will make your coding easier!