Neon: Any helper for u32 to u8?

Created on 6 Apr 2019  路  2Comments  路  Source: neon-bindings/neon

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 :)

Most helpful comment

All 2 comments

Close?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

manuel2258 picture manuel2258  路  5Comments

rm-rf-etc picture rm-rf-etc  路  3Comments

chpio picture chpio  路  6Comments

leshow picture leshow  路  3Comments

hardliner66 picture hardliner66  路  3Comments