Scoped app: Is there an API for base64 encoding / decoding raw byte arrays (not the Base64 encode/decode of string via GlideSystem)
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-14-2021 10:04 AM
Wanted to know if we have any such APIs available for scoped apps? A sample use case is decoding encoded keys from a messaging provider- in this case, gs.base64Decode() is generating a different value compared to a server side JS invoker compared to how it would have been if we just fetched the raw bytes.
- Labels:
-
Platform and Cloud Security

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-06-2021 01:14 PM
Hoping this helps as I had this problem and overcame it using an implementation of the fix script below: It has a sample which was encoded externally and then decoded within ServiceNow using both gs.base64Encode and decoding it as Hex - The Hex decode may be why you may be getting a different result.
var data = 'This is a test of payload data';
var secret = 'secret';
var EXTGeneratedSignature = '6d77f08aac96b28aa442960af0a0d5e0b9a80791aecb7b47829320bc724a408d';
// signature was generated by https://www.freeformatter.com/hmac-generator.html
var key = gs.base64Encode(secret);
var mac = new CertificateEncryption();
var SNgeneratedSignature = mac.generateMac(key, 'HmacSHA256', data);
var EXTdecode = hexToBase64(EXTGeneratedSignature);
if (SNgeneratedSignature == EXTdecode) {
gs.info('The signature is valid');
} else {
gs.info('the signature is not valid');
}
function hexToBase64(str) {
function btoa(bin) {
var tableStr = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';
var table = tableStr.split('');
for (var i = 0, j = 0, len = bin.length / 3, base64 = []; i < len; ++i) {
var a = bin.charCodeAt(j++),
b = bin.charCodeAt(j++),
c = bin.charCodeAt(j++);
if ((a | b | c) > 255) throw new Error('String contains an invalid character');
base64[base64.length] =
table[a >> 2] +
table[((a << 4) & 63) | (b >> 4)] +
(isNaN(b) ? '=' : table[((b << 2) & 63) | (c >> 6)]) +
(isNaN(b + c) ? '=' : table[c & 63]);
}
return base64.join('');
}
return btoa(
String.fromCharCode.apply(
null,
str
.replace(/\r|\n/g, '')
.replace(/([\da-fA-F]{2}) ?/g, '0x$1 ')
.replace(/ +$/, '')
.split(' ')
)
);
}