Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 74 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ Why use `hashit`?
- You can get multiple hashes "for free" on any CPU with multiple cores
- Works very well across multiple platforms without slowdown (Windows, Linux, macOS)
- Builds on illumos and Solaris, without the `sqlite` format, auditing SQLite databases or `--progress`, as the libraries they use do not support those platforms
- Supports many hashes `hashit --hashes` CRC32, xxHash64, MD4, MD5, SHA1, SHA224, SHA256, SHA384, SHA512, Blake2b-256, Blake2b-512, Blake3, SHA3-224, SHA3-256, SHA3-384, SHA3-512, ed2k. Select them with `--hash` using the lowercase names shown in brackets by `hashit --hashes`, such as `sha3256` or `blake2b256`, or `--hash all`. The `ed2k` hash of a file which is an exact multiple of 9,728,000 bytes uses the variant without an extra empty chunk, so differs from tools such as `rhash` for those sizes
- Supports many hashes `hashit --hashes` CRC32, xxHash64, MD4, MD5, SHA1, SHA224, SHA256, SHA384, SHA512, Blake2b-256, Blake2b-512, Blake3, SHA3-224, SHA3-256, SHA3-384, SHA3-512, ed2k, Tiger, Whirlpool. Select them with `--hash` using the lowercase names shown in brackets by `hashit --hashes`, such as `sha3256` or `blake2b256`, or `--hash all`. Whirlpool is about ten times slower than SHA512, so `--hash all` takes much longer on large files than it did without it. The `ed2k` hash of a file which is an exact multiple of 9,728,000 bytes uses the variant without an extra empty chunk, so differs from tools such as `rhash` for those sizes
- Output is compatible with `hashdeep`, and audits can be verified by either tool (see [Absolute Paths](#absolute-paths))

### Usage
Expand Down Expand Up @@ -162,6 +162,8 @@ Blake2b-512 ad692390b5e680f24490d5f4676f279f84e802c79b98ef36369404b88d8ced237dbd
SHA3-384 8301eb66f20215a14a77aa6e977a9d08524caf31fa09a9fd97e7b3c4c3e9266159d9c35c661960024654ff36f5131302
SHA3-512 1024928532bccfe9feb0b3f9fe54eda6fb05685fb3236eb09cb3765f0d1face3ea5f5dc681aaeec44134cd89691348a03cc6a2abfe1eae55d8eb274a51f97e3d
ed2k a4961e3d704e28a40debbf1f1e4a62cd
Tiger 8461dc5bd991619db31c3fb2b6a1dd451cf64086ad12ce68
Whirlpool 3166f9a5ecf7ff1992e792c39cd7ad52a1b5079447a96e96d506a964bb4be956ee78d98d85948296967de031913399ad2d7e821f3a9fd478bf7ee659918949f2

$ mkdir -p example/sub && echo hello > example/a.txt && echo world > example/sub/b.txt
$ hashit --hash md5 example
Expand Down Expand Up @@ -189,7 +191,7 @@ Files are processed in parallel, so the order of the output may vary between run
`--format` (`-f`) selects the output format. Results are written to standard output, or to a file with `--output` (`-o`). The output file is written to a temporary file next to it first, so the directory must be writable, being the directory of the file a symbolic link points to when `--output` is one, and an existing file is only replaced with `--overwrite`, is never replaced when it is a directory or other special file, and is kept rather than replaced when any error occurred. A new output file is still written when errors occurred, so it may not contain every file. A replaced file keeps its permission bits, but not its owner, special bits such as setuid, or extended attributes. When `--output` is a symbolic link the file it points to is replaced, and a symbolic link which cannot be followed, such as a loop or a link to a file which does not exist, is refused. The replacement is a new file, so other hard links to the old file keep the old content. If `hashit` is interrupted by `SIGINT`, `SIGTERM`, `SIGHUP`, `SIGQUIT` or `SIGPIPE` the temporary file is removed and it exits with 128 plus the signal number, such as `130` for `SIGINT`. A signal which was ignored when `hashit` started, such as `SIGHUP` under `nohup`, stays ignored. Other signals such as `SIGUSR1` are ignored, and `SIGKILL` cannot be caught, so leaves the temporary file behind. The message `results written to` is printed to standard output. The output file, the audit file, and paths reaching them through a symbolic link, either a link to the file or a symbolically linked directory, are never included in the results. Another hard link to the output file is ordinary data and is included. Supplying one of them explicitly, such as with a wildcard like `hashit -a sums.md5 *`, skips it with a `WARN`. When the only files supplied were the output or audit file an existing output file is kept rather than replaced with empty results, so `hashit -o doc.txt --overwrite doc.txt` is an error. Without `--overwrite` a file created at the output path while `hashit` was running, such as by another run writing the same output, is not replaced, and the new results are discarded with an error.

- `text`, the default, shown above. `--mtime` adds an `MTime` line in local time.
- `json`, an array of objects such as `[{"File":"example/a.txt","MD5":"b1946ac92492d2347c6235b4d2611184","Bytes":6}]`. The keys are `File`, `CRC32`, `XxHash64`, `MD4`, `MD5`, `SHA1`, `SHA224`, `SHA256`, `SHA384`, `SHA512`, `Blake2b256`, `Blake2b512`, `Blake3`, `Sha3224`, `Sha3256`, `Sha3384`, `Sha3512`, `ed2k` and `Bytes`. Hashes which were not calculated are omitted. `--mtime` adds `MTime` in RFC 3339 format with nanoseconds and the local time zone.
- `json`, an array of objects such as `[{"File":"example/a.txt","MD5":"b1946ac92492d2347c6235b4d2611184","Bytes":6}]`. The keys are `File`, `CRC32`, `XxHash64`, `MD4`, `MD5`, `SHA1`, `SHA224`, `SHA256`, `SHA384`, `SHA512`, `Blake2b256`, `Blake2b512`, `Blake3`, `Sha3224`, `Sha3256`, `Sha3384`, `Sha3512`, `ed2k`, `Tiger`, `Whirlpool` and `Bytes`. Hashes which were not calculated are omitted. `--mtime` adds `MTime` in RFC 3339 format with nanoseconds and the local time zone.
- `sum`, the same as `md5sum` and similar tools, with one line per hash per file when several hashes are selected. `--mtime` adds a `# mtime` comment line, see [Auditing with sum Files](#auditing-with-sum-files).
- `hashdeep`, see [Auditing](#auditing). `--mtime` adds a `## mtime` comment line, see [Modification Times](#modification-times).
- `hashonly`, one hash per line. With several hashes or files there is nothing to say which hash belongs to which file, so it is best used with a single hash and file. `--mtime` adds the time on its own line.
Expand Down Expand Up @@ -253,7 +255,7 @@ In this case, `hashit` would process `main.go` (and the `.hashignore` file itsel

`hashit` provides a powerful auditing feature that is compatible with `hashdeep`. This allows you to verify the integrity of a set of files by comparing their current state against a previously generated list of known hashes.

You can use `hashit` to generate an audit file and have `hashdeep` verify it, or use `hashdeep` to generate the audit file and have `hashit` verify it.
You can use `hashit` to generate an audit file and have `hashdeep` verify it, or use `hashdeep` to generate the audit file and have `hashit` verify it. `hashit` can audit a `hashdeep` file made with any of the hashes `hashdeep` writes: MD5, SHA1, SHA256, Tiger and Whirlpool. `hashdeep` before 4.2 (2012) calculated Tiger wrongly for large files, so a file it wrote with a Tiger column may report those files as modified, by `hashit` and by current versions of `hashdeep` alike, and needs writing again.

#### Generating an Audit File

Expand All @@ -265,6 +267,8 @@ $ hashit --format hashdeep processor > audit.txt

This will create a file named `audit.txt` containing the hashes of the files in the `processor` directory.

Like `hashdeep`, it holds MD5 and SHA256 unless `-c` chooses others. It can hold any of the hashes `hashdeep` writes, MD5, SHA1, SHA256, Tiger and Whirlpool, in that order, so `hashit --format hashdeep -c sha256,tiger processor` writes SHA256 and Tiger. Other hashes are left out with a warning, and if none of the chosen hashes can be held, MD5 and SHA256 are written.

#### Verifying with `hashdeep`

You can then use `hashdeep` to audit a directory against the file generated by `hashit`.
Expand Down Expand Up @@ -305,6 +309,69 @@ Known files expecting: 14
Files missing: 0
```

#### Tiger and Whirlpool

`hashdeep` calculates five hashes: MD5, SHA1, SHA256, Tiger and Whirlpool. It also ships as `tigerdeep` and `whirlpooldeep`, which write only one of them. Collections catalogued with those tools, such as forensic case files and archives, may hold only Tiger or Whirlpool, chosen as both come from outside the MD5 and SHA family. Tiger is a fast 192 bit hash, and Whirlpool is a 512 bit hash standardised in ISO/IEC 10118-3. `hashit` calculates both, so it can audit any file those tools write, and write files they can audit, without converting them or calculating the hashes again.

Whirlpool is about ten times slower than SHA512, so only choose it when another tool or an existing catalogue needs it.

To audit a `hashdeep` file holding Tiger and Whirlpool with `hashit`:

```shell
$ hashdeep -c tiger,whirlpool -r -l photos > photos.hd
$ head -2 photos.hd
%%%% HASHDEEP-1.0
%%%% size,tiger,whirlpool,filename

$ hashit -a photos.hd photos
hashit: Audit passed
Files examined: 2
Known files expecting: 2
Files matched: 2
Files modified: 0
Files moved: 0
New files found: 0
Files missing: 0
```

To write a `hashdeep` file with Tiger for `hashdeep` to audit, or every `hashdeep` hash with `-c all`:

```shell
$ hashit -f hashdeep -c sha256,tiger photos > photos.hd
$ head -2 photos.hd
%%%% HASHDEEP-1.0
%%%% size,sha256,tiger,filename

$ hashdeep -r -l -a -k photos.hd photos
hashdeep: Audit passed

$ hashit -f hashdeep -c all photos | head -2 | tail -1
%%%% size,md5,sha1,sha256,tiger,whirlpool,filename
```

`tigerdeep` output can be audited as it is, as a hash of 48 characters can only be Tiger. A Whirlpool hash is 128 characters, as long as a SHA512 hash, so give `whirlpooldeep` output a `.whirlpool` extension:

```shell
$ tigerdeep -r -l photos > photos.tiger
$ hashit -a photos.tiger photos
hashit: Audit passed

$ whirlpooldeep -r -l photos > photos.whirlpool
$ hashit -a photos.whirlpool photos
hashit: Audit passed
```

Both match the hashes `hashdeep` and `rhash` calculate:

```shell
$ hashit -c tiger,whirlpool photos/a.txt
photos/a.txt (6 bytes)
Tiger b529747e5337b97818c401dd266799a693165013ce146cac
Whirlpool 63f8341c1720d76087bb66a8f3fc9ea21a279edeae866a3611ba425cbd9ac9a4e39b97066b492fed6037988a72a045344c8675107e6bb569276c2f597ea9cbb7
```

Here `photos/a.txt` holds `hello` and a newline.

#### Understanding Audit Results

`hashit`'s audit output is designed to be similar to `hashdeep`'s verbose output, providing a clear summary of what has changed.
Expand Down Expand Up @@ -409,7 +476,7 @@ $ hashit -a checksums.md5 .

Note that `md5sum *` does not include files in subdirectories, while `hashit` audits the whole directory, so for a directory with subdirectories generate the file with something like `find -L . -type f -exec md5sum {} +`, where `-L` follows symbolic links as `hashit` does.

The hash is determined by the file extension where it is one of `.md4`, `.md5`, `.sha1`, `.sha224`, `.sha256`, `.sha384`, `.sha512`, `.sha3-224`, `.sha3-256`, `.sha3-384`, `.sha3-512`, `.b2` or `.blake2b` (Blake2b-512 or Blake2b-256), `.b3` or `.blake3`, `.ed2k`, or `.crc32`, and otherwise by the length of the hash. A hash of 8 characters is assumed to be `crc32` and one of 16 `xxhash64`. Where lengths are shared the more common hash is assumed, being `md5` over `md4` and `ed2k`, `sha224` over `sha3-224`, `sha256` over `sha3-256`, `blake3` and `b2sum -l 256`, `sha384` over `sha3-384`, and `sha512` over `sha3-512` and `b2sum`. A wrong guess reports every file as modified, so name `b2sum` files `.b2` or use `--tag` output for the others. Relative paths are resolved against both the current directory and the directory containing the sum file, so run the audit from the directory the sum file was created in, or keep the sum file at the root of the files it lists.
The hash is determined by the file extension where it is one of `.md4`, `.md5`, `.sha1`, `.sha224`, `.sha256`, `.sha384`, `.sha512`, `.sha3-224`, `.sha3-256`, `.sha3-384`, `.sha3-512`, `.b2` or `.blake2b` (Blake2b-512 or Blake2b-256), `.b3` or `.blake3`, `.ed2k`, `.tiger`, `.whirlpool`, or `.crc32`, and otherwise by the length of the hash. A hash of 8 characters is assumed to be `crc32`, one of 16 `xxhash64` and one of 48 `tiger`. Where lengths are shared the more common hash is assumed, being `md5` over `md4` and `ed2k`, `sha224` over `sha3-224`, `sha256` over `sha3-256`, `blake3` and `b2sum -l 256`, `sha384` over `sha3-384`, and `sha512` over `sha3-512`, `whirlpool` and `b2sum`. A wrong guess reports every file as modified, with a warning naming the other hashes of that length and their extensions, so name `b2sum` files `.b2` and Whirlpool files `.whirlpool`, or use `--tag` output for the others. A line whose hash is not the length of any hash its extension names, such as `b2sum -l 192` output in a `.b2` file, is not recognised rather than guessed. Relative paths are resolved against both the current directory and the directory containing the sum file, so run the audit from the directory the sum file was created in, or keep the sum file at the root of the files it lists.

Likewise the output of `hashit --format sum` can be verified by `md5sum -c` and friends, so long as only a single hash is selected:

Expand Down Expand Up @@ -437,7 +504,9 @@ This method is recommended for any large-scale or repeated auditing tasks.

#### Generating a SQLite Audit Database

To generate a SQLite database of hashes, use the `--format sqlite` flag combined with `--output`. Without `--output` the database is written to `hashit.db`. It is recommended to use `--hash all` to store all possible hashes, which enables the most robust "paranoid" audit checks when the audit is also run with `--hash all`.
To generate a SQLite database of hashes, use the `--format sqlite` flag combined with `--output`. Without `--output` the database is written to `hashit.db`. It is recommended to use `--hash all` to store all possible hashes, which enables the most robust "paranoid" audit checks when the audit is also run with `--hash all`. That includes Whirlpool, which is much slower than the others, so for large collections `--hash sha512,blake3` or similar may be a better trade off.

Databases written by `hashit` 1.5.0, before Tiger and Whirlpool were added, cannot be audited by later versions, which say so, and need to be created again.

```shell
# Generate a database with all hashes for the current directory
Expand Down
2 changes: 2 additions & 0 deletions assets/db/migrations.sql
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ create table if not exists file_hashes (
sha3_384 text,
sha3_512 text,
ed2k text,
tiger text,
whirlpool text,
size integer not null,
mtime text null
);
Expand Down
4 changes: 2 additions & 2 deletions assets/db/queries.sql
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
-- name: FileHashInsertReplace :one
insert or replace into file_hashes (
filepath, crc32, xxhash64, md4, md5, sha1, sha224, sha256, sha384, sha512,
blake2b_256, blake2b_512, blake3, sha3_224, sha3_256, sha3_384, sha3_512, ed2k,
blake2b_256, blake2b_512, blake3, sha3_224, sha3_256, sha3_384, sha3_512, ed2k, tiger, whirlpool,
size, mtime
) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
returning *;

-- name: FileHashByFilePath :one
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ require (
github.com/cespare/xxhash/v2 v2.3.0
github.com/djherbis/times v1.6.0
github.com/gosuri/uiprogress v0.0.1
github.com/jzelinskie/whirlpool v0.0.0-20201016144138-0675e54bb004
github.com/minio/blake2b-simd v0.0.0-20160723061019-3f5f724cb5b1
github.com/spf13/cobra v1.10.2
github.com/zeebo/blake3 v0.2.4
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ github.com/hashicorp/golang-lru/v2 v2.0.7 h1:a+bsQ5rvGLjzHuww6tVxozPZFVghXaHOwFs
github.com/hashicorp/golang-lru/v2 v2.0.7/go.mod h1:QeFd9opnmA6QUJc5vARoKUSoFhyfM2/ZepoAG6RGpeM=
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
github.com/jzelinskie/whirlpool v0.0.0-20201016144138-0675e54bb004 h1:G+9t9cEtnC9jFiTxyptEKuNIAbiN5ZCQzX2a74lj3xg=
github.com/jzelinskie/whirlpool v0.0.0-20201016144138-0675e54bb004/go.mod h1:KmHnJWQrgEvbuy0vcvj00gtMqbvNn1L+3YUZLK/B92c=
github.com/klauspost/cpuid/v2 v2.4.1 h1:I9KxadAkvo+QjtcEE2f2tyfEvmIla/T0m+14PrZNl0U=
github.com/klauspost/cpuid/v2 v2.4.1/go.mod h1:19jmZ9mjzoF//ddRSUsv0zfBTJWh3QJh9FNxZTMrGxU=
github.com/mattn/go-isatty v0.0.24 h1:tGZZoVgT/KiqK1c8ocVLeDS8BSWMRd47J3Lbz7vsReI=
Expand Down
16 changes: 13 additions & 3 deletions processor/audit.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,8 @@ func fileHashToRecord(f database.FileHash) *AuditRecord {
add(HashNames.Sha3384, f.Sha3384)
add(HashNames.Sha3512, f.Sha3512)
add(HashNames.Ed2k, f.Ed2k)
add(HashNames.Tiger, f.Tiger)
add(HashNames.Whirlpool, f.Whirlpool)
return r
}

Expand Down Expand Up @@ -213,6 +215,14 @@ func sqliteAuditHashes(pathName string) ([]string, error) {
if keyed != 1 || shadowed != 0 || withoutRowid != 0 || text != 1 || hasCollateClause(tableSQL.String) || nullPath != 0 || !strings.EqualFold(collation.String, "binary") {
return nil, errors.New("audit database was not created by hashit, its file_hashes table must have a text filepath on every row as its primary key, compared exactly, and a rowid")
}
// databases written before tiger and whirlpool were added, such as by 1.5.0, lack their columns
var current int
if err := db.QueryRow("select count(*) from pragma_table_info('file_hashes') where name in ('tiger', 'whirlpool')").Scan(&current); err != nil {
return nil, err
}
if current != 2 {
return nil, errors.New("audit database was written by an older version of hashit, which this version cannot read, so create it again with this version")
}

// a database of an empty directory has no records, and passes when audited against one
// databases record which hashes they hold, but those from earlier development builds do not,
Expand Down Expand Up @@ -542,7 +552,7 @@ func markSqliteDuplicates(ctx context.Context, tx *sql.Tx, base string) (int, er
path := `replace(filepath, '\', '/')`
values := []string{"substr(" + path + ", length(rtrim(" + path + ", replace(" + path + ", '/', ''))) + 1)"}
for _, column := range []string{"crc32", "xxhash64", "md4", "md5", "sha1", "sha224", "sha256", "sha384", "sha512",
"blake2b_256", "blake2b_512", "blake3", "sha3_224", "sha3_256", "sha3_384", "sha3_512", "ed2k"} {
"blake2b_256", "blake2b_512", "blake3", "sha3_224", "sha3_256", "sha3_384", "sha3_512", "ed2k", "tiger", "whirlpool"} {
values = append(values, "lower(ifnull("+column+", ''))")
}
key := "size || '|' || " + strings.Join(values, " || '|' || ")
Expand Down Expand Up @@ -851,7 +861,7 @@ func judgeSqliteAudit(ctx context.Context, db *sql.DB, tx *sql.Tx, stmts *sqlite

// stream every record in path order joined against what we saw, reporting as we go
rows, err := db.QueryContext(ctx, `select f.filepath, f.crc32, f.xxhash64, f.md4, f.md5, f.sha1, f.sha224, f.sha256, f.sha384, f.sha512,
f.blake2b_256, f.blake2b_512, f.blake3, f.sha3_224, f.sha3_256, f.sha3_384, f.sha3_512, f.ed2k, f.size, f.mtime,
f.blake2b_256, f.blake2b_512, f.blake3, f.sha3_224, f.sha3_256, f.sha3_384, f.sha3_512, f.ed2k, f.tiger, f.whirlpool, f.size, f.mtime,
s.status, s.disk_file
from file_hashes f left join temp.seen s on s.filepath = f.filepath
where f.rowid not in (select rid from temp.duplicates)
Expand All @@ -871,7 +881,7 @@ order by f.filepath`)
var status sql.NullInt64
var diskFile sql.NullString
err := rows.Scan(&f.Filepath, &f.Crc32, &f.Xxhash64, &f.Md4, &f.Md5, &f.Sha1, &f.Sha224, &f.Sha256, &f.Sha384, &f.Sha512,
&f.Blake2b256, &f.Blake2b512, &f.Blake3, &f.Sha3224, &f.Sha3256, &f.Sha3384, &f.Sha3512, &f.Ed2k,
&f.Blake2b256, &f.Blake2b512, &f.Blake3, &f.Sha3224, &f.Sha3256, &f.Sha3384, &f.Sha3512, &f.Ed2k, &f.Tiger, &f.Whirlpool,
&f.Size, &f.Mtime, &status, &diskFile)
if err != nil {
printError(fmt.Sprintf("failed to read audit database: %s", err.Error()))
Expand Down
Loading
Loading