Big news! The community will be moving to a new platform April 21. Read more.
Big news! The community will be moving to a new platform April 21. Read more.

JavaScript Base64-Encoding Binary Data in IDM (a.k.a using Java and JavaScript within the IDM engine)

JavaScript Base64-Encoding Binary Data in IDM (a.k.a using Java and JavaScript within the IDM engine)

Base64-encoding data is found in places all over the IT world and in home users' systems as well. Being able to encode data in Java or ECMAScript/JavaScript can be valuable when those are the languages available to you. This sample shows how to convert from one format to another and eventually encode data for use within a directory like eDirectory.



In a recent article I mentioned the possibilities of ECMAScript/Javascript within Java for scripting within Java applications or just for your own system. While working on an issue recently the need to convert a string of characters representing binary to a Base64-encoded representation of that binary value was manifest. As the issue was for use within Novell Identity Manager the following was able to be used to accept the string of binary and then convert it to a valid Base64-encoded value. The same could be done within any application with access to the com.novell.xml.util package (or a suitable replacement to do the Base64-encoding) and a Rhino implementation to run the rest of the conversion code from string to binary. ECMAScript/JavaScript Development Without a Web Browser



<code interpreter="rhino">
function b64encbinstring(binstring) {
importPackage(java.io);
importPackage(java.lang);
importPackage(Packages.com.novell.xml.util);
var mylong0 = java.lang.Long.parseLong(binstring, 2);
var bos = new java.io.ByteArrayOutputStream();
var dos = new java.io.DataOutputStream(bos);
dos.writeLong(mylong0);
dos.flush();
var bytedata = bos.toByteArray();
var base64c = new Packages.com.novell.xml.util.Base64Codec();
var base64string=new Packages.java.lang.String(base64c.encode(bytedata, (8-Math.ceil(binstring.length/8)), Math.ceil(binstring.length/8), false));
return base64string;
}

b64encbinstring('00101010110111100001010101000101101'); //Line to Base64-encode a string of zeros and ones and return the appropriate string.
</quote>



This may not be the most-elegant method of doing what is needed but it was something that otherwise would have required custom Java code to do the same compiled into the application in one way or another. If somebody can condense the code above to make is simpler or avoid unnecessary steps please feel free to do so in the comments section.


Labels (2)

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 #:
6 of 6
Last update:
‎2020-03-10 19:24
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.