Used to check for browser translation.
用于检测浏览器翻译。
ブラウザの翻訳を検出する
Utilities

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

input

ReadableStream

The content stream for which the hash needs to be calculated.

Returns

Name

Type

Description

hash

string

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

key

string

The file path to the object whose hash needs to be verified.

hash

string

The expected hash value to compare against.

Returns

Name

Type

Description

void

void

This function does not return a value on success. It throws an error if the hash does not match.

Process Flow

Yes

No

Start Verification

Get Object Key and Expected Hash

Read Object from Key

Calculate Real Hash

Real Hash Matches Expected Hash?

Object Integrity Verified

Remove Corrupted Object

Throw Error: Hash Mismatch

End


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.