The CreatorCon Call for Content is officially open! Get started here.

Using atob/btoa server side, and GlideRhinoHelper?

xiaix
Tera Guru

So, I'm trying to Base64 decode a Base64 encoded string within a Script Include (server side).  Of course I get "atob is not defined", so I'm wondering how to do this in ServiceNow.

I'm thinking I can use ServiceNow's "GlideRhinoHelper", but there is 0 documentation for it!

Anyone able to assist?

1 ACCEPTED SOLUTION

xiaix
Tera Guru

Ok, finally got it.

JSON.parse(GlideStringUtil.base64Decode(s))

find_real_file.png


So for example this Base64 encoded string:
eyJndWlkIjoiMjhmZDdiYmQtM2U2Ny00MDMwLTlhN2YtN2YzMzk1Zjc3Nzg2IiwiYWN0aW9uIjoidXBkYXRlIiwiZmllbGRzIjoiZW1haWwsbGFzdF9uYW1lIn0=

would process out to:

find_real_file.png

View solution in original post

5 REPLIES 5

Have you tried a vanilla Javascript function to go from base64 to hex?

 

I've tried something like this as an example (of course using only a subset of ASCII chars):

 

var hmacGCE = new GlideCertificateEncryption;
var key = GlideStringUtil.base64Encode('secret');
var hmacString = hmacGCE.generateMac(key, "HmacSHA1", JSON.stringify('This is a test'));

gs.info(hmacString);

function base64ToHex(base64) {
var keyStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
var output = "";
var chr1, chr2, chr3, enc1, enc2, enc3, enc4;
var i = 0;

// Remove all characters that are not A-Z, a-z, 0-9, +, /, or =
base64 = base64.replace(/[^A-Za-z0-9+/=]/g, "");

while (i < base64.length) {
enc1 = keyStr.indexOf(base64.charAt(i++));
enc2 = keyStr.indexOf(base64.charAt(i++));
enc3 = keyStr.indexOf(base64.charAt(i++));
enc4 = keyStr.indexOf(base64.charAt(i++));

chr1 = (enc1 << 2) | (enc2 >> 4);
chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
chr3 = ((enc3 & 3) << 6) | enc4;

output = output + String.fromCharCode(chr1);

if (enc3 !== 64) {
output = output + String.fromCharCode(chr2);
}
if (enc4 !== 64) {
output = output + String.fromCharCode(chr3);
}
}

// Convert binary string to hexadecimal string
var hex = '';
for (var j = 0; j < output.length; j++) {
var _byte = output.charCodeAt(j);
hex += ('0' + (_byte & 0xFF).toString(16)).slice(-2);
}

return hex;
}

gs.info(base64ToHex(hmacString));