react-native-quick-crypto icon indicating copy to clipboard operation
react-native-quick-crypto copied to clipboard

🐛 Calcuating hash of long strings is quite slow

Open Clarkkkk opened this issue 10 months ago • 0 comments

What's happening?

For example:

const hash = QuickCrypto.createHash('sha256');
hash.update(longText);
const hashValue = hash.copy().digest('hex')

The longText is a 8MB text file, and it takes 3s on a Snapdragon 870 device.

After some investigations, i found that it is because the string is converted to buffer on the js side first, and the conversion is slow. Since react-native-quick-crypto is using JSI already, can it provide a method to consume a string directly?

void HybridHash::updateString(const std::string& data) {
  if (!ctx) {
    throw std::runtime_error("Hash context not initialized");
  }

  // Update the digest directly with the string data
  if (EVP_DigestUpdate(ctx, reinterpret_cast<const uint8_t*>(data.data()), data.length()) != 1) {
    throw std::runtime_error("Failed to update hash digest: " + std::to_string(ERR_get_error()));
  }
}

It will massively shorten the hash time to less than 100ms.

Reproducible Code

const hash = QuickCrypto.createHash('sha256');
hash.update(longText);
const hashValue = hash.copy().digest('hex')

Relevant log output

-

Device

Snapdragon 870 device

QuickCrypto Version

1.0.0-beta.13

Can you reproduce this issue in the QuickCrypto Example app?

Yes, I can reproduce the same issue in the Example app here

Additional information

Clarkkkk avatar Apr 03 '25 10:04 Clarkkkk