Unify encrypt function

This commit is contained in:
Julia Friesel
2018-09-14 09:20:21 +02:00
parent 6c49d8a36c
commit 3dbc26f50a
5 changed files with 34 additions and 52 deletions
+3 -1
View File
@@ -17,7 +17,9 @@ export default class AppWrapper extends Component {
<App/>
:
<PasswordPrompt
onCorrectPassword={() => this.setState({showApp: true})}
showApp={() => {
this.setState({showApp: true})
}}
/>
}
</View>
+2 -11
View File
@@ -9,7 +9,7 @@ import { LocalDate, ChronoUnit } from 'js-joda'
import nodejs from 'nodejs-mobile-react-native'
import styles from '../styles/index'
import cycleModule from '../lib/cycle'
import { requestHash, getOrCreateCycleDay, getBleedingDaysSortedByDate, fillWithMucusDummyData, fillWithCervixDummyData, deleteAll, encryptAndRestartApp } from '../db'
import { requestHash, getOrCreateCycleDay, getBleedingDaysSortedByDate, fillWithMucusDummyData, fillWithCervixDummyData, deleteAll, changeEncryptionAndRestartApp } from '../db'
import {bleedingPrediction as labels} from './labels'
export default class Home extends Component {
@@ -38,18 +38,9 @@ export default class Home extends Component {
this.startEncryption = async (msg) => {
msg = JSON.parse(msg)
if (msg.type === 'sha512') {
const hash = msg.message
const key = new Uint8Array(64)
for (let i = 0; i < key.length; i++) {
const twoDigitHex = hash.slice(i * 2, i * 2 + 2)
key[i] = parseInt(twoDigitHex, 16)
}
encryptAndRestartApp(key)
}
changeEncryptionAndRestartApp(msg.message)
}
nodejs.start('main.js')
nodejs.channel.addListener(
'message',
this.startEncryption,
+2 -4
View File
@@ -19,8 +19,7 @@ export default class PasswordPrompt extends Component {
this.setState({showPasswordPrompt: true})
} else {
await openDb({persistConnection: true})
console.log(this.props)
this.props.onCorrectPassword()
this.props.showApp()
}
})
@@ -35,7 +34,6 @@ export default class PasswordPrompt extends Component {
msg = JSON.parse(msg)
if (msg.type != 'sha512') return
try {
console.log('password prompt opening db')
await openDb({hash: msg.message, persistConnection: true })
} catch (err) {
Alert.alert(
@@ -48,7 +46,7 @@ export default class PasswordPrompt extends Component {
)
return
}
this.setState({ showApp: true })
this.props.showApp()
}
componentWillUnmount() {
+3 -4
View File
@@ -12,7 +12,7 @@ import {
} from '../../local-storage'
import styles from '../../styles/index'
import { settings as labels, shared } from '../labels'
import { requestHash, openDb } from '../../db'
import { requestHash, openDb, changeEncryptionAndRestartApp } from '../../db'
export default class PasswordSetting extends Component {
constructor(props) {
@@ -40,9 +40,6 @@ export default class PasswordSetting extends Component {
if (msg.type != 'sha512') return
try {
await openDb({ hash: msg.message, persistConnection: false })
this.setState({
enteringCurrentPassword: false
})
} catch (err) {
Alert.alert(
shared.incorrectPassword,
@@ -60,7 +57,9 @@ export default class PasswordSetting extends Component {
onPress: () => this.setState({currentPassword: null})
}]
)
return
}
await changeEncryptionAndRestartApp()
}
render() {
+24 -32
View File
@@ -170,13 +170,7 @@ function requestHash(pw) {
export async function openDb ({ hash, persistConnection }) {
if (hash) {
const key = new Uint8Array(64)
for (let i = 0; i < key.length; i++) {
const twoDigitHex = hash.slice(i * 2, i * 2 + 2)
key[i] = parseInt(twoDigitHex, 16)
}
realmConfig.encryptionKey = key
realmConfig.encryptionKey = hashToInt8Array(hash)
}
const connection = await Realm.open(realmConfig)
@@ -184,37 +178,26 @@ export async function openDb ({ hash, persistConnection }) {
if (persistConnection) db = connection
}
async function encryptAndRestartApp(key) {
const oldPath = db.path
export async function changeEncryptionAndRestartApp(hash) {
let key
if (hash) key = hashToInt8Array(hash)
const defaultPath = db.path
const dir = db.path.split('/')
dir.pop()
dir.push('copied.realm')
const copyPath = dir.join('/')
const exists = await fs.exists(copyPath)
if (exists) await fs.unlink(copyPath)
db.writeCopyTo(copyPath)
// for some reason, realm complains if we give it a key with value undefined
if (key) {
db.writeCopyTo(copyPath, key)
} else {
db.writeCopyTo(copyPath)
}
db.close()
await fs.unlink(oldPath)
realmConfig.encryptionKey = key
db = new Realm(realmConfig)
await saveEncryptionFlag(true)
restart.Restart()
}
export async function removeDbEncryptionAndRestartApp(key) {
const oldPath = db.path
const dir = db.path.split('/')
dir.pop()
dir.push('copied.realm')
const copyPath = dir.join('/')
const exists = await fs.exists(copyPath)
if (exists) await fs.unlink(copyPath)
db.writeCopyTo(copyPath)
db.close()
await fs.unlink(oldPath)
realmConfig.encryptionKey = key
db = new Realm(realmConfig)
await saveEncryptionFlag(true)
await fs.unlink(defaultPath)
await fs.moveFile(copyPath, defaultPath)
await saveEncryptionFlag(key ? true : false)
restart.Restart()
}
@@ -222,6 +205,16 @@ async function deleteDbAndOpenNew() {
const exists = await fs.exists(Realm.defaultPath)
if (exists) await fs.unlink(Realm.defaultPath)
await openDb({ persistConnection: true })
await saveEncryptionFlag(false)
}
function hashToInt8Array(hash) {
const key = new Uint8Array(64)
for (let i = 0; i < key.length; i++) {
const twoDigitHex = hash.slice(i * 2, i * 2 + 2)
key[i] = parseInt(twoDigitHex, 16)
}
return key
}
export {
@@ -240,6 +233,5 @@ export {
tryToImportWithDelete,
tryToImportWithoutDelete,
requestHash,
encryptAndRestartApp,
deleteDbAndOpenNew
}