Hashing
The DID Spaces SDK provides essential utilities for managing data integrity through hashing functions. These functions allow you to calculate content hashes for objects and verify their integrity, ensuring that data remains unaltered during storage and retrieval operations.
For other foundational helper functions, refer to the Utilities section.
getHash
#
The getHash
function is used to calculate the cryptographic hash of an object's content. This hash serves as a unique identifier for the content and is crucial for verifying data integrity.
Parameters
Name | Type | Description |
---|---|---|
|
| The content stream for which the hash needs to be calculated. |
Returns
Name | Type | Description |
---|---|---|
|
| The calculated hash string of the content. |
Example
import { createReadStream } from 'fs-extra';
import { getHash } from '@did-space/client';
async function calculateFileHash(filePath) {
try {
const fileStream = createReadStream(filePath);
const hash = await getHash(fileStream);
console.log(`Hash of ${filePath}: ${hash}`);
return hash;
} catch (error) {
console.error(`Error calculating hash for ${filePath}:`, error.message);
}
}
// Example usage
calculateFileHash('./path/to/your/file.txt');
This example demonstrates how to calculate the hash of a local file using getHash
by creating a readable stream from the file.
verifyObjectHash
#
The verifyObjectHash
function is critical for ensuring the integrity of objects stored in or retrieved from DID Spaces. It calculates the hash of an object at a given path and compares it against a provided expected hash. If the hashes do not match, it indicates data corruption or tampering, and the function will remove the corrupted object and throw an error.
Parameters
Name | Type | Description |
---|---|---|
|
| The file path to the object whose hash needs to be verified. |
|
| The expected hash value to compare against. |
Returns
Name | Type | Description |
---|---|---|
|
| This function does not return a value on success. It throws an error if the hash does not match. |
Process Flow
Example
import { verifyObjectHash } from '@did-space/client';
async function verifyDownloadedObject(filePath, expectedHash) {
try {
await verifyObjectHash(filePath, expectedHash);
console.log(`Object at ${filePath} successfully verified.`);
} catch (error) {
console.error(`Verification failed for ${filePath}: ${error.message}`);
}
}
// Example usage after downloading a file
const downloadedFilePath = './downloaded_data.zip';
const knownGoodHash = 'some-pre-calculated-hash-value'; // Replace with the actual expected hash
verifyDownloadedObject(downloadedFilePath, knownGoodHash);
This example illustrates how to use verifyObjectHash
to check if a downloaded file's integrity matches its known hash. If the hashes don't align, the file is automatically removed, and an error is logged.
Understanding and utilizing these hashing utilities is crucial for building robust applications that interact with DID Spaces, ensuring the reliability and integrity of your stored data. Explore more helper functions in the File System utilities section for data manipulation.