0

I am trying to to do a base64 encode of byte array. However, I am limited for choices. btoa() method is not supported by Rhino Javascript engine. Is there a way this can be achieved.

Below snippet error that rhino js does not support.

java.util.concurrent.ExecutionException: javax.script.ScriptException: ReferenceError: "btoa" is not defined.

Below is snippet of code.

var encodedData = btoa(stringToEncode);

I am trying to encode salted sha256 byte array into base64.

var md = java.security.MessageDigest.getInstance('SHA-256');
md.update(salt);
var sha256Bytes = []
sha256Bytes=md.digest(new java.lang.String(givenPassword).getBytes('UTF-8'))
var sha256Hash = btoa(sha256Bytes);
Karan Nayyar
  • 548
  • 1
  • 4
  • 10
  • sorry for any confusion caused by my (retracted) reopen vote. Actually you can just use the Java functions. `encoded = java.lang.String(java.util.Base64.getEncoder().encode(java.lang.String("hello").getBytes('UTF-8'))); decoded = new java.lang.String(java.util.Base64.getDecoder().decode(enccoded));`. But it took me (no experience in Rhino) a while to get the encoding part right. – jps Feb 01 '22 at 20:30

0 Answers0