grafana / xk6-webcrypto

WIP implementation of the WebCrypto specification for k6
GNU Affero General Public License v3.0
7 stars 4 forks source link

Implement support for the crypto.getRandomValues operation #21

Closed oleiade closed 1 year ago

oleiade commented 2 years ago

This PR addresses #19

It adds an implementation for the crypto.getRandomValues operation to the extension.

import { crypto } from "k6/x/webcrypto";

export default function () {
  const array = new Uint32Array(10);
  crypto.getRandomValues(array);

  for (const num of array) {
    console.log(num);
  }
}

The underlying implementation depends on the Go rand/crypto.Read method as a PRNG. This standard functionality relies on the underlying OS' pseudo-random number generator, such as /dev/urandom on Linux.

This implementation suffers a few limitations that shouldn't be a deal-breaker, but for which I couldn't find any acceptable workaround at this day:

Let me know what you think šŸ™‡šŸ»

mstoykov commented 1 year ago

The specification indicates that certain specific errors such as QuotaExceededError should be returned under certain conditions. At the moment I have defined specific errors the Go way, and I throw them using common.Throw; but that doesn't allow me to, for instance, switch on them in the context of a catch statement, as in:

AFAIK this will require that you actually define this error and then make instances of them.