ALERT! The community will be read-only starting on April 19, 8am Pacific as the migration begins. Read more for important details.
ALERT! The community will be read-only starting on April 19, 8am Pacific as the migration begins.Read more for important details.

SBM ModScript, Part 13 - Base64Encode

SBM ModScript, Part 13 - Base64Encode

Someone recently asked me if it is possible to base64 encode a string in ModScript... so I did.

def Base64Encode( string input ) {
  add_global_const("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/", "CONST_BASE64TABLE");
  
  var sOut = "";
  
  var buf = [uint8_t(),uint8_t(), uint8_t()];
  var idx = -1;

  for ( c : input ) {
    buf[++idx] = c;
    if (idx == 2) {
      // Output the 4 encoded bytes
      sOut += CONST_BASE64TABLE[buf[0] >> 2];                              // first 6-bits of byte 0
      sOut += CONST_BASE64TABLE[(((buf[0] & 0x03) << 4)) | (buf[1] >> 4)]; // last 2-bits of byte 0 + first 4-bits of byte 1
      sOut += CONST_BASE64TABLE[(((buf[1] & 0x0f) << 2)) | (buf[2] >> 6)]; // last 4-bits of byte 1 + first 2-bits of byte 2
      sOut += CONST_BASE64TABLE[buf[2] & 0x3f];                            // last 6-bits of byte 2

      idx = -1;
    }
  }

  // Special case handling of last 1 or 2 characters
  if ( idx != -1 ) {
    sOut += CONST_BASE64TABLE[buf[0] >> 2];             // first 6-bits of byte 0
    
    if (idx == 0) {
      sOut += CONST_BASE64TABLE[((buf[0] & 0x03) << 4)];  // last 2-bits of byte 0
      sOut += '=';                                  // pad
    }
    else if (idx == 1) {
      sOut += CONST_BASE64TABLE[(((buf[0] & 0x03) << 4)) | (buf[1] >> 4)]; // last 2-bits of byte 0 + first 4-bits of byte 1
      sOut += CONST_BASE64TABLE[((buf[1] & 0x0f) << 2)];                   // last 4-bits of byte 1
    }
    
    sOut += '=';                                  // pad
  }
  
  sOut; // return
}

 

The function above iterates the string contents and uses base64 to create an encoded output string. Notice that the "buf" variable is a Vector of 3 unsigned, 8 bit integers. As we are going to use bit shifting in order to encode the data, it is important to use unsigned byte data to ensure the expected bit-shift result. We then use the bit-shifted value to index into CONST_BASE64TABLE to find the data-representation we are looking for. The result is a base64 encoding of the string data. 

A likely use-case for this would be when adding custom headers to a RESTDataSource call-out.

 

SBM ModScript - Table of Contents

Labels (1)

DISCLAIMER:

Some content on Community Tips & Information pages is not officially supported by Micro Focus. Please refer to our Terms of Use for more detail.
Top Contributors
Version history
Revision #:
3 of 3
Last update:
‎2020-07-29 16:07
Updated by:
 
The opinions expressed above are the personal opinions of the authors, not of Micro Focus. By using this site, you accept the Terms of Use and Rules of Participation. Certain versions of content ("Material") accessible here may contain branding from Hewlett-Packard Company (now HP Inc.) and Hewlett Packard Enterprise Company. As of September 1, 2017, the Material is now offered by Micro Focus, a separately owned and operated company. Any reference to the HP and Hewlett Packard Enterprise/HPE marks is historical in nature, and the HP and Hewlett Packard Enterprise/HPE marks are the property of their respective owners.