123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151 |
- import { Component } from "@angular/core";
- import {
- NavController,
- ToastController,
- LoadingController,
- AlertController
- } from "ionic-angular";
- import { Storage } from "@ionic/storage";
- import { CryptoProvider } from "../../providers/crypto/crypto";
- import { SocialSharing } from "@ionic-native/social-sharing";
- import * as openpgp from 'openpgp';
- @Component({
- selector: "page-settings",
- templateUrl: "settings.html"
- })
- export class SettingsPage {
- keywords: string;
- privateKey: string;
- publicKey: string;
- email: string;
- hkp = new openpgp.HKP('https://sks-keyservers.net/');
- constructor(
- public navCtrl: NavController,
- public toastCtrl: ToastController,
- private cryptoUtils: CryptoProvider,
- private storage: Storage,
- private loadingCtrl: LoadingController,
- private sharing: SocialSharing,
- private alertCtrl: AlertController
- ) {
- this.loadValuesFromStorage();
- }
- async loadValuesFromStorage() {
- this.privateKey = await this.storage.get("privateKey");
- this.publicKey = await this.storage.get("publicKey");
- this.keywords = await this.storage.get("keywords");
- this.email = await this.storage.get("email");
- }
- generateKeys() {
- if (!this.email){
- console.log("email is not provided or not valid");
- return;
- }
- else{
- this.storage.set("email", this.email);
- if (this.publicKey || this.privateKey) {
- const alert = this.alertCtrl.create({
- title: "Are you sure?",
- subTitle:
- "You already have keys entered. Do you want to overwrite them?",
- buttons: [
- {
- text: "No",
- role: "cancel"
- },
- {
- text: "Yes",
- handler: () => {
- this.startKeyGeneration();
- }
- }
- ]
- });
- alert.present();
- } else {
- this.startKeyGeneration();
- }
- }
- }
- private async startKeyGeneration() {
- const keys = await this.cryptoUtils.generatePgpKeys(this.email);
-
- this.privateKey = keys.privateKeyArmored;
- this.publicKey = keys.publicKeyArmored;
- console.log("public key", this.privateKey);
- console.log("public key", this.publicKey);
- // this.privateKey = await this.cryptoUtils.extractPrivateKey(keys);
- }
- save() {
- this.storage.set("publicKey", this.publicKey);
- this.storage.set("privateKey", this.privateKey);
- this.storage.set("keywords", this.keywords ? this.keywords.trim() : "");
- this.showToast("Successfully saved!");
- }
- async publishPublicKey() {
- await this.publishPublicKey2();
- await this.lookupKeys(this.email);
- }
-
- async publishPublicKey2() {
- const loading = this.loadingCtrl.create();
- loading.present();
- // console.log("Uploding publish public key", this.publicKey);
- if(!this.publicKey) return;
- this.hkp.upload(this.publicKey).then(function() {
- console.log("Uploding public key");
- });
- loading.dismiss();
- this.showToast("Public key has been published!");
- //lookup key to verify it has been pubblished
- // await this.lookupKeys(this.email);
- }
- public async lookupKeys(email:string){
- var options = {
- query: email
- };
- let armoredPubkey = await this.hkp.lookup(options);
- let pubkey = await openpgp.key.readArmored(armoredPubkey);
- console.log('Found public key:',pubkey);
- // if(! (email == 'rohit.hosn@gmail.com'))
- // this.pk.push(pubkey.publicKeyArmored);
- // return pubkey;
- }
- exportPrivateKey() {
- if (this.privateKey.length) {
- this.sharing
- .share(this.privateKey, null, null, null)
- .then(() => console.log("Private key was exported"))
- .catch(() =>
- this.showToast(
- "Sorry! Something went wrong trying to export the private key :("
- )
- );
- } else {
- this.showToast("There is nothing to share.");
- }
- }
- private showToast(message: string) {
- const toast = this.toastCtrl.create({
- message: message,
- position: "bottom",
- duration: 3000
- });
- toast.present();
- }
- }
|