Quantum
How do I make the crypto.subtle.digest algo flexible?
Oh nice! I wasn't even looking at the lower cased
digestAlgorithms
that allows you to call includes on it. Yes, your guard approach is excellent and works nicely. Thanks for this and the insights!26 replies
How do I make the crypto.subtle.digest algo flexible?
If it is recommended that the
new
method here only has algo as DigestAlgorithm, I could abide by that too, ok. But in any case, I'm getting that issue when attempting to use includes
on DigestAlgorithm
.26 replies
How do I make the crypto.subtle.digest algo flexible?
Yes, I understand and agree with casting being necessary when input is coming from untrusted source and should only live at outer edge of program or library. The
new
method is the outer edge of the library where the input comes in.
I am attempting to work in the DigestAlgorithm.includes(algo)
that you showed above, but I am getting this error here:
'DigestAlgorithm' only refers to a type, but is being used as a value here.deno-ts(2693)
26 replies
How do I make the crypto.subtle.digest algo flexible?
Maybe the algo comes from the database or maybe it comes from user input or somewhere else. Something needs to check it against DigestAlgorithm somewhere and the best that I could see how to do that until now was with "if" statements or something.
What @marvinh. seemed to indicate is there is a way to cast algo to a DigestAlgorithm in the method signature that could then be used with "DigestAlgorithm.includes", which is interesting and might be what is needed here.
26 replies
How do I make the crypto.subtle.digest algo flexible?
The "str is DigestAlgorithm" is interesting and might be what allows DigestAlgorithm.includes(algo) to work. How would I implement that on my "new" method above?
Something like this doesn't quite work:
static async new({algo, seed}: {algo: string, seed: string}): Promise<Random>, algo is DigestAlgorithm {
26 replies
How do I make the crypto.subtle.digest algo flexible?
I think I was hoping Deno and/or TypeScript would allow for something like the following so that there is no need for all the "if" statements, which is error prone and tedious. More importantly, whenever Deno adds a new one to DigestAlgorithm (they added "BLAKE2B-128" 2 months ago), I would also need to change the code to add another "if" statement. What I was hoping for is something kind of like this:
If something along those lines can't be accomplished, I'm seeing this as a wider language issue. DigestAlgorithm is just an example. The same problem would come up elsewhere.
26 replies
How do I make the crypto.subtle.digest algo flexible?
@cknight I am writing the beginnings of a deterministic pseudo random library that allows the user of the library to specify what algorithm to use. It would be a separate issue to discuss the merits of such a library. (Very happy to discuss!) The best that I can do to make this flexible is by doing the following:
26 replies