Skip to content

Commit

Permalink
refactor(crypto): adjust and clean code
Browse files Browse the repository at this point in the history
  • Loading branch information
kalitine committed May 25, 2018
1 parent 8aeeb7e commit fae458d
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 23 deletions.
6 changes: 3 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
"@coast-team/mnemonicjs": "^1.0.0",
"bragi-browser": "0.0.12",
"core-js": "^2.5.4",
"crypto-api-wrapper": "^0.2.2-beta-1",
"crypto-api-wrapper": "^0.4.1-beta",
"hammerjs": "^2.0.8",
"jio": "git+https://github.com/Nexedi/jio.git#81cb08d",
"loglevel": "^1.6.1",
Expand Down
17 changes: 5 additions & 12 deletions src/app/core/crypto/symmetric-crypto.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,29 +3,22 @@ import { symmetricCrypto } from 'crypto-api-wrapper'

@Injectable()
export class SymmetricCryptoService {
private textDecoder: TextDecoder
private key: CryptoKey
constructor() {
this.textDecoder = new TextDecoder('utf-8')
}

async generateKey(): Promise<string> {
this.key = await symmetricCrypto.generateEncryptionKey()
const keyData = await symmetricCrypto.exportKey(this.key)
return btoa(JSON.stringify(keyData))
return symmetricCrypto.toB64(await symmetricCrypto.exportKey(this.key))
}

async importKey(keyData: string): Promise<void> {
this.key = await symmetricCrypto.importKey(JSON.parse(atob(keyData)))
async importKey(key: string): Promise<void> {
this.key = await symmetricCrypto.importKey(symmetricCrypto.fromB64(key))
}

async encrypt(msg: Uint8Array): Promise<Uint8Array> {
const ciphertext = await symmetricCrypto.encrypt(msg, this.key)
return ciphertext
return symmetricCrypto.encrypt(msg, this.key)
}

async decrypt(ciphertext: Uint8Array): Promise<Uint8Array> {
const plaintext = await symmetricCrypto.decrypt(ciphertext, this.key)
return plaintext
return symmetricCrypto.decrypt(ciphertext, this.key)
}
}
2 changes: 1 addition & 1 deletion src/app/doc/doc-resolver.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export class DocResolverService implements Resolve<Doc>, CanDeactivate<DocCompon
async resolve(route: ActivatedRouteSnapshot): Promise<Doc> {
const key = route.params['key']
const remote = route.paramMap.get('remote')
this.symCrypto.importKey(key)
await this.symCrypto.importKey(key)

try {
// Retreive the document from the local database
Expand Down
11 changes: 5 additions & 6 deletions src/app/doc/doc.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Component, Injectable, NgZone, OnDestroy, OnInit, ViewChild } from '@an
import { MediaChange, ObservableMedia } from '@angular/flex-layout'
import { ActivatedRoute } from '@angular/router'
import { MuteCore, State } from 'mute-core'
import { merge, Subject, Subscription } from 'rxjs'
import { merge, Subscription } from 'rxjs'
import { filter, flatMap, map } from 'rxjs/operators'

import { ICollaborator } from 'mute-core/dist/types/collaborators/ICollaborator'
Expand Down Expand Up @@ -83,11 +83,10 @@ export class DocComponent implements OnDestroy, OnInit {
avatar: this.settings.profile.avatar,
})
this.muteCore.collaboratorsService.messageSource = this.network.onMessage
const newOnMessage = new Subject<any>()
this.muteCore.syncMessageService.messageSource = newOnMessage.asObservable()
this.network.onMessage.pipe(filter((msg) => msg.service === 423)).subscribe((msg) => {
this.symCrypto.decrypt(msg.content).then((content) => newOnMessage.next(Object.assign({}, msg, { content })))
})
this.muteCore.syncMessageService.messageSource = this.network.onMessage.pipe(
filter(({ service }) => service === 423),
flatMap((msg) => this.symCrypto.decrypt(msg.content).then((content) => Object.assign({}, msg, { content })))
)

this.network.initSource = this.muteCore.onInit

Expand Down

0 comments on commit fae458d

Please sign in to comment.