TooTallNate / ref

Turn Buffer instances into "pointers"
http://tootallnate.github.com/ref
453 stars 141 forks source link

how can i call this function ? #43

Closed frndxyz closed 8 years ago

frndxyz commented 8 years ago

.C function

void __stdcall lyra2re_hash(const char* input, char* output)
{
    sph_blake256_context     ctx_blake;
    sph_groestl256_context   ctx_groestl;
    sph_keccak256_context    ctx_keccak;
    sph_skein256_context     ctx_skein;

    uint32_t hashA[8], hashB[8];

    sph_blake256_init(&ctx_blake);
    sph_blake256 (&ctx_blake, input, 80);
    sph_blake256_close (&ctx_blake, hashA);

    sph_keccak256_init(&ctx_keccak);
    sph_keccak256 (&ctx_keccak,hashA, 32);
    sph_keccak256_close(&ctx_keccak, hashB);

    LYRA2((void*)hashA, 32, (const void*)hashB, 32, (const void*)hashB, 32, 1, 8, 8);

    sph_skein256_init(&ctx_skein);
    sph_skein256 (&ctx_skein, hashA, 32);
    sph_skein256_close(&ctx_skein, hashB);

    sph_groestl256_init(&ctx_groestl);
    sph_groestl256 (&ctx_groestl, hashB, 32);
    sph_groestl256_close(&ctx_groestl, hashA);

    memcpy(output, hashA, 32);
}

created win64 DLL with name lyra2re.dll

my node source

var ref = require("ref");
var FFI = require('ffi');

//var hashPtr = ref.refType('pointer');

var lyra2re = new FFI.Library('lyra2re', {
    'lyra2re_hash': [
        'void', ['string', "string"]
    ]
});

var returnedHash = ref.alloc("pointer");

lyra2re.lyra2re_hash("test", returnedHash);

var buf2 = ref.readPointer(returnedHash, 0, 32);
console.log("test");
TooTallNate commented 8 years ago

You're close. Your definition looks good, you just need to do the "output buffer" a bit differently. Do something like this:

var output = new Buffer(32);
lyra2re.lyra2re_hash("test", output);
console.log(output.toString());
frndxyz commented 8 years ago

hey it works , thanks @TooTallNate