jsonhash
A command-line tool to compute hash of file content and filepath, returning the result as JSON.
Supported hashing algorithms
- SHA256: Recommended to avoid collision. By default.
- MD5: Fast (but unsafe) - suitable for files - provided for compatibility/legacy purposes
Features
- Supports both SHA256 and MD5 hash algorithms
- Computes hash of file content
- Computes hash of absolute filepath
- Computes hash of short path (relative to current directory). Useful to create bucket/file ids.
- Returns all information in JSON format. Output and Error messages in JSON format.
- Adopted a request/response style output (>= version 0.2.0)
Usage
- The -a|--alg flag is optional. SHA256 by default
Compute SHA256 hash (default)
Compute MD5 hash
Compute SHA256 hash (explicit)
Response
The tool returns a JSON object with the following fields:
request: the request objectresponse: the response objecterror: the error object
Notes
request always is present.
response and error are mutually exclusive.
Request object
method: the name of this tool. 'jsonhash'version: the version of this toolts: timestamp in nanoseconds when the request was receivedparams: contains the parameters as "key":valuefilepath: filepath you provided in your requestalg: Algorithm used (sha256 or md5)id: value provided with --id, else null
Response object
ts: timestamp in nanoseconds when the request was respondedabsfilepath: Absolute path of the filehash: Hash of the file contentfilename: Name of the fileshortpath: Short path relative to current directorypathid: Hash of the absolute filepathshortpathid: Hash of the short path
Example
Returns:
Error messages
- Error messages are provided in json format
error message will contain:
code: code at integer
message: error message as String
| Code | Value | Description |
|---|---|---|
| 3 | HASH-ALGORITHM-INVALID | incorrect / unsupported hash algorithm |
| 5 | NOT-A-FILE | The filepath is not a file (Directories are not supported) |
| 6 | FILE-DOES-NOT-EXIST | The filepath does not exist. No such address: The specified address is unavailable. |
| 66 | FILE-OPENING-ERROR | file cannot be opened |
| 70 | HASH-DIGEST-COMPUTATION-ERROR | An error occured during the computation of the hash digest |
Dependencies
- Rust
- sha2 crate for SHA256 hashing
- md5 crate for MD5 hashing
- clap for command-line argument parsing
- serde for JSON serialization
- tempfile for testing
Building
Testing
- A test.txt sample file is included:
test.txt - A bash test script that uses the sample file is included:
test.sh
License
Author
Changelog
- Version 0.2.0 : Breaking compatibility with v.0.1.x : New JSON request-reponse style with versioning and timestamps (consistent with the jfsm library).