Trying to export a sha256 function to JavaScript.
I took some code from #401 but am stuck attempting to pass a u8 vector to input().
extern crate neon;
extern crate hex;
extern crate sha2;
use neon::prelude::*;
use sha2::{Sha256, Digest};
fn sha256hex(mut cx: FunctionContext) -> JsResult<JsString> {
let buf: Handle<JsBuffer> = cx.argument(0)?;
let result: str = {
let guard = cx.lock();
let data = buf.borrow(&guard);
let mut hasher = Sha256::new();
hasher.input(data.as_slice::<u32>());
hex::encode(hasher.result().as_string());
};
Ok(cx.string(result))
}
register_module!(mut ctx, {
ctx.export_function("sha256hex", sha256hex)
});
cc @kjvalencik if you have 2 minutes and are feeling generous today :)
The Rust stdlib has helpers for this: https://doc.rust-lang.org/std/primitive.u64.html#method.to_ne_bytes
Close?
Most helpful comment
The Rust stdlib has helpers for this: https://doc.rust-lang.org/std/primitive.u64.html#method.to_ne_bytes