16

I have tried crypto, which obviously doesn't work because it depends on node running. So, I turned to crypto-browserify, but I believe there are dependency issues. If anyone could help, I'd be much appreciative.

For reference, I previously used crypto like so:

crypto.createHmac('md5', "MY_KEY").update('SOMETHING_TO_ENCRYPT').digest('hex');

EDIT

I am trying to encrypt a string salted with a key. The above code is what I used previously to attain this. I need to do an analogous operation in React-Native.

When trying to use crypto-browserify (which I thought would work in React-Native), I get the following in the packager:

Unable to resolve module stream from /Users/kelseyregan/BitBucket/React-Native-core/reup/node_modules/plist-parser/node_modules/sax/lib/sax.js
Unable to resolve module string_decoder from /Users/kelseyregan/BitBucket/React-Native-core/reup/node_modules/plist-parser/node_modules/sax/lib/sax.js
Unable to resolve module stream from /Users/kelseyregan/BitBucket/React-Native-core/reup/node_modules/crypto-browserify/node_modules/create-hash/node_modules/cipher-base/index.js
Unable to resolve module string_decoder from /Users/kelseyregan/BitBucket/React-Native-core/reup/node_modules/crypto-browserify/node_modules/create-hash/node_modules/cipher-base/index.js
Unable to resolve module stream from /Users/kelseyregan/BitBucket/React-Native-core/reup/node_modules/crypto-browserify/node_modules/create-hmac/browser.js
Unable to resolve module stream from /Users/kelseyregan/BitBucket/React-Native-core/reup/node_modules/crypto-browserify/node_modules/browserify-cipher/node_modules/browserify-aes/node_modules/cipher-base/index.js
Unable to resolve module string_decoder from /Users/kelseyregan/BitBucket/React-Native-core/reup/node_modules/crypto-browserify/node_modules/browserify-cipher/node_modules/browserify-aes/node_modules/cipher-base/index.js
Unable to resolve module stream from /Users/kelseyregan/BitBucket/React-Native-core/reup/node_modules/crypto-browserify/node_modules/browserify-cipher/node_modules/browserify-des/node_modules/cipher-base/index.js
Unable to resolve module string_decoder from /Users/kelseyregan/BitBucket/React-Native-core/reup/node_modules/crypto-browserify/node_modules/browserify-cipher/node_modules/browserify-des/node_modules/cipher-base/index.js
Unable to resolve module stream from /Users/kelseyregan/BitBucket/React-Native-core/reup/node_modules/crypto-browserify/node_modules/browserify-sign/browser.js
Unable to resolve module vm from /Users/kelseyregan/BitBucket/React-Native-core/reup/node_modules/crypto-browserify/node_modules/browserify-sign/node_modules/parse-asn1/node_modules/asn1.js/lib/asn1/api.js
Unable to resolve module buffer from /Users/kelseyregan/BitBucket/React-Native-core/reup/node_modules/crypto-browserify/node_modules/browserify-sign/node_modules/parse-asn1/node_modules/asn1.js/lib/asn1/base/buffer.js
Unable to resolve module buffer from /Users/kelseyregan/BitBucket/React-Native-core/reup/node_modules/crypto-browserify/node_modules/browserify-sign/node_modules/parse-asn1/node_modules/asn1.js/lib/asn1/decoders/pem.js
Unable to resolve module buffer from /Users/kelseyregan/BitBucket/React-Native-core/reup/node_modules/crypto-browserify/node_modules/browserify-sign/node_modules/parse-asn1/node_modules/asn1.js/lib/asn1/encoders/der.js
Unable to resolve module buffer from /Users/kelseyregan/BitBucket/React-Native-core/reup/node_modules/crypto-browserify/node_modules/browserify-sign/node_modules/parse-asn1/node_modules/asn1.js/lib/asn1/encoders/pem.js
Unable to resolve module stream from /Users/kelseyregan/BitBucket/React-Native-core/reup/node_modules/crypto-browserify/node_modules/browserify-sign/node_modules/parse-asn1/node_modules/browserify-aes/node_modules/cipher-base/index.js
Unable to resolve module string_decoder from /Users/kelseyregan/BitBucket/React-Native-core/reup/node_modules/crypto-browserify/node_modules/browserify-sign/node_modules/parse-asn1/node_modules/browserify-aes/node_modules/cipher-base/index.js
Unable to resolve module vm from /Users/kelseyregan/BitBucket/React-Native-core/reup/node_modules/crypto-browserify/node_modules/public-encrypt/node_modules/parse-asn1/node_modules/asn1.js/lib/asn1/api.js
Unable to resolve module buffer from /Users/kelseyregan/BitBucket/React-Native-core/reup/node_modules/crypto-browserify/node_modules/public-encrypt/node_modules/parse-asn1/node_modules/asn1.js/lib/asn1/base/buffer.js
Unable to resolve module buffer from /Users/kelseyregan/BitBucket/React-Native-core/reup/node_modules/crypto-browserify/node_modules/public-encrypt/node_modules/parse-asn1/node_modules/asn1.js/lib/asn1/decoders/pem.js
Unable to resolve module buffer from /Users/kelseyregan/BitBucket/React-Native-core/reup/node_modules/crypto-browserify/node_modules/public-encrypt/node_modules/parse-asn1/node_modules/asn1.js/lib/asn1/encoders/der.js
Unable to resolve module buffer from /Users/kelseyregan/BitBucket/React-Native-core/reup/node_modules/crypto-browserify/node_modules/public-encrypt/node_modules/parse-asn1/node_modules/asn1.js/lib/asn1/encoders/pem.js
Unable to resolve module stream from /Users/kelseyregan/BitBucket/React-Native-core/reup/node_modules/crypto-browserify/node_modules/public-encrypt/node_modules/parse-asn1/node_modules/browserify-aes/node_modules/cipher-base/index.js
Unable to resolve module string_decoder from /Users/kelseyregan/BitBucket/React-Native-core/reup/node_modules/crypto-browserify/node_modules/public-encrypt/node_modules/parse-asn1/node_modules/browserify-aes/node_modules/cipher-base/index.js
Kelsey
  • 541
  • 3
  • 11
  • I don't understand why this is getting down voted? – Kelsey Oct 23 '15 at 22:43
  • My fault, I thought I was being as explicit as I needed to be. Here is some output and clarification. – Kelsey Oct 23 '15 at 23:09
  • 1
    There seems to be an ASN.1 module in browserify, but I am wondering why it is needed at all... – Maarten Bodewes Oct 23 '15 at 23:29
  • 1
    I'm not sure. I'm surprised with all the buzz around RN that this isn't addressed further anywhere. I'm writing a native module for it now in case there isn't a solution. – Kelsey Oct 23 '15 at 23:32
  • Note that your really should not refer to the generation of an HMAC authentication tag as "encryption". Encryption is to keep data confidential, it should be reversible with the key. You cannot retrieve the org. data using the HMAC function, even if you have a key. HMAC is a one way function. In technical terms it is a PRF instead of a PRP. – Maarten Bodewes Oct 23 '15 at 23:41
  • Thanks for updating. – Kelsey Oct 23 '15 at 23:54
  • 2
    I'm using the npm package crypto-js with RN on version 0.12.0, and it works flawlessly. Maybe try that package instead? – Nader Dabit Oct 24 '15 at 15:37
  • Thanks, you the man. Want to respond so I can accept? – Kelsey Oct 24 '15 at 21:04
  • @Kelsey will you please provide steps how did you generate HMAC in react native? – bittu Feb 14 '18 at 09:21
  • @bittu install the 'crypto-js' package through npm, then use it like this: var CryptoJS = require('crypto-js'); var hash = CryptoJS.HmacMD5('THING_TO_HASH, "HASH_KEY"); hash = hash.toString(CryptoJS.enc.Hex); – Kelsey Feb 20 '18 at 20:19
  • I have used crypto-js and it work flawless on ios but it didn't work on android as expected.Do I need to do any setting for android to use cryto-js? – bittu Mar 06 '18 at 04:52

2 Answers2

6

As Nader Dabit pointed out, using the npm package crypto-js with RN on version 0.12.0 seems to do the trick.

Kelsey
  • 541
  • 3
  • 11
2

For the year of 2020, you should use crypto-js with v3.3.0 now.

Feng Liu
  • 954
  • 13
  • 22