> ## Documentation Index
> Fetch the complete documentation index at: https://docs.mirage.strukto.ai/llms.txt
> Use this file to discover all available pages before exploring further.

# Checksums

> Answer md5sum and sha256sum from a stored hash.

`md5sum`, `sha256sum` and the other hash commands can ask your VFS for a hash
it already stores, instead of reading and hashing the file. Return `None`
(TypeScript: `null`) and Mirage hashes the file.

For example, [Google Drive](https://docs.mirage.strukto.ai/python/vfs/gdrive.md) answers `md5sum`, `sha1sum`
and `sha256sum` from the hashes Drive stores.

## `checksum`

The hash of one file.

> [tab] Python
>
> ```python theme={null}
> async def checksum(self, path: PathSpec, algorithm: str) -> str | None: ...
> ```

> [tab] TypeScript
>
> ```typescript theme={null}
> checksum(path: PathSpec, algorithm: string): Promise<string | null>
> ```

| Argument | Meaning |
| - | - |
| `path` | The file. |
| `algorithm` | `md5`, `sha1`, `sha224`, `sha256`, `sha384`, `sha512`, `blake2b`, or `blake2b-N` for BLAKE2b at N bits. |

| Return | Mirage then |
| - | - |
| The lowercase hex hash of exactly the bytes a read returns | Prints it. The file is not read. |
| `None` | Reads the file and hashes it. |

These commands call `checksum`, with this `algorithm`:

* `md5sum`, `md5`: `md5`
* `sha1sum`: `sha1`
* `sha224sum`: `sha224`
* `sha256sum`: `sha256`
* `sha384sum`: `sha384`
* `sha512sum`: `sha512`
* `b2sum`: `blake2b`, or `blake2b-N` with `-l N`

This reads the file instead:

* `-c`: check hashes listed in a file

## Example

This example extends `NotesVFS` from [Custom VFS](https://docs.mirage.strukto.ai/home/setup/custom-vfs.md#example-a-read-only-vfs).
A real service returns the hash it stores; this one computes MD5 and leaves the
others to Mirage:

> [tab] Python
>
> ```python theme={null}
> import hashlib
>
> from mirage import PathSpec
>
> from notes import NotesVFS
>
>
> class HashedNotesVFS(NotesVFS):
>     async def checksum(self, path: PathSpec, algorithm: str) -> str | None:
>         if algorithm != "md5":
>             return None
>         return hashlib.md5(self.page(path).encode()).hexdigest()
> ```

> [tab] TypeScript
>
> ```typescript theme={null}
> import { createHash } from 'node:crypto'
> import type { PathSpec } from '@struktoai/mirage-node'
> import { NotesVFS } from './notes.ts'
>
> export class HashedNotesVFS extends NotesVFS {
>   override async checksum(path: PathSpec, algorithm: string): Promise<string | null> {
>     if (algorithm !== 'md5') return null
>     return createHash('md5').update(this.page(path)).digest('hex')
>   }
> }
> ```

`md5sum /notes/hello.txt` then reads nothing. `sha256sum` still reads the page.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.