pgp-key-server.ts 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. import { Injectable } from "@angular/core";
  2. import { Storage } from "@ionic/storage";
  3. import * as openpgp from 'openpgp';
  4. @Injectable()
  5. export class PgpKeyServerProvider {
  6. hkp = new openpgp.HKP('https://sks-keyservers.net/');
  7. pk: any[] = [];
  8. passphrase = "passphrase";
  9. constructor(private storage: Storage) {}
  10. public async generateKey(passphrase, email) {
  11. let options = {
  12. userIds: [{ email: email }], // multiple user IDs
  13. curve: "ed25519",// ECC curve name
  14. passphrase: this.passphrase
  15. };
  16. // passphrase: this.passphrase // protects the private key
  17. let a = await openpgp.generateKey(options);
  18. return a;
  19. }
  20. public async publishPubKey(pubkey) {
  21. this.hkp.upload(pubkey).then(function(result) {
  22. });
  23. }
  24. public async lookupKeys(email: string) {
  25. let pubkey;
  26. let myEmail = await this.storage.get("email");
  27. if(email === myEmail){
  28. pubkey= await this.storage.get("publicKey");
  29. let ampubkey = (await openpgp.key.readArmored(pubkey)).keys[0];
  30. this.pk.push(ampubkey);
  31. }
  32. else{
  33. //lookup followers pubkey on server
  34. var options = {
  35. query: email
  36. };
  37. try {
  38. let armoredPubkey = await this.hkp.lookup(options);
  39. pubkey = (await openpgp.key.readArmored(armoredPubkey)).keys[0];
  40. this.pk.push(pubkey);
  41. return pubkey;
  42. } catch (err) {
  43. console.log("Error: key not found");
  44. }
  45. }
  46. }
  47. /**
  48. * Encrypt text with RSA
  49. * @param plainText plain text
  50. * @param privateKey private key
  51. */
  52. public async encrypt(plainText) {
  53. if (!this.pk) { console.log("this.pk is empty"); return; }
  54. console.log("this.pk",this.pk);
  55. const options = {
  56. message: openpgp.message.fromText(plainText), // input as Message object
  57. publicKeys: await Promise.all(this.pk), // for encryption
  58. }
  59. const ciphertext = await openpgp.encrypt(options);
  60. return ciphertext.data;
  61. }
  62. public async decrypt(encrypted: string, privKeyObj) {
  63. const options2 = {
  64. message: await openpgp.message.readArmored(encrypted), // parse armored message
  65. privateKeys: [privKeyObj] // for decryption
  66. }
  67. try {
  68. let plaintext = await openpgp.decrypt(options2);
  69. return plaintext.data
  70. } catch (err) {
  71. console.log('Error thrown:', err);
  72. }
  73. return null;
  74. }
  75. public async revokeKey() {
  76. //using revocation certificate
  77. let pubkey = await this.storage.get("publicKey");
  78. let atest = (await openpgp.key.readArmored(pubkey)).keys[0];
  79. let revocatnCert = this.storage.get("revocationCert");
  80. try {
  81. var options = {
  82. key: atest,
  83. revocationCertificate: revocatnCert
  84. };
  85. openpgp.revokeKey(options).then(function(key) {
  86. console.log("public key revoked", key);
  87. });
  88. } catch (e) {
  89. console.log('revoke failed', e);
  90. }
  91. }
  92. async getArmoredPrivateKey(key:string){
  93. const privKeyObj = (await openpgp.key.readArmored(key)).keys[0];
  94. if(privKeyObj){
  95. await privKeyObj.decrypt(this.passphrase);
  96. return privKeyObj;
  97. }
  98. else
  99. return key;
  100. }
  101. }