pgp-key-server.ts 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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 // protects the private key
  15. };
  16. let a = await openpgp.generateKey(options);
  17. console.log('the key generated is:', a);
  18. return a;
  19. }
  20. public async publishPubKey(pubkey) {
  21. console.log('passing pubkey to uplaoded : ', pubkey);
  22. this.hkp.upload(pubkey).then(function() {
  23. console.log('public key successfully uploaded');
  24. });
  25. }
  26. public async lookupKeys(email: string) {
  27. console.log('looking up keys for',email);
  28. var options = {
  29. query: email
  30. };
  31. let armoredPubkey = await this.hkp.lookup(options);
  32. let pubkey = (await openpgp.key.readArmored(armoredPubkey)).keys[0];
  33. console.log('Found latest public key:', pubkey);
  34. this.pk.push(pubkey);
  35. console.log('pk is:', this.pk);
  36. }
  37. /**
  38. * Encrypt text with RSA
  39. * @param plainText plain text
  40. * @param privateKey private key
  41. */
  42. public async encrypt(plainText: string) {
  43. // this.lookupKeys('rohit.shiva.gowda@gmail.com');
  44. const options = {
  45. message: openpgp.message.fromText(plainText), // input as Message object
  46. publicKeys: await Promise.all(this.pk), // for encryption
  47. // privateKey s: [privKeyObj] // for signing (optional)
  48. }
  49. console.log('options are:', options);
  50. const ciphertext = await openpgp.encrypt(options);
  51. console.log('encrypted text is:', ciphertext);
  52. return ciphertext.data;
  53. }
  54. public async decrypt(encrypted: string,a) {
  55. const privKeyObj = (await openpgp.key.readArmored(a)).keys[0];
  56. console.log('privKeyObj', privKeyObj);
  57. await privKeyObj.decrypt(this.passphrase);
  58. console.log('a is:',a);
  59. const options2 = {
  60. message: await openpgp.message.readArmored(encrypted), // parse armored message
  61. privateKeys: [privKeyObj] // for decryption
  62. }
  63. console.log('options2 is: ', options2);
  64. let plaintext = await openpgp.decrypt(options2);
  65. console.log('decrypted text is:', plaintext, plaintext.data);
  66. return plaintext.data // 'Hello, World!'
  67. }
  68. public async revokeKey() {
  69. //using revocation certificate
  70. let pubkey = await this.storage.get("publicKey");
  71. let atest = (await openpgp.key.readArmored(pubkey)).keys[0];
  72. console.log('inside revoke key pubkey is:', atest);
  73. let revocatnCert = this.storage.get("revocationCert");
  74. try {
  75. var options = {
  76. key: atest,
  77. revocationCertificate: revocatnCert
  78. };
  79. openpgp.revokeKey(options).then(function(key) {
  80. console.log("public key revoked", key);
  81. });
  82. } catch (e) {
  83. console.log('revoke failed1', e);
  84. }
  85. }
  86. }