How to do Base 64 encoding of byte array #1172
Unanswered
Karan-nayyar
asked this question in
Q&A
Replies: 1 comment 2 replies
-
This worked in my environment. $ java -version
openjdk version "1.8.0_312"
OpenJDK Runtime Environment (build 1.8.0_312-8u312-b07-0ubuntu1~20.04-b07)
OpenJDK 64-Bit Server VM (build 25.312-b07, mixed mode)
$ java -jar ./rhino-1.7.13.jar var md = java.security.MessageDigest.getInstance('SHA-256');
md.update(new java.lang.String('salt').getBytes('UTF-8'));
var sha256Bytes = md.digest(new java.lang.String('givenPassword').getBytes('UTF-8'));
var sha256Hash = java.lang.String(java.util.Base64.getEncoder().encode(sha256Bytes));
sha256Hash; // QbczksI4v06hS3FACAYCUZgg8WSkBoAi1zjhVrU2JTQ= |
Beta Was this translation helpful? Give feedback.
2 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
I am using JS Script Engine: Rhino 1.7.13 version.
I am trying to encode byte array using java package Base64 in a javascript.
Below is the error in logs.
java.util.concurrent.ExecutionException: javax.script.ScriptException: TypeError: Cannot call property getEncoder in object [JavaPackage java.util.Base64]. It is not a function, it is "object".
Below is the snippet of code.
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 = java.lang.String(java.util.Base64.getEncoder().encode(sha256Bytes));
Also,package java.util.Base64.Encoder is imported.Tried with btoa() method also. Js engine does not support it. How can I achieve this base64 encoding of byte array. I have tried many alternatives but none are working for me.
Beta Was this translation helpful? Give feedback.
All reactions