A command-line tool to compute hash of file content and filepath, returning the result as JSON.
- SHA256: Recommended to avoid collision. By default.
- MD5: Fast (but unsafe) - suitable for files - provided for compatibility/legacy purposes
- 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)
- The -a|--alg flag is optional. SHA256 by default
jsonhash <filepath>jsonhash -a md5 <filepath>jsonhash --alg md5 <filepath>jsonhash -a sha256 <filepath>jsonhash -alg sha256 <filepath>The tool returns a JSON object with the following fields:
request: the request objectresponse: the response objecterror: the error object
request always is present.
response and error are mutually exclusive.
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
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
jsonhash test.txtReturns:
{
"request": {
"method": "jsonhash",
"params": {
"filepath": "test.txt",
"id": "1",
"alg": "md5"
},
"ts": 1760367864236976611,
"version": "0.2.0"
},
"response": {
"ts": 1760367864237186637,
"absfilepath": "/tmp/jsonhash/test.txt",
"hash": "9458edef495a29ee98b40a63ed80e9b7",
"filename": "test.txt",
"shortpath": "jsonhash/test.txt",
"pathid": "50b53521888c464428cbb6189ef50602",
"shortpathid": "d746f98d3344150895a94a752349ce82"
}
}- 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 |
{
"request": {
"method": "jsonhash",
"params": {
"filepath": "/temp/non-existing-file",
"id": null,
"alg": "sha256"
},
"ts": 1760368083128138305,
"version": "0.2.0"
},
"error": {
"code": 6,
"message": "FILE-DOES-NOT-EXIST"
}
}
- Rust
- sha2 crate for SHA256 hashing
- md5 crate for MD5 hashing
- clap for command-line argument parsing
- serde for JSON serialization
- tempfile for testing
cargo build --releasecargo test- A test.txt sample file is included:
test.txt - A bash test script that uses the sample file is included:
test.sh
- 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).