SHA3 Implementation in WebAssembly Text
An algorithmically compliant implementation of SHA3 based on the specification published as NIST FIPS 202. Implemented in WebAssembly Text.
The resulting binary is 5.3 Kb 😎
Table of Contents
Overview
The SHA3 algorithm can be used in two modes:
- As a drop-in replacement for SHA2, or in
- Extendible Output Function (XOF) mode
Drop-In Replacement Mode for SHA2
When used as a drop-in replacement for SHA2, the length of the generated digest d must exacly match the digest lengths generated by SHA2.
Therefore, d may only be one of 224, 256, 384, or 512 bits.
Extendible Output Function (XOF) Mode
When used in XOF mode, the SHA3 function can produce an arbitrary quantity of pseudo-random output data.
§6.2 in the above NIST FIPS 202 document defines two XOF mode functions known as SHAKE128 and SHAKE256.
How Does Drop-In Mode Differ From XOF Mode?
Internally, the same hashing function is used in both modes; but with the following differences:
| Mode | Data In Last BlockTerminated With | Output Length |
|---|---|---|
| Drop-in | 0x06 | One of 224, 256, 384 or 512 bits |
| XOF | 0x1111 | Any length between 1 and 16,777,216 |
Run The Published Version
Assuming you have wasmer installed, you can run the published version of this program as follows:
wasmer run chriswhealy/sha3 --volume=<local_directory>:/. --command-name=<cmd> <output-length> <filename>
| Arg | Possible Values | Description |
|---|---|---|
<local_directory> | A local directory on your machine | The directory to be preopened for WASM |
<cmd> | 224 , 256, 384 or 512 | Run SHA3 in SHA2 drop-in replacement mode and produce a digest this many bits long |
<cmd> | shake128 , shake256 | Run SHA3 in XOF mode and produce <output-length> bytes of pseudo-random data |
<output-length> | An integer between 1 and 16777216 | Only relevant when using the shake128 and shake256 commands |
<filename> | The filename to be hashed | This file must live in or below the <local_directory> |