{"action":"created","issue":{"url":"https://api.github.com/repos/realm/realm-js/issues/444","repository_url":"https://api.github.com/repos/realm/realm-js","labels_url":"https://api.github.com/repos/realm/realm-js/issues/444/labels{/name}","comments_url":"https://api.github.com/repos/realm/realm-js/issues/444/comments","events_url":"https://api.github.com/repos/realm/realm-js/issues/444/events","html_url":"https://github.com/realm/realm-js/issues/444","id":155377797,"number":444,"title":"React Native - Realm Encryption - Unable to encrypt and then access DB","user":{"login":"celrak","id":3343356,"avatar_url":"https://avatars.githubusercontent.com/u/3343356?v=3","gravatar_id":"","url":"https://api.github.com/users/celrak","html_url":"https://github.com/celrak","followers_url":"https://api.github.com/users/celrak/followers","following_url":"https://api.github.com/users/celrak/following{/other_user}","gists_url":"https://api.github.com/users/celrak/gists{/gist_id}","starred_url":"https://api.github.com/users/celrak/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/celrak/subscriptions","organizations_url":"https://api.github.com/users/celrak/orgs","repos_url":"https://api.github.com/users/celrak/repos","events_url":"https://api.github.com/users/celrak/events{/privacy}","received_events_url":"https://api.github.com/users/celrak/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":1,"created_at":"2016-05-17T22:31:13Z","updated_at":"2016-05-19T04:04:31Z","closed_at":null,"body":"Hey guys,\r\n\r\nFirst off, Realm is awesome! We are stoked on the React Native release and will be using it in production soon. We ran into one issue, we are having trouble using the Encryption feature as described in the docs.\r\n\r\nThe example provided here (https://realm.io/docs/react-native/latest/#encryption) seems incomplete(?) and the sample code here (https://github.com/realm/realm-js) doesn't seem to have any encryption examples. Without storing the encryption key, how can you open the database again exactly? In any case the example doesn't seem to be working, is there a more robust code sample to reference?\r\n\r\nFor storing the encryption key we went the route of using the react-native-keychain module to store the encryption key/other data. The module itself seems to work great, but when keychain to try to access the Realm DB we have had no luck and see 'Unable to access / open Realm etc etc\" red error screen. (seeing errors like this with or without using react-native-keychain)\r\n\r\nThanks much,\r\nCharles"},"comment":{"url":"https://api.github.com/repos/realm/realm-js/issues/comments/220222649","html_url":"https://github.com/realm/realm-js/issues/444#issuecomment-220222649","issue_url":"https://api.github.com/repos/realm/realm-js/issues/444","id":220222649,"user":{"login":"appden","id":70904,"avatar_url":"https://avatars.githubusercontent.com/u/70904?v=3","gravatar_id":"","url":"https://api.github.com/users/appden","html_url":"https://github.com/appden","followers_url":"https://api.github.com/users/appden/followers","following_url":"https://api.github.com/users/appden/following{/other_user}","gists_url":"https://api.github.com/users/appden/gists{/gist_id}","starred_url":"https://api.github.com/users/appden/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/appden/subscriptions","organizations_url":"https://api.github.com/users/appden/orgs","repos_url":"https://api.github.com/users/appden/repos","events_url":"https://api.github.com/users/appden/events{/privacy}","received_events_url":"https://api.github.com/users/appden/received_events","type":"User","site_admin":false},"created_at":"2016-05-19T04:04:31Z","updated_at":"2016-05-19T04:04:31Z","body":"The `react-native-keychain` only supports password strings, whereas Realm expects a 512-bit (64-bytes) data blob as either an `ArrayBuffer` or `ArrayBufferView` (e.g. `Uint8Array`, et al).\r\n\r\nIf you're randomly generating a password, then I would suggest base64-encoding that data into a string before storing with `react-native-keychain`, then base64 decoding it when you get it out. I'd recommend checking out [base64-js](https://www.npmjs.com/package/base64-js) for dealing with raw data.\r\n\r\nOn the other hand, if the user is providing a password, then you can store that directly in the keychain and use a 512-bit hash of that password as the encryption key. A SHA-512 hash would accomplish that, but typically with encryption you want to use a hashing algorithm that includes a salt and is fundamentally slow, thus making it much harder to brute force. The standard for doing this is [PBKDF2](https://en.wikipedia.org/wiki/PBKDF2), though newer and better hashing methods exist. I'd recommend checking out [crypto-js](https://www.npmjs.com/package/crypto-js) or [pbkdf2-js](https://www.npmjs.com/package/pbkdf2-js) for this."}}