# core/converters/fingerprint.converter.ts

> 36 lines of code and 5 definitions.

Tree: Content Fingerprint
Language: typescript
Layer: processing
Canonical: https://banes-lab.com/anatomy/content-fingerprint#file-content-fingerprint-core-converters-fingerprint-converter-ts
Source text: https://banes-lab.com/source/content-fingerprint/core/converters/fingerprint.converter.ts.txt

Listed in [core/converters](https://banes-lab.com/api/source/content-fingerprint/core/converters.md).

## Definitions

- `hashFile` (lexical_declaration, line 11, exported)
- `fingerprint` (lexical_declaration, line 18, exported)
- `digestOf` (lexical_declaration, line 29, exported)
- `fingerprintOf` (lexical_declaration, line 33, exported)
- `hasher` (lexical_declaration, line 34, exported)

## Contained in

- [core/converters](https://banes-lab.com/anatomy/content-fingerprint/folder-content-fingerprint-core-converters.md)

## Linked from

- [core/converters](https://banes-lab.com/anatomy/content-fingerprint/folder-content-fingerprint-core-converters.md)

## Source

```typescript
import {
    ABSENT_SENTINEL,
    HASH_ALGORITHM,
    HASH_ENCODING,
    RECORD_SEPARATOR,
    UNIT_SEPARATOR,
} from "#configuration/constants/fingerprint.constants";
import { existsSync, readFileSync } from "node:fs";
import { createHash } from "node:crypto";

export const hashFile = function hashFile(file: string): string {
    if (!existsSync(file)) {
        return ABSENT_SENTINEL;
    }
    return createHash(HASH_ALGORITHM).update(readFileSync(file)).digest(HASH_ENCODING);
};

export const fingerprint = function fingerprint(files: readonly string[]): string {
    const hasher = createHash(HASH_ALGORITHM);
    for (const file of [...files].sort((a, b) => a.localeCompare(b))) {
        hasher.update(file);
        hasher.update(UNIT_SEPARATOR);
        hasher.update(hashFile(file));
        hasher.update(RECORD_SEPARATOR);
    }
    return hasher.digest(HASH_ENCODING);
};

export const digestOf = function digestOf(body: string): string {
    return createHash(HASH_ALGORITHM).update(body).digest(HASH_ENCODING);
};

export const fingerprintOf = function fingerprintOf(parts: readonly string[]): string {
    const hasher = createHash(HASH_ALGORITHM);
    for (const part of parts) {
        hasher.update(part);
        hasher.update(RECORD_SEPARATOR);
    }
    return hasher.digest(HASH_ENCODING);
};
```
