Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Web crypto stringToArrayBuffer sample implementation seems off #231

Closed
priyajeet opened this issue Oct 4, 2023 · 3 comments
Closed

Web crypto stringToArrayBuffer sample implementation seems off #231

priyajeet opened this issue Oct 4, 2023 · 3 comments
Labels
needs triage Triage needed by staff and/or partners. Automatically applied when an issue is opened.

Comments

@priyajeet
Copy link

priyajeet commented Oct 4, 2023

Looks like these lines do nothing since buf is returned. Should return bufView? Which may also fix issues related to nodejs/node#46067 (see upstream issues linked to this issue and solutions) which was causing errors like below thrown for people using that implementation.

'importkey' on 'subtlecrypto': 2nd argument is not instance of arraybuffer, buffer, typedarray, or dataview.

Example code on this page also needs fixing as it copy/pastes the stringToArrayBuffer implementation.
https://developer.mozilla.org/en-US/docs/Web/API/SubtleCrypto/importKey

Also see, where bufView is returned.
https://stackoverflow.com/questions/34814480/how-to-load-a-public-key-in-pem-format-for-encryption

@caugner caugner added the needs triage Triage needed by staff and/or partners. Automatically applied when an issue is opened. label Oct 10, 2023
@ferdnyc
Copy link
Contributor

ferdnyc commented Oct 10, 2023

@priyajeet No, returning buf is correct.

The code in question is:

  /*
  Convert a string into an ArrayBuffer
  from https://developers.google.com/web/updates/2012/06/How-to-convert-ArrayBuffer-to-and-from-String
  */
  function str2ab(str) {
    const buf = new ArrayBuffer(str.length);
    const bufView = new Uint8Array(buf);
    for (let i = 0, strLen = str.length; i < strLen; i++) {
      bufView[i] = str.charCodeAt(i);
    }
    return buf;
  }

As MDN:ArrayBuffer explains:

You cannot directly manipulate the contents of an ArrayBuffer; instead, you create one of the typed array objects or a DataView object which represents the buffer in a specific format, and use that to read and write the contents of the buffer.

bufView is only created as an accessor used to manipulate the contents of buf. It inserts character values into buf, which is then returned once it's been populated.

@ferdnyc
Copy link
Contributor

ferdnyc commented Oct 10, 2023

(I can't speak to the correctness of the surrounding/calling code, but that function is named str2ab and that's what it does: Takes a string as input, and returns an ArrayBuffer representation of that string.)

@ferdnyc
Copy link
Contributor

ferdnyc commented Oct 12, 2023

I posted a comment on that StackOverflow answer you linked to, noting the incorrect return value in the code there.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
needs triage Triage needed by staff and/or partners. Automatically applied when an issue is opened.
Projects
None yet
Development

No branches or pull requests

3 participants