We have a Java UUID being written to a ByteArrayOutputStream. Then bytearray of that ByteArrayOutputStream is base64 encoded and sent in a JSON. The encoding process is as follows.
public String uuidToString(UUID uuidObj) {
final ByteArrayOutputStream baos = new ByteArrayOutputStream();
final DataOutputStream dos = new DataOutputStream(baos);
dos.writeLong(uuidObj.getMostSignificantBits());
dos.writeLong(uuidObj.getLeastSignificantBits());
dos.flush();
return Base64.encodeBase64URLSafeString(baos.toByteArray());
}
I am trying to figure out how to decode this String in the javascript side. Ofcourse the base64 part is easy. Using atob I get a String and then using Crypto.js I can get a byte array out of that base64 decoded String. Then I tried to use the conversion techniques mentioned in the following link to convert but its always returning garbage.
Converting byte array to string in javascript
Using the String as is directly in JSON is not an option for a couple of reasons. Firstly we want to keep this String opaque, but we also want to keep the size of the final encoded string that is sent over the wire to be small.
I know another option is protocol buffers, but the lack of a supported js implementation and the extra build time leg work seem to be barriers on that front.
So do I have any other options ? And any suggestions on why my javascript decoding isn't working.