WASMhash

As you type in the input below, the text's SHA1 is calculated and displayed. The hashing function was written in Rust, without any thought put toward it eventually running in a browser or compiled to WebAssembly.

Text:

SHA1:

The WebAssembly code is loaded by a 130-line JavaScript file created by stdweb.

The JavaScript code on this page is:

Rust.wasmhash.then( function( hasher ) {
  const output = document.getElementById("output");
  document.getElementById("string").addEventListener("keyup", (e) => {
    const hash = hasher.sha1(e.target.value);
    output.innerText = hash;
  });
});
    

The Rust code is:

#[macro_use]
extern crate stdweb;
extern crate sha1;

use sha1::Sha1;

fn hash( string: String ) -> String {
    let mut hasher = Sha1::new();
    hasher.update( string.as_bytes() );
    hasher.digest().to_string()
}

fn main() {
    stdweb::initialize();

    js! {
        Module.exports.sha1 = @{hash};
    }
}
    

That's it!