# 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
## Usage
### Compute SHA256 hash (default)
```bash
jsonhash <filepath>
```
### Compute MD5 hash
```bash
jsonhash -a md5 <filepath>
```
```bash
jsonhash --alg md5 <filepath>
```
### Compute SHA256 hash (explicit)
```bash
jsonhash -a sha256 <filepath>
```
```bash
jsonhash -alg sha256 <filepath>
```
## Output Format
The tool returns a JSON object with the following fields:
- `absfilepath`: Absolute path of the file
- `hash`: Hash of the file content
- `filename`: Name of the file
- `shortpath`: Short path relative to current directory
- `alg`: Algorithm used (sha256 or md5)
- `pathid`: Hash of the absolute filepath
- `shortpathid`: Hash of the short path
- `status`: Status of the operation (OK or ERROR)
- `message`: Error message if status is ERROR. Empty if successful.
## Example
```bash
jsonhash test.txt
```
Returns:
```json
{
"absfilepath": "/home/user/test.txt",
"hash": "8a8ee87a279ef688b4e1200ee8730d7edaf8231b04a2c2570f802190507cda56",
"filename": "test.txt",
"shortpath": "user/test.txt",
"alg": "sha256",
"pathid": "a1b2c3d4e5f6...",
"shortpathid": "f6e5d4c3b2a1...",
"status": "OK",
"message": ""
}
```
## Error messages
- Error messages are provided in json format
- Error messages:
- HASH-DIGEST-COMPUTATION-ERROR: An error occured during the computation of the hash digest
- HASH-ALGORITHM-INVALID: You are specifying an incorrect hash algorithm
- NOT-A-FILE: The filepath is not a file (is a directory)
- FILE-DOES-NOT-EXIST : The file does not exist
- FILE-OPENING-ERROR : The file cannot be opened
```json
{
"filepath": "{filepath}",
"status": "ERROR",
"message": "{error-message}"
}
```
## 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
```bash
cargo build --release
```
## Testing
```bash
cargo test
- A test.txt sample file is included: `test.txt`
- A bash test script that uses the sample file is included: `test_hash.sh`
## License
[MIT](mit.txt) or [Apache2](license.txt)
## Author
[David HEURTEVENT - frua.fr](https://github.com/fruafr)