question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How do I adapt the example in the Usage section to work in React Native?

See original GitHub issue
import crypto from 'isomorphic-webcrypto'

crypto.subtle.digest(
  { name: 'SHA-256' },
  new Uint8Array([1,2,3]).buffer
)
.then(hash => {
  // do something with the hash buffer
})

This does not work as is in React Native. I have tried the following, but I’m not really sure how to get the values into the digest function. I just want to prove it works as it should.

    const view = new Int8Array( new ArrayBuffer(3));
    crypto.subtle.digest(
      { name: 'SHA-256' },
      view
    )
    .then(hash => {
      // do something with the hash buffer
      console.log('hash: ', hash);
    });

Thanks!

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:7 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
kevlenedcommented, Mar 19, 2018

I added additional details about hashing to the README. I also included an example of how to generate a hash in the React Native examples.

1reaction
kevlenedcommented, Mar 19, 2018

It should return an ArrayBuffer. If you perform toString() on that you’ll get something like ArrayBuffer []. Brief explanation:

An ArrayBuffer is an untyped array that just represents a chunk of memory with a defined length. In order to get a view of that memory, you have to cast it to a typed array. Often, this is a Uint8Array. The U makes it unsigned, so it’s alway positive and the 8 makes each element represent one byte.

I imagine what you want is a SHA256 hash as a string. This is most commonly represented in hex. I wrote hex-lite to make this easy, so here’s an example:

import crypto from 'isomorphic-webcrypto';
import hex from 'hex-lite';

crypto.subtle.digest(
  { name: 'SHA-256' },
  new Uint8Array([1,2,3]).buffer
)
.then(hash => {
  console.log('sha256 hash', hex.fromBuffer(hash));
});
Read more comments on GitHub >

github_iconTop Results From Across the Web

React Native Navigation: Tutorial with examples
In this React Native Navigation tutorial, we'll show you some examples of navigation patterns you can implement with React Navigation.
Read more >
Integration with Existing Apps - React Native
To use the power of autolinking, we have to apply it a few places. First add the following entry to settings.gradle : apply...
Read more >
Introducing Hooks - React
Completely opt-in. You can try Hooks in a few components without rewriting any existing code. But you don't have to learn or use...
Read more >
Accessibility - React
Sometimes we break HTML semantics when we add <div> elements to our JSX to make our React code work, especially when working with...
Read more >
Integrating with Other Libraries - React
While it is generally recommended to use unidirectional data flow such as React state, Flux, or Redux, React components can use a model...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found