UNPKG

@nostr-dev-kit/blossom

Version:

Blossom protocol support for NDK (Nostr Development Kit)

40 lines 1.25 kB
/** * Default implementation of SHA256 calculator * Uses Web Crypto API */ export class DefaultSHA256Calculator { /** * Calculate SHA256 hash of a file * * @param file File to hash * @returns Hash as hex string */ async calculateSha256(file) { // Convert file to ArrayBuffer const buffer = await file.arrayBuffer(); // Hash the buffer const hashBuffer = await crypto.subtle.digest("SHA-256", buffer); // Convert to hex string return Array.from(new Uint8Array(hashBuffer)) .map((b) => b.toString(16).padStart(2, "0")) .join(""); } } /** * Singleton instance of the default SHA256 calculator * Created lazily to avoid issues in non-browser environments */ let _defaultSHA256Calculator; export function getDefaultSHA256Calculator() { if (!_defaultSHA256Calculator) { _defaultSHA256Calculator = new DefaultSHA256Calculator(); } return _defaultSHA256Calculator; } // For backwards compatibility, export as defaultSHA256Calculator export const defaultSHA256Calculator = { calculateSha256: async (file) => { return getDefaultSHA256Calculator().calculateSha256(file); }, }; //# sourceMappingURL=sha256.js.map