keccak.js (1071B)
1 const fs = require('fs'); 2 3 const memory = new WebAssembly.Memory({initial: 2}); 4 const table = new WebAssembly.Table({initial: 3, element: 'anyfunc'}); 5 const importsObj = { 6 env: { 7 memory: memory, 8 __linear_memory: memory, 9 __indirect_function_table: table, 10 }, 11 } 12 13 async function init() { 14 const code_keccak = fs.readFileSync('./lib/keccak.wasm'); 15 const m_keccak = new WebAssembly.Module(code_keccak); 16 const i_keccak = new WebAssembly.Instance(m_keccak, importsObj); 17 18 let r; 19 const p = i_keccak.exports.keccak_hash_heap_init(32, 3); 20 console.debug('r ' + p); 21 22 let outBuf = new Uint8Array(memory.buffer, p, 256); 23 let inBuf = new Uint8Array(memory.buffer, p + 256, 3); 24 inBuf.set([0x66, 0x6f, 0x6f], 0); 25 r = i_keccak.exports.keccak_hash(p, 32, p+256, 3, 200-64, 1); 26 if (r < 0) { 27 console.error('error ' + r); 28 } elseĀ { 29 console.log('in ' + Buffer.from(new Uint8Array(memory.buffer, p+256, 3)).toString('hex')); 30 console.log('out ' + Buffer.from(new Uint8Array(memory.buffer, p, 32)).toString('hex')); 31 } 32 i_keccak.exports.keccak_hash_heap_free(p); 33 } 34 35 init();