Use SHA512 for encryption too

This commit is contained in:
Julia Friesel
2018-09-13 17:07:10 +02:00
parent f2f0278a1c
commit b82b2d625a
3 changed files with 49 additions and 25 deletions
+23 -1
View File
@@ -6,9 +6,10 @@ import {
ScrollView ScrollView
} from 'react-native' } from 'react-native'
import { LocalDate, ChronoUnit } from 'js-joda' import { LocalDate, ChronoUnit } from 'js-joda'
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 } from '../db' import { requestHash, getOrCreateCycleDay, getBleedingDaysSortedByDate, fillWithMucusDummyData, fillWithCervixDummyData, deleteAll, encryptAndRestartApp } 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 {
@@ -34,10 +35,31 @@ export default class Home extends Component {
})(this) })(this)
getBleedingDaysSortedByDate().addListener(this.setStateWithCurrentText) getBleedingDaysSortedByDate().addListener(this.setStateWithCurrentText)
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)
}
}
nodejs.start('main.js')
nodejs.channel.addListener(
'message',
this.startEncryption,
this
)
} }
componentWillUnmount() { componentWillUnmount() {
getBleedingDaysSortedByDate().removeListener(this.setStateWithCurrentText) getBleedingDaysSortedByDate().removeListener(this.setStateWithCurrentText)
nodejs.channel.removeListener('message', this.startEncryption)
} }
passTodayToDayView() { passTodayToDayView() {
+24 -17
View File
@@ -20,30 +20,37 @@ export default class PasswordPrompt extends Component {
this.setState({showApp: true}) this.setState({showApp: true})
} }
}) })
this.tryToOpenDb = 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)
}
try {
await openDbConnection(key)
} catch (err) {
console.log(err)
this.setState({ wrongPassword: true })
}
}
}
nodejs.start('main.js') nodejs.start('main.js')
nodejs.channel.addListener( nodejs.channel.addListener(
'message', 'message',
async msg => { this.tryToOpenDb,
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)
}
try {
await openDbConnection(key)
} catch(err) {
console.log(err)
this.setState({wrongPassword: true})
}
}
},
this this
) )
} }
componentWillUnmount() {
nodejs.channel.removeListener('message', this.tryToOpenDb)
}
render() { render() {
return ( return (
<View style={{ flex: 1 }}> <View style={{ flex: 1 }}>
+2 -7
View File
@@ -174,7 +174,7 @@ function requestHash(pw) {
})) }))
} }
async function encrypt(hash) { async function encryptAndRestartApp(key) {
const oldPath = db.path const oldPath = db.path
const dir = db.path.split('/') const dir = db.path.split('/')
dir.pop() dir.pop()
@@ -185,11 +185,6 @@ async function encrypt(hash) {
db.writeCopyTo(copyPath) db.writeCopyTo(copyPath)
db.close() db.close()
await fs.unlink(oldPath) await fs.unlink(oldPath)
const key = new Array(64).map((_, i) => {
let code = hash.charCodeAt(i)
if (isNaN(code)) code = 0
return code
})
realmConfig.encryptionKey = key realmConfig.encryptionKey = key
db = new Realm(realmConfig) db = new Realm(realmConfig)
await saveEncryptionFlag(true) await saveEncryptionFlag(true)
@@ -212,5 +207,5 @@ export {
tryToImportWithDelete, tryToImportWithDelete,
tryToImportWithoutDelete, tryToImportWithoutDelete,
requestHash, requestHash,
encrypt encryptAndRestartApp
} }