Unify encrypt function
This commit is contained in:
@@ -17,7 +17,9 @@ export default class AppWrapper extends Component {
|
|||||||
<App/>
|
<App/>
|
||||||
:
|
:
|
||||||
<PasswordPrompt
|
<PasswordPrompt
|
||||||
onCorrectPassword={() => this.setState({showApp: true})}
|
showApp={() => {
|
||||||
|
this.setState({showApp: true})
|
||||||
|
}}
|
||||||
/>
|
/>
|
||||||
}
|
}
|
||||||
</View>
|
</View>
|
||||||
|
|||||||
+2
-11
@@ -9,7 +9,7 @@ import { LocalDate, ChronoUnit } from 'js-joda'
|
|||||||
import nodejs from 'nodejs-mobile-react-native'
|
import nodejs from 'nodejs-mobile-react-native'
|
||||||
import styles from '../styles/index'
|
import styles from '../styles/index'
|
||||||
import cycleModule from '../lib/cycle'
|
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'
|
import {bleedingPrediction as labels} from './labels'
|
||||||
|
|
||||||
export default class Home extends Component {
|
export default class Home extends Component {
|
||||||
@@ -38,18 +38,9 @@ export default class Home extends Component {
|
|||||||
|
|
||||||
this.startEncryption = async (msg) => {
|
this.startEncryption = async (msg) => {
|
||||||
msg = JSON.parse(msg)
|
msg = JSON.parse(msg)
|
||||||
if (msg.type === 'sha512') {
|
changeEncryptionAndRestartApp(msg.message)
|
||||||
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)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
nodejs.start('main.js')
|
|
||||||
nodejs.channel.addListener(
|
nodejs.channel.addListener(
|
||||||
'message',
|
'message',
|
||||||
this.startEncryption,
|
this.startEncryption,
|
||||||
|
|||||||
@@ -19,8 +19,7 @@ export default class PasswordPrompt extends Component {
|
|||||||
this.setState({showPasswordPrompt: true})
|
this.setState({showPasswordPrompt: true})
|
||||||
} else {
|
} else {
|
||||||
await openDb({persistConnection: true})
|
await openDb({persistConnection: true})
|
||||||
console.log(this.props)
|
this.props.showApp()
|
||||||
this.props.onCorrectPassword()
|
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
@@ -35,7 +34,6 @@ export default class PasswordPrompt extends Component {
|
|||||||
msg = JSON.parse(msg)
|
msg = JSON.parse(msg)
|
||||||
if (msg.type != 'sha512') return
|
if (msg.type != 'sha512') return
|
||||||
try {
|
try {
|
||||||
console.log('password prompt opening db')
|
|
||||||
await openDb({hash: msg.message, persistConnection: true })
|
await openDb({hash: msg.message, persistConnection: true })
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
Alert.alert(
|
Alert.alert(
|
||||||
@@ -48,7 +46,7 @@ export default class PasswordPrompt extends Component {
|
|||||||
)
|
)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
this.setState({ showApp: true })
|
this.props.showApp()
|
||||||
}
|
}
|
||||||
|
|
||||||
componentWillUnmount() {
|
componentWillUnmount() {
|
||||||
|
|||||||
@@ -12,7 +12,7 @@ import {
|
|||||||
} from '../../local-storage'
|
} from '../../local-storage'
|
||||||
import styles from '../../styles/index'
|
import styles from '../../styles/index'
|
||||||
import { settings as labels, shared } from '../labels'
|
import { settings as labels, shared } from '../labels'
|
||||||
import { requestHash, openDb } from '../../db'
|
import { requestHash, openDb, changeEncryptionAndRestartApp } from '../../db'
|
||||||
|
|
||||||
export default class PasswordSetting extends Component {
|
export default class PasswordSetting extends Component {
|
||||||
constructor(props) {
|
constructor(props) {
|
||||||
@@ -40,9 +40,6 @@ export default class PasswordSetting extends Component {
|
|||||||
if (msg.type != 'sha512') return
|
if (msg.type != 'sha512') return
|
||||||
try {
|
try {
|
||||||
await openDb({ hash: msg.message, persistConnection: false })
|
await openDb({ hash: msg.message, persistConnection: false })
|
||||||
this.setState({
|
|
||||||
enteringCurrentPassword: false
|
|
||||||
})
|
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
Alert.alert(
|
Alert.alert(
|
||||||
shared.incorrectPassword,
|
shared.incorrectPassword,
|
||||||
@@ -60,7 +57,9 @@ export default class PasswordSetting extends Component {
|
|||||||
onPress: () => this.setState({currentPassword: null})
|
onPress: () => this.setState({currentPassword: null})
|
||||||
}]
|
}]
|
||||||
)
|
)
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
await changeEncryptionAndRestartApp()
|
||||||
}
|
}
|
||||||
|
|
||||||
render() {
|
render() {
|
||||||
|
|||||||
+24
-32
@@ -170,13 +170,7 @@ function requestHash(pw) {
|
|||||||
|
|
||||||
export async function openDb ({ hash, persistConnection }) {
|
export async function openDb ({ hash, persistConnection }) {
|
||||||
if (hash) {
|
if (hash) {
|
||||||
const key = new Uint8Array(64)
|
realmConfig.encryptionKey = hashToInt8Array(hash)
|
||||||
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
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const connection = await Realm.open(realmConfig)
|
const connection = await Realm.open(realmConfig)
|
||||||
@@ -184,37 +178,26 @@ export async function openDb ({ hash, persistConnection }) {
|
|||||||
if (persistConnection) db = connection
|
if (persistConnection) db = connection
|
||||||
}
|
}
|
||||||
|
|
||||||
async function encryptAndRestartApp(key) {
|
export async function changeEncryptionAndRestartApp(hash) {
|
||||||
const oldPath = db.path
|
let key
|
||||||
|
if (hash) key = hashToInt8Array(hash)
|
||||||
|
const defaultPath = db.path
|
||||||
const dir = db.path.split('/')
|
const dir = db.path.split('/')
|
||||||
dir.pop()
|
dir.pop()
|
||||||
dir.push('copied.realm')
|
dir.push('copied.realm')
|
||||||
const copyPath = dir.join('/')
|
const copyPath = dir.join('/')
|
||||||
const exists = await fs.exists(copyPath)
|
const exists = await fs.exists(copyPath)
|
||||||
if (exists) await fs.unlink(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()
|
db.close()
|
||||||
await fs.unlink(oldPath)
|
await fs.unlink(defaultPath)
|
||||||
realmConfig.encryptionKey = key
|
await fs.moveFile(copyPath, defaultPath)
|
||||||
db = new Realm(realmConfig)
|
await saveEncryptionFlag(key ? true : false)
|
||||||
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)
|
|
||||||
restart.Restart()
|
restart.Restart()
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -222,6 +205,16 @@ async function deleteDbAndOpenNew() {
|
|||||||
const exists = await fs.exists(Realm.defaultPath)
|
const exists = await fs.exists(Realm.defaultPath)
|
||||||
if (exists) await fs.unlink(Realm.defaultPath)
|
if (exists) await fs.unlink(Realm.defaultPath)
|
||||||
await openDb({ persistConnection: true })
|
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 {
|
export {
|
||||||
@@ -240,6 +233,5 @@ export {
|
|||||||
tryToImportWithDelete,
|
tryToImportWithDelete,
|
||||||
tryToImportWithoutDelete,
|
tryToImportWithoutDelete,
|
||||||
requestHash,
|
requestHash,
|
||||||
encryptAndRestartApp,
|
|
||||||
deleteDbAndOpenNew
|
deleteDbAndOpenNew
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user