Although the specification defines the SubtleCrypto.digest operation as strictly taking ArrayBuffer instances as data parameters, [this conversation]() brought up the idea of accepting strings as a convenience too. In a standard JS runtime environment, converting from ArrayBuffer to string would be trivial using something like TextEncoder. In k6, we do not have such an API at our disposal.
Solution space
My understanding is that we have three main options:
Derive from the specs to have the digest operation accept strings as input as a convenience. It is already the case in the current k6 crypto package and would be a valuable convenience for users. There are no official tests for this in the Web Platform Tests, but as Goja has native support for strings, the conversion from string to something that can be comprehended as an array of bytes by the rest of the webcrypto implementation's logic would be simple enough to test.
Either expose a string to array buffer, and vice versa helper function either in the context of a jslib, or as a documentation artifact. For instance:
Problem definition
Although the specification defines the
SubtleCrypto.digest
operation as strictly takingArrayBuffer
instances as data parameters, [this conversation]() brought up the idea of accepting strings as a convenience too. In a standard JS runtime environment, converting fromArrayBuffer
tostring
would be trivial using something likeTextEncoder
. In k6, we do not have such an API at our disposal.Solution space
My understanding is that we have three main options:
digest
operation accept strings as input as a convenience. It is already the case in the current k6 crypto package and would be a valuable convenience for users. There are no official tests for this in the Web Platform Tests, but as Goja has native support for strings, the conversion from string to something that can be comprehended as an array of bytes by the rest of the webcrypto implementation's logic would be simple enough to test.function stringToArrayBuffer(s) { return Uint8Array.from(new String(s), (x) => x.charCodeAt(0)); }