DenoDDeno
Powered by
QuantumQ
Denoβ€’3y agoβ€’
25 replies
Quantum

How do I make the crypto.subtle.digest algo flexible?

This is a learning moment, bear with me. πŸ˜…

I can hard code an algo for the digest like this:
const algo = "SHA-256";
const d = new Uint8Array(
    await crypto.subtle.digest(
        algo, new TextEncoder().encode("test")
    )
);
const algo = "SHA-256";
const d = new Uint8Array(
    await crypto.subtle.digest(
        algo, new TextEncoder().encode("test")
    )
);


But if I want to make the algo flexible, it forces me to make it a DigestAlgorithm and it cannot be a string shown here:
async function digest(algo: string): Promise<Uint8Array> {
    return new Uint8Array(
        await crypto.subtle.digest(
            algo, new TextEncoder().encode("test")
        )
    );
}
async function digest(algo: string): Promise<Uint8Array> {
    return new Uint8Array(
        await crypto.subtle.digest(
            algo, new TextEncoder().encode("test")
        )
    );
}


I reviewed what that is in the source:
https://github.com/denoland/deno_std/blob/main/crypto/_wasm/mod.ts

I see this:
export type DigestAlgorithm = typeof digestAlgorithms[number];
export type DigestAlgorithm = typeof digestAlgorithms[number];

(I don't quite see what is going on here and am interested.)

Bottom line is, when I make algo:string a algo:DigestAlgorithm in the digest function param list, it stops complaining, but I need to have some calling code somewhere be a flexible string (or maybe an enum if necessary) that somehow translates to a DigestAlgorithm for this to work.

I've been struggling with this. Any ideas?
Deno banner
DenoJoin
Chat about Deno, a modern runtime for JavaScript and TypeScript.
20,944Members
Resources
Recent Announcements

Similar Threads

Was this page helpful?

Similar Threads

Issues with Crypto Subtle Digest ext:deno_crypto/00_crypto.js
beanBbean / help
3y ago
definition for subtle crypto broken?
jcayzacJjcayzac / help
3mo ago
Updating digest with web crypto
ioBIioB / help
4y ago
How do I use crypto.subtle.importKey to verify the signature sent by Admob SSV ?
TheCarpetMerchantTTheCarpetMerchant / help
2y ago