sigit.io/src/utils/hash.ts

20 lines
710 B
TypeScript
Raw Normal View History

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) => {
// Compute the SHA-256 hash of the array buffer
2024-04-22 11:24:50 +00:00
const hash = await sha256(input).catch((err) => {
// 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`)
return null // Return null if hashing fails
})
return hash // Return the SHA-256 hash
}