/**
 * {@link Digest} class provides methods to compute cryptographic hashes.
 */
const hasNativeToBase64 = typeof Uint8Array.prototype.toBase64 === 'function';
export class Digest {
    _textEncoder = new TextEncoder();
    /**
     * Computes the hash of a given text input using the specified algorithm.
     * @param input - The text input to hash.
     * @param algorithm - The hash algorithm to use.
     * @returns A promise that resolves to the hash as an `ArrayBuffer`.
     */
    text(input, algorithm) {
        return this.data(this._textEncoder.encode(input), algorithm);
    }
    /**
     * Computes the hash of a given data input using the specified algorithm.
     * @param input - The data input to hash.
     * @param algorithm - The hash algorithm to use.
     * @returns A promise that resolves to the hash as an `ArrayBuffer`.
     */
    data(input, algorithm) {
        return crypto.subtle.digest(algorithm, input);
    }
    /**
     * Converts an `ArrayBuffer` to a Base64 encoded string.
     * @param input - The `ArrayBuffer` to convert.
     * @returns The Base64 encoded string.
     */
    toBase64(input) {
        const uint8Array = input instanceof Uint8Array ? input : new Uint8Array(input);
        if (hasNativeToBase64) {
            return uint8Array.toBase64();
        }
        let binaryString = '';
        for (let i = 0; i < uint8Array.length; i++) {
            binaryString += String.fromCharCode(uint8Array[i]);
        }
        return btoa(binaryString);
    }
    /**
     * Computes the hash of a given `Blob` input using the specified algorithm.
     * @param input - The `Blob` input to hash.
     * @param algorithm - The hash algorithm to use.
     * @returns A promise that resolves to the hash as an `ArrayBuffer`.
     */
    async blob(input, algorithm) {
        const arrayBuffer = await input.arrayBuffer();
        return crypto.subtle.digest(algorithm, arrayBuffer);
    }
}
/**
 * An instance of the {@link Digest} class.
 */
export const digest = new Digest();
//# sourceMappingURL=digest.js.map