How Can We Generate Random Salt Of 32 Bytes In Rhino JS
I am trying to generate a random salt of 32 bytes size. But my JS engine Rhino 1.7.13 doesn't support SecureRandom class.
Below is the code snippet of the same.
function getSalt() {
var random = new SecureRandom();
var salt1 = new Array(32);
random.nextBytes(salt1);
return salt1;
}
Error logged as below.
java.util.concurrent.ExecutionException: javax.script.ScriptException: ReferenceError: "SecureRandom" is not defined.
Also, rhino js engine does not allow any import or load of external library. Is there a way we can generate secure random salts in Rhino?
Answer
For SecureRandom
use the Java namespace.
And the byte array has to be a Java byte array, otherwise you'll get an error:
Cannot convert [email protected] to byte[]
I found this answer from Tomasz Gawel, which shows how to declare a java byte array in Rhino.
With the above-mentioned modifications, the complete script is:
function getSalt() {
var random = new java.security.SecureRandom();
var salt1 = java.lang.reflect.Array.newInstance(java.lang.Byte.TYPE, 32)
random.nextBytes(salt1);
return salt1;
}
saltB64 = java.lang.String(java.util.Base64.getEncoder().encode(getSalt()))
print(saltB64)
Related Questions
- → How to update data attribute on Ajax complete
- → October CMS - Radio Button Ajax Click Twice in a Row Causes Content to disappear
- → Octobercms Component Unique id (Twig & Javascript)
- → Passing a JS var from AJAX response to Twig
- → Laravel {!! Form::open() !!} doesn't work within AngularJS
- → DropzoneJS & Laravel - Output form validation errors
- → Import statement and Babel
- → Uncaught TypeError: Cannot read property '__SECRET_DOM_DO_NOT_USE_OR_YOU_WILL_BE_FIRED' of undefined
- → React-router: Passing props to children
- → ListView.DataSource looping data for React Native
- → Can't test submit handler in React component
- → React + Flux - How to avoid global variable
- → Webpack, React & Babel, not rendering DOM