---
title: "@push.rocks/smarthash"
---
# @push.rocks/smarthash
simplified access to node hash functions

## Install
To install `@push.rocks/smarthash`, use the following command with npm:

```bash
npm install @push.rocks/smarthash --save
```

This will add `@push.rocks/smarthash` to your project's dependencies.

## Usage
The `@push.rocks/smarthash` library provides a simplified interface to Node.js hash functions, including utilities for hashing strings, files, and streams with popular algorithms like SHA-256 and MD5. Below are detailed examples to demonstrate the capabilities and usage of this library using ESM syntax and TypeScript.

### Hashing Strings
#### SHA-256
```typescript
import { sha256FromString, sha256FromStringSync } from '@push.rocks/smarthash';

// Asynchronously hash a string
const asyncHash = async () => {
  const hash = await sha256FromString('Hello, world!');
  console.log(hash); // Output the hashed string
};
asyncHash();

// Synchronously hash a string
const syncHash = sha256FromStringSync('Hello, world!');
console.log(syncHash); // Output the hashed string
```

#### MD5
```typescript
import { md5FromString } from '@push.rocks/smarthash';

// Asynchronously hash a string with MD5
const md5Hash = async () => {
  const hash = await md5FromString('Hello, world!');
  console.log(hash); // Output the MD5 hash
};
md5Hash();
```

### Hashing Files
To hash a file, you can use the `sha256FromFile` function. It reads the file specified by the path and calculates the SHA-256 hash.
```typescript
import { sha256FromFile } from '@push.rocks/smarthash';

// Asynchronously hash a file
const hashFile = async () => {
  const fileHash = await sha256FromFile('./path/to/your/file.txt');
  console.log(fileHash); // Output the file's hash
};
hashFile();
```

### Hashing Streams
`@push.rocks/smarthash` also allows for hashing of streams, which is particularly useful for large files or data streams.
```typescript
import { sha256FromStream } from '@push.rocks/smarthash';
import fs from 'fs';

// Create a read stream from a file
const readStream = fs.createReadStream('./path/to/your/largeFile.txt');

// Asynchronously hash the stream
const hashStream = async () => {
  const streamHash = await sha256FromStream(readStream);
  console.log(streamHash); // Output the hash of the stream's content
};
hashStream();
```

### Advanced Usage
#### Hashing Objects
For hashing JavaScript objects, you can serialize them and hash the resulting string. This is useful for creating hashes from complex data structures.
```typescript
import { sha265FromObject } from '@push.rocks/smarthash';

// Hash a JavaScript object
const hashObject = async () => {
  const object = { hello: 'world', number: 42 };
  const objectHash = await sha265FromObject(object);
  console.log(objectHash); // Output the hash of the object
};
hashObject();
```

#### Error Handling
All asynchronous functions return promises, so you can use `.catch()` for error handling or try-catch blocks within async functions.
```typescript
import { sha256FromString } from '@push.rocks/smarthash';

// Using .catch() for promises
sha256FromString('test')
  .then(hash => console.log(hash))
  .catch(error => console.error(error));

// Using try-catch with async functions
const getHash = async () => {
  try {
    const hash = await sha256FromString('test');
    console.log(hash);
  } catch (error) {
    console.error(error);
  }
};
getHash();
```

### Best Practices
- Utilize asynchronous functions for hashing large files or streams to avoid blocking the main thread.
- For hashing sensitive information, ensure to follow security best practices, including using up-to-date algorithms and managing secrets properly.

By leveraging `@push.rocks/smarthash`, developers can easily integrate hashing functionalities into their Node.js applications, benefiting from its simplified API and TypeScript support for better development experience.

## License and Legal Information

This repository contains open-source code that is licensed under the MIT License. A copy of the MIT License can be found in the [license](license) file within this repository. 

**Please note:** The MIT License does not grant permission to use the trade names, trademarks, service marks, or product names of the project, except as required for reasonable and customary use in describing the origin of the work and reproducing the content of the NOTICE file.

### Trademarks

This project is owned and maintained by Task Venture Capital GmbH. The names and logos associated with Task Venture Capital GmbH and any related products or services are trademarks of Task Venture Capital GmbH and are not included within the scope of the MIT license granted herein. Use of these trademarks must comply with Task Venture Capital GmbH's Trademark Guidelines, and any usage must be approved in writing by Task Venture Capital GmbH.

### Company Information

Task Venture Capital GmbH  
Registered at District court Bremen HRB 35230 HB, Germany

For any legal inquiries or if you require further information, please contact us via email at hello@task.vc.

By using this repository, you acknowledge that you have read this section, agree to comply with its terms, and understand that the licensing of the code does not imply endorsement by Task Venture Capital GmbH of any derivative works.
