@fnet/checksum
v0.1.10
Published
A simple utility to calculate the checksum of a file, directory or content
Downloads
190
Readme
@fnet/checksum
This project offers a straightforward tool for generating checksums, which are useful for verifying the integrity of files, directories, or data structures. It provides a reliable method to ensure that the contents of your data remain consistent and unchanged during transfer or storage.
How It Works
The tool computes a checksum, a form of a digital fingerprint, using a specified hashing algorithm (defaulted to SHA-256) for various types of inputs. Whether you want to validate the integrity of a single file, a whole directory, or even a complex object structure, this utility offers a simple and effective way to calculate these checksums.
Key Features
- File Checksum: Compute the checksum of a single file to ensure its integrity.
- Directory Checksum: Calculate a checksum for all files within a directory, maintaining an alphabetical order for consistency.
- Object and Array Checksum: Supports generating checksums for strings, buffers, nested objects, and arrays, giving flexible support for various data structures.
Conclusion
In summary, @fnet/checksum provides a dependable way to compute checksums for files, directories, and various data structures. It aids users in confirming the data integrity in a straightforward and efficient manner.
Developer Guide for @fnet/checksum
Library
Overview
The @fnet/checksum
library is designed to help developers compute checksums for various types of data, ensuring data integrity and reliability. It provides functions to generate checksums for files, directories, and data objects using configurable hashing algorithms. This can be particularly useful for verifying data consistency during file transfers, backups, or data storage.
Installation
To add @fnet/checksum
to your project, use either npm or yarn:
npm install @fnet/checksum
or
yarn add @fnet/checksum
Usage
The library exports a core function that allows you to compute checksums. Here's how you can use it in different scenarios:
import calculateChecksum from '@fnet/checksum';
// Example: Calculate checksum for a string or Buffer content
calculateChecksum({ content: 'Sample data' }).then(checksum => {
console.log(checksum);
});
// Example: Calculate checksum for files
calculateChecksum({ path: '/path/to/file.txt' }).then(checksum => {
console.log(checksum);
});
// Example: Calculate checksum for directories
calculateChecksum({ path: '/path/to/directory' }).then(checksum => {
console.log(checksum);
});
// Example: Calculate checksum for an object
calculateChecksum({ content: { 'file1': 'data1', 'file2': 'data2' } }).then(checksum => {
console.log(checksum);
});
Examples
Calculate Checksum for Simple Text
calculateChecksum({ content: 'Hello World' }).then(checksum => {
console.log('Checksum for text:', checksum.value);
});
Calculate Checksum for a File
calculateChecksum({ path: '/path/to/myfile.txt' }).then(checksum => {
console.log('Checksum for file:', checksum.value);
});
Calculate Checksum for a Directory
calculateChecksum({ path: '/path/to/mydir' }).then(checksum => {
console.log('Checksum for directory:', checksum.value);
console.log('Files included in checksum:', checksum.inputs);
});
Calculate Checksum for an Object
const dataObject = {
'file1.txt': 'This is content of file1',
'file2.txt': 'This is content of file2'
};
calculateChecksum({ content: dataObject }).then(checksum => {
console.log('Checksum for object:', checksum.value);
});
Acknowledgement
The @fnet/checksum
library utilizes Node.js built-in modules for filesystem operations and cryptographic processes. Special thanks to contributors and the open-source community for making such tools accessible and reliable.
Input Schema
$schema: https://json-schema.org/draft/2020-12/schema
type: object
properties:
content:
oneOf:
- type: string
- type: array
items:
type: string
- type: object
additionalProperties:
oneOf:
- type: string
- type: object
additionalProperties:
type:
- string
- object
- type: array
items:
type: string
propertyNames:
type: string
description: The content to calculate checksum for. Can be string, Buffer (as
string), or an object with nested values.
path:
type: string
description: The file or directory path to calculate checksum from.
algorithm:
type: string
default: sha256
description: The hashing algorithm to use.
required: []
additionalProperties: false