2024-04-18 11:12:11 +00:00
|
|
|
import { sha256 } from 'crypto-hash'
|
|
|
|
import { toast } from 'react-toastify'
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Computes the SHA-256 hash of an ArrayBuffer.
|
|
|
|
* @param arrayBuffer The ArrayBuffer to hash.
|
|
|
|
* @returns A Promise resolving to the SHA-256 hash as a hexadecimal string, or null if hashing fails.
|
|
|
|
*/
|
2024-04-22 11:24:50 +00:00
|
|
|
export const getHash = async (input: ArrayBuffer | string) => {
|
2024-04-18 11:12:11 +00:00
|
|
|
// Compute the SHA-256 hash of the array buffer
|
2024-04-22 11:24:50 +00:00
|
|
|
const hash = await sha256(input).catch((err) => {
|
2024-04-18 11:12:11 +00:00
|
|
|
// Handle error if hashing fails
|
|
|
|
console.log(`error occurred in hashing arrayBuffer :>> `, err)
|
2024-04-22 11:24:50 +00:00
|
|
|
toast.error(err.message || `error occurred in hashing`)
|
2024-04-18 11:12:11 +00:00
|
|
|
return null // Return null if hashing fails
|
|
|
|
})
|
|
|
|
|
|
|
|
return hash // Return the SHA-256 hash
|
|
|
|
}
|