sha3iuf-d 1.0.1-dev

sha3iuf written in pure D.


To use this package, run the following command in your project's root directory:

Manual usage
Put the following dependency into your project's dependences section:

sha3iuf-d

Cross-Compiling

This library is a port of SHA3IUF, a library for calculating SHA3, to the D language.

Usage

D

The SHA3 hash is calculated as follows.

import sha3iuf_d;
import std.stdio;
import std.digest;

sha3_context c = void;

sha3_Init256(&c);
sha3_SetFlags(&c, SHA3_FLAGS.SHA3_FLAGS_NONE);
sha3_Update(&c, "a".ptr, "a".length);
sha3_Update(&c, "bc".ptr, "ab".length);

//No need to free.
const (ubyte)* hash = cast(const (ubyte)*)(sha3_Finalize(&c));

ubyte[] output = new ubyte[256 / 8];
output[0 .. (256 / 8)] = hash[0 .. (256 / 8)];
std.stdio.writeln(std.digest.toHexString!(std.digest.LetterCase.lower)(output));

You can also write something like the following.

import sha3iuf_d;
import std.stdio;
import std.digest;

enum image_size = 256;
enum input = "abc";
ubyte[image_size / 8] output;

sha3_return_t result = sha3_HashBuffer(image_size, SHA3_FLAGS.SHA3_FLAGS_NONE, input.ptr, input.length, output.ptr, output.length);

if (result == SHA3_RETURN.SHA3_RETURN_OK) {
	std.stdio.writeln(std.digest.toHexString!(std.digest.LetterCase.lower)(output));
} else {
	std.stdio.stderr.writeln("Hash calculation failed.");
}

wasm

The generated WASM is not optimized, so you need to optimize it with a tool such as Binaryen.

wasm-opt -O -o sha3iuf-d.wasm sha3iuf-d.wasm

If you want to run it in Node.js, you can do the following.

const fs = require('fs');
const {TextEncode, TextDecoder} = require('util');
const image_size = 256;

let bytes = new Uint8Array(fs.readFileSync('./sha3iuf-d.wasm'));
let instance = new WebAssembly.Instance(new WebAssembly.Module(bytes), {});

let input = new Uint8Array(instance.exports.memory.buffer, instance.exports.get_input_buffer_address(), instance.exports.input_buf_length());
let output = new Uint8Array(instance.exports.memory.buffer, instance.exports.get_output_buffer_address(), instance.exports.output_buf_length());

//"abc"
input[0] = 0x61;
input[1] = 0x62;
input[2] = 0x63;

let result = instance.exports.sha3_HashBuffer(image_size, 0, instance.exports.get_input_buffer_address(), 'abc'.length, instance.exports.get_output_buffer_address(), instance.exports.output_buf_length());

if (result == 0) {
	let hash_digest = new Uint8Array(instance.exports.memory.buffer, instance.exports.get_output_buffer_address(), image_size / 8);
	console.log(Array.from(hash_digest).map(v => v.toString(16).padStart(2,'0')).join(''));
} else {
	console.log('Hash calculation failed.');
}
Authors:
  • dokutoku
Dependencies:
none
Versions:
1.0.1-dev 2021-Mar-30
1.0.0-dev 2021-Mar-07
~master 2022-Jul-26
Show all 3 versions
Download Stats:
  • 0 downloads today

  • 0 downloads this week

  • 0 downloads this month

  • 0 downloads total

Score:
0.6
Short URL:
sha3iuf-d.dub.pm