shogun-core 5.2.0 → 5.2.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +145 -1143
- package/dist/browser/defaultVendors-node_modules_hpke_chacha20poly1305_esm_mod_js.shogun-core.js +1220 -0
- package/dist/browser/defaultVendors-node_modules_hpke_chacha20poly1305_esm_mod_js.shogun-core.js.map +1 -0
- package/dist/browser/defaultVendors-node_modules_hpke_hybridkem-x-wing_esm_mod_js.shogun-core.js +844 -0
- package/dist/browser/defaultVendors-node_modules_hpke_hybridkem-x-wing_esm_mod_js.shogun-core.js.map +1 -0
- package/dist/browser/defaultVendors-node_modules_mlkem_esm_mod_js.shogun-core.js +2335 -0
- package/dist/browser/defaultVendors-node_modules_mlkem_esm_mod_js.shogun-core.js.map +1 -0
- package/dist/browser/defaultVendors-node_modules_noble_ciphers_chacha_js.shogun-core.js +999 -0
- package/dist/browser/defaultVendors-node_modules_noble_ciphers_chacha_js.shogun-core.js.map +1 -0
- package/dist/browser/defaultVendors-node_modules_noble_curves_esm_abstract_curve_js-node_modules_noble_curves_esm_-1ce4ed.shogun-core.js +1651 -0
- package/dist/browser/defaultVendors-node_modules_noble_curves_esm_abstract_curve_js-node_modules_noble_curves_esm_-1ce4ed.shogun-core.js.map +1 -0
- package/dist/browser/defaultVendors-node_modules_noble_curves_esm_abstract_edwards_js-node_modules_noble_curves_es-a82056.shogun-core.js +825 -0
- package/dist/browser/defaultVendors-node_modules_noble_curves_esm_abstract_edwards_js-node_modules_noble_curves_es-a82056.shogun-core.js.map +1 -0
- package/dist/browser/defaultVendors-node_modules_noble_curves_esm_ed25519_js.shogun-core.js +508 -0
- package/dist/browser/defaultVendors-node_modules_noble_curves_esm_ed25519_js.shogun-core.js.map +1 -0
- package/dist/browser/defaultVendors-node_modules_noble_curves_esm_ed448_js.shogun-core.js +747 -0
- package/dist/browser/defaultVendors-node_modules_noble_curves_esm_ed448_js.shogun-core.js.map +1 -0
- package/dist/browser/defaultVendors-node_modules_noble_curves_esm_nist_js.shogun-core.js +1608 -0
- package/dist/browser/defaultVendors-node_modules_noble_curves_esm_nist_js.shogun-core.js.map +1 -0
- package/dist/browser/defaultVendors-node_modules_noble_post-quantum_ml-dsa_js.shogun-core.js +2117 -0
- package/dist/browser/defaultVendors-node_modules_noble_post-quantum_ml-dsa_js.shogun-core.js.map +1 -0
- package/dist/browser/defaultVendors-node_modules_openpgp_dist_openpgp_min_mjs.shogun-core.js +86 -0
- package/dist/browser/defaultVendors-node_modules_openpgp_dist_openpgp_min_mjs.shogun-core.js.map +1 -0
- package/dist/browser/node_modules_hpke_ml-kem_esm_mod_js.shogun-core.js +539 -0
- package/dist/browser/node_modules_hpke_ml-kem_esm_mod_js.shogun-core.js.map +1 -0
- package/dist/browser/shogun-core.js +160386 -0
- package/dist/browser/shogun-core.js.map +1 -0
- package/dist/config/simplified-config.js +236 -0
- package/dist/core.js +329 -0
- package/dist/crypto/asymmetric.js +99 -0
- package/dist/crypto/double-ratchet.js +370 -0
- package/dist/crypto/file-encryption.js +213 -0
- package/dist/crypto/hashing.js +87 -0
- package/dist/crypto/index.js +34 -0
- package/dist/crypto/mls-codec.js +202 -0
- package/dist/crypto/mls.js +550 -0
- package/dist/crypto/pgp.js +390 -0
- package/dist/crypto/random-generation.js +341 -0
- package/dist/crypto/sframe.js +350 -0
- package/dist/crypto/signal-protocol.js +376 -0
- package/dist/crypto/symmetric.js +91 -0
- package/dist/crypto/types.js +2 -0
- package/dist/crypto/utils.js +140 -0
- package/dist/examples/auth-test.js +253 -0
- package/dist/examples/crypto-identity-example.js +151 -0
- package/dist/examples/crypto-working-test.js +83 -0
- package/dist/examples/double-ratchet-test.js +155 -0
- package/dist/examples/mls-advanced-example.js +294 -0
- package/dist/examples/mls-sframe-test.js +304 -0
- package/dist/examples/pgp-example.js +200 -0
- package/dist/examples/quick-auth-test.js +61 -0
- package/dist/examples/random-generation-test.js +151 -0
- package/dist/examples/signal-protocol-test.js +38 -0
- package/dist/examples/simple-api-test.js +114 -0
- package/dist/examples/simple-crypto-identity-example.js +84 -0
- package/dist/examples/timeout-test.js +227 -0
- package/dist/examples/zkproof-credentials-example.js +212 -0
- package/dist/examples/zkproof-example.js +201 -0
- package/dist/gundb/api.js +435 -0
- package/dist/gundb/crypto.js +283 -0
- package/dist/gundb/db.js +1946 -0
- package/dist/gundb/derive.js +232 -0
- package/dist/gundb/errors.js +76 -0
- package/dist/gundb/index.js +22 -0
- package/dist/gundb/rxjs.js +447 -0
- package/dist/gundb/types.js +5 -0
- package/dist/index.js +58 -0
- package/dist/interfaces/common.js +2 -0
- package/dist/interfaces/events.js +40 -0
- package/dist/interfaces/plugin.js +2 -0
- package/dist/interfaces/shogun.js +37 -0
- package/dist/managers/AuthManager.js +226 -0
- package/dist/managers/CoreInitializer.js +228 -0
- package/dist/managers/CryptoIdentityManager.js +366 -0
- package/dist/managers/EventManager.js +70 -0
- package/dist/managers/PluginManager.js +299 -0
- package/dist/plugins/base.js +50 -0
- package/dist/plugins/index.js +32 -0
- package/dist/plugins/nostr/index.js +20 -0
- package/dist/plugins/nostr/nostrConnector.js +419 -0
- package/dist/plugins/nostr/nostrConnectorPlugin.js +453 -0
- package/dist/plugins/nostr/nostrSigner.js +319 -0
- package/dist/plugins/nostr/types.js +2 -0
- package/dist/plugins/smartwallet/index.js +18 -0
- package/dist/plugins/smartwallet/smartWalletPlugin.js +511 -0
- package/dist/plugins/smartwallet/types.js +2 -0
- package/dist/plugins/web3/index.js +20 -0
- package/dist/plugins/web3/types.js +2 -0
- package/dist/plugins/web3/web3Connector.js +533 -0
- package/dist/plugins/web3/web3ConnectorPlugin.js +455 -0
- package/dist/plugins/web3/web3Signer.js +314 -0
- package/dist/plugins/webauthn/index.js +19 -0
- package/dist/plugins/webauthn/types.js +14 -0
- package/dist/plugins/webauthn/webauthn.js +496 -0
- package/dist/plugins/webauthn/webauthnPlugin.js +489 -0
- package/dist/plugins/webauthn/webauthnSigner.js +310 -0
- package/dist/plugins/zkproof/index.js +53 -0
- package/dist/plugins/zkproof/types.js +2 -0
- package/dist/plugins/zkproof/zkCredentials.js +213 -0
- package/dist/plugins/zkproof/zkProofConnector.js +198 -0
- package/dist/plugins/zkproof/zkProofPlugin.js +272 -0
- package/dist/storage/storage.js +145 -0
- package/dist/types/config/simplified-config.d.ts +114 -0
- package/dist/types/core.d.ts +305 -0
- package/dist/types/crypto/asymmetric.d.ts +6 -0
- package/dist/types/crypto/double-ratchet.d.ts +22 -0
- package/dist/types/crypto/file-encryption.d.ts +19 -0
- package/dist/types/crypto/hashing.d.ts +9 -0
- package/dist/types/crypto/index.d.ts +13 -0
- package/dist/types/crypto/mls-codec.d.ts +39 -0
- package/dist/types/crypto/mls.d.ts +130 -0
- package/dist/types/crypto/pgp.d.ts +95 -0
- package/dist/types/crypto/random-generation.d.ts +35 -0
- package/dist/types/crypto/sframe.d.ts +102 -0
- package/dist/types/crypto/signal-protocol.d.ts +26 -0
- package/dist/types/crypto/symmetric.d.ts +9 -0
- package/dist/types/crypto/types.d.ts +144 -0
- package/dist/types/crypto/utils.d.ts +22 -0
- package/dist/types/examples/auth-test.d.ts +8 -0
- package/dist/types/examples/crypto-identity-example.d.ts +5 -0
- package/dist/types/examples/crypto-working-test.d.ts +1 -0
- package/dist/types/examples/double-ratchet-test.d.ts +1 -0
- package/dist/types/examples/mls-advanced-example.d.ts +53 -0
- package/dist/types/examples/mls-sframe-test.d.ts +1 -0
- package/dist/types/examples/pgp-example.d.ts +75 -0
- package/dist/types/examples/quick-auth-test.d.ts +8 -0
- package/dist/types/examples/random-generation-test.d.ts +1 -0
- package/dist/types/examples/signal-protocol-test.d.ts +1 -0
- package/dist/types/examples/simple-api-test.d.ts +10 -0
- package/dist/types/examples/simple-crypto-identity-example.d.ts +6 -0
- package/dist/types/examples/timeout-test.d.ts +8 -0
- package/dist/types/examples/zkproof-credentials-example.d.ts +12 -0
- package/dist/types/examples/zkproof-example.d.ts +11 -0
- package/dist/types/gundb/api.d.ts +185 -0
- package/dist/types/gundb/crypto.d.ts +95 -0
- package/dist/types/gundb/db.d.ts +397 -0
- package/dist/types/gundb/derive.d.ts +21 -0
- package/dist/types/gundb/errors.d.ts +42 -0
- package/dist/types/gundb/index.d.ts +3 -0
- package/dist/types/gundb/rxjs.d.ts +110 -0
- package/dist/types/gundb/types.d.ts +255 -0
- package/dist/types/index.d.ts +16 -0
- package/dist/types/interfaces/common.d.ts +85 -0
- package/dist/types/interfaces/events.d.ts +131 -0
- package/dist/types/interfaces/plugin.d.ts +162 -0
- package/dist/types/interfaces/shogun.d.ts +208 -0
- package/dist/types/managers/AuthManager.d.ts +72 -0
- package/dist/types/managers/CoreInitializer.d.ts +40 -0
- package/dist/types/managers/CryptoIdentityManager.d.ts +102 -0
- package/dist/types/managers/EventManager.d.ts +49 -0
- package/dist/types/managers/PluginManager.d.ts +145 -0
- package/dist/types/plugins/base.d.ts +35 -0
- package/dist/types/plugins/index.d.ts +18 -0
- package/dist/types/plugins/nostr/index.d.ts +4 -0
- package/dist/types/plugins/nostr/nostrConnector.d.ts +119 -0
- package/dist/types/plugins/nostr/nostrConnectorPlugin.d.ts +163 -0
- package/dist/types/plugins/nostr/nostrSigner.d.ts +105 -0
- package/dist/types/plugins/nostr/types.d.ts +122 -0
- package/dist/types/plugins/smartwallet/index.d.ts +2 -0
- package/dist/types/plugins/smartwallet/smartWalletPlugin.d.ts +67 -0
- package/dist/types/plugins/smartwallet/types.d.ts +80 -0
- package/dist/types/plugins/web3/index.d.ts +4 -0
- package/dist/types/plugins/web3/types.d.ts +107 -0
- package/dist/types/plugins/web3/web3Connector.d.ts +129 -0
- package/dist/types/plugins/web3/web3ConnectorPlugin.d.ts +160 -0
- package/dist/types/plugins/web3/web3Signer.d.ts +114 -0
- package/dist/types/plugins/webauthn/index.d.ts +3 -0
- package/dist/types/plugins/webauthn/types.d.ts +183 -0
- package/dist/types/plugins/webauthn/webauthn.d.ts +129 -0
- package/dist/types/plugins/webauthn/webauthnPlugin.d.ts +179 -0
- package/dist/types/plugins/webauthn/webauthnSigner.d.ts +91 -0
- package/dist/types/plugins/zkproof/index.d.ts +48 -0
- package/dist/types/plugins/zkproof/types.d.ts +123 -0
- package/dist/types/plugins/zkproof/zkCredentials.d.ts +112 -0
- package/dist/types/plugins/zkproof/zkProofConnector.d.ts +46 -0
- package/dist/types/plugins/zkproof/zkProofPlugin.d.ts +76 -0
- package/dist/types/storage/storage.d.ts +51 -0
- package/dist/types/utils/errorHandler.d.ts +119 -0
- package/dist/types/utils/eventEmitter.d.ts +39 -0
- package/dist/types/utils/seedPhrase.d.ts +50 -0
- package/dist/types/utils/validation.d.ts +27 -0
- package/dist/utils/errorHandler.js +246 -0
- package/dist/utils/eventEmitter.js +79 -0
- package/dist/utils/seedPhrase.js +97 -0
- package/dist/utils/validation.js +81 -0
- package/package.json +10 -57
|
@@ -0,0 +1,390 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* PGP/OpenPGP Implementation
|
|
4
|
+
* Simple and immediate PGP functionality using openpgp library
|
|
5
|
+
* Provides encryption, decryption, signing, and key management
|
|
6
|
+
*/
|
|
7
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
8
|
+
exports.demonstratePGP = exports.verifyPGPSignature = exports.signPGPMessage = exports.decryptPGPMessage = exports.encryptPGPMessage = exports.generatePGPKeyPair = exports.createPGPManager = exports.PGPManager = void 0;
|
|
9
|
+
class PGPManager {
|
|
10
|
+
constructor() {
|
|
11
|
+
this.openpgp = null;
|
|
12
|
+
this.initialized = false;
|
|
13
|
+
console.log("🔐 [PGP] Manager created");
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* Initialize PGP manager with openpgp library
|
|
17
|
+
*/
|
|
18
|
+
async initialize() {
|
|
19
|
+
if (this.initialized) {
|
|
20
|
+
console.warn("[PGP] Already initialized");
|
|
21
|
+
return;
|
|
22
|
+
}
|
|
23
|
+
try {
|
|
24
|
+
console.log("🔐 [PGP] Initializing...");
|
|
25
|
+
// Dynamic import of openpgp
|
|
26
|
+
this.openpgp = await import("openpgp");
|
|
27
|
+
// Configure openpgp
|
|
28
|
+
this.openpgp.config.preferredHashAlgorithm =
|
|
29
|
+
this.openpgp.enums.hash.sha256;
|
|
30
|
+
this.openpgp.config.preferredSymmetricAlgorithm =
|
|
31
|
+
this.openpgp.enums.symmetric.aes256;
|
|
32
|
+
this.openpgp.config.preferredCompressionAlgorithm =
|
|
33
|
+
this.openpgp.enums.compression.zlib;
|
|
34
|
+
this.initialized = true;
|
|
35
|
+
console.log("✅ [PGP] Initialized successfully");
|
|
36
|
+
}
|
|
37
|
+
catch (error) {
|
|
38
|
+
console.error("❌ [PGP] Initialization failed:", error);
|
|
39
|
+
throw new Error(`PGP initialization failed: ${error instanceof Error ? error.message : String(error)}`);
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* Generate a new PGP key pair
|
|
44
|
+
*/
|
|
45
|
+
async generateKeyPair(name, email, passphrase) {
|
|
46
|
+
this.ensureInitialized();
|
|
47
|
+
try {
|
|
48
|
+
console.log(`🔑 [PGP] Generating key pair for ${name} <${email}>`);
|
|
49
|
+
const keyOptions = {
|
|
50
|
+
type: "rsa",
|
|
51
|
+
rsaBits: 4096,
|
|
52
|
+
userIDs: [{ name, email }],
|
|
53
|
+
passphrase: passphrase || undefined,
|
|
54
|
+
format: "armored",
|
|
55
|
+
};
|
|
56
|
+
const { privateKey, publicKey } = await this.openpgp.generateKey(keyOptions);
|
|
57
|
+
// Get key information
|
|
58
|
+
const privateKeyObj = await this.openpgp.readPrivateKey({
|
|
59
|
+
armoredKey: privateKey,
|
|
60
|
+
});
|
|
61
|
+
const keyId = privateKeyObj.getKeyID().toHex();
|
|
62
|
+
const fingerprint = privateKeyObj.getFingerprint();
|
|
63
|
+
const keyPair = {
|
|
64
|
+
publicKey,
|
|
65
|
+
privateKey,
|
|
66
|
+
keyId,
|
|
67
|
+
fingerprint,
|
|
68
|
+
created: new Date(),
|
|
69
|
+
};
|
|
70
|
+
console.log(`✅ [PGP] Key pair generated: ${keyId}`);
|
|
71
|
+
return keyPair;
|
|
72
|
+
}
|
|
73
|
+
catch (error) {
|
|
74
|
+
console.error("❌ [PGP] Key generation failed:", error);
|
|
75
|
+
throw new Error(`PGP key generation failed: ${error instanceof Error ? error.message : String(error)}`);
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
/**
|
|
79
|
+
* Encrypt a message with public key
|
|
80
|
+
*/
|
|
81
|
+
async encryptMessage(message, publicKeyArmored, privateKeyArmored, passphrase) {
|
|
82
|
+
this.ensureInitialized();
|
|
83
|
+
try {
|
|
84
|
+
console.log("🔒 [PGP] Encrypting message");
|
|
85
|
+
const publicKey = await this.openpgp.readKey({
|
|
86
|
+
armoredKey: publicKeyArmored,
|
|
87
|
+
});
|
|
88
|
+
const messageObj = await this.openpgp.createMessage({ text: message });
|
|
89
|
+
const encryptOptions = {
|
|
90
|
+
message: messageObj,
|
|
91
|
+
encryptionKeys: publicKey,
|
|
92
|
+
format: "armored",
|
|
93
|
+
};
|
|
94
|
+
// Add signing if private key provided
|
|
95
|
+
if (privateKeyArmored) {
|
|
96
|
+
const privateKey = await this.openpgp.readPrivateKey({
|
|
97
|
+
armoredKey: privateKeyArmored,
|
|
98
|
+
});
|
|
99
|
+
if (passphrase) {
|
|
100
|
+
// In OpenPGP v6, private keys are automatically decrypted when needed
|
|
101
|
+
// No need to call decrypt explicitly
|
|
102
|
+
}
|
|
103
|
+
encryptOptions.signingKeys = privateKey;
|
|
104
|
+
}
|
|
105
|
+
const encrypted = await this.openpgp.encrypt(encryptOptions);
|
|
106
|
+
const result = {
|
|
107
|
+
message: encrypted,
|
|
108
|
+
encrypted: true,
|
|
109
|
+
signed: !!privateKeyArmored,
|
|
110
|
+
};
|
|
111
|
+
console.log("✅ [PGP] Message encrypted");
|
|
112
|
+
return result;
|
|
113
|
+
}
|
|
114
|
+
catch (error) {
|
|
115
|
+
console.error("❌ [PGP] Encryption failed:", error);
|
|
116
|
+
throw new Error(`PGP encryption failed: ${error instanceof Error ? error.message : String(error)}`);
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
/**
|
|
120
|
+
* Decrypt a message with private key
|
|
121
|
+
*/
|
|
122
|
+
async decryptMessage(encryptedMessage, privateKeyArmored, passphrase) {
|
|
123
|
+
this.ensureInitialized();
|
|
124
|
+
try {
|
|
125
|
+
console.log("🔓 [PGP] Decrypting message");
|
|
126
|
+
const privateKey = await this.openpgp.readPrivateKey({
|
|
127
|
+
armoredKey: privateKeyArmored,
|
|
128
|
+
});
|
|
129
|
+
if (passphrase) {
|
|
130
|
+
// In OpenPGP v6, private keys are automatically decrypted when needed
|
|
131
|
+
// No need to call decrypt explicitly
|
|
132
|
+
}
|
|
133
|
+
const message = await this.openpgp.readMessage({
|
|
134
|
+
armoredMessage: encryptedMessage,
|
|
135
|
+
});
|
|
136
|
+
const { data: decrypted } = await this.openpgp.decrypt({
|
|
137
|
+
message,
|
|
138
|
+
decryptionKeys: privateKey,
|
|
139
|
+
format: "text",
|
|
140
|
+
});
|
|
141
|
+
console.log("✅ [PGP] Message decrypted");
|
|
142
|
+
return decrypted;
|
|
143
|
+
}
|
|
144
|
+
catch (error) {
|
|
145
|
+
console.error("❌ [PGP] Decryption failed:", error);
|
|
146
|
+
throw new Error(`PGP decryption failed: ${error instanceof Error ? error.message : String(error)}`);
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
/**
|
|
150
|
+
* Sign a message with private key
|
|
151
|
+
*/
|
|
152
|
+
async signMessage(message, privateKeyArmored, passphrase) {
|
|
153
|
+
this.ensureInitialized();
|
|
154
|
+
try {
|
|
155
|
+
console.log("✍️ [PGP] Signing message");
|
|
156
|
+
const privateKey = await this.openpgp.readPrivateKey({
|
|
157
|
+
armoredKey: privateKeyArmored,
|
|
158
|
+
});
|
|
159
|
+
if (passphrase) {
|
|
160
|
+
// In OpenPGP v6, private keys are automatically decrypted when needed
|
|
161
|
+
// No need to call decrypt explicitly
|
|
162
|
+
}
|
|
163
|
+
const messageObj = await this.openpgp.createMessage({ text: message });
|
|
164
|
+
const signature = await this.openpgp.sign({
|
|
165
|
+
message: messageObj,
|
|
166
|
+
signingKeys: privateKey,
|
|
167
|
+
format: "armored",
|
|
168
|
+
});
|
|
169
|
+
const result = {
|
|
170
|
+
message,
|
|
171
|
+
signature,
|
|
172
|
+
valid: true,
|
|
173
|
+
keyId: privateKey.getKeyID().toHex(),
|
|
174
|
+
};
|
|
175
|
+
console.log("✅ [PGP] Message signed");
|
|
176
|
+
return result;
|
|
177
|
+
}
|
|
178
|
+
catch (error) {
|
|
179
|
+
console.error("❌ [PGP] Signing failed:", error);
|
|
180
|
+
throw new Error(`PGP signing failed: ${error instanceof Error ? error.message : String(error)}`);
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
/**
|
|
184
|
+
* Verify a message signature
|
|
185
|
+
*/
|
|
186
|
+
async verifySignature(message, signature, publicKeyArmored) {
|
|
187
|
+
this.ensureInitialized();
|
|
188
|
+
try {
|
|
189
|
+
console.log("🔍 [PGP] Verifying signature");
|
|
190
|
+
const publicKey = await this.openpgp.readKey({
|
|
191
|
+
armoredKey: publicKeyArmored,
|
|
192
|
+
});
|
|
193
|
+
const messageObj = await this.openpgp.createMessage({ text: message });
|
|
194
|
+
const signatureObj = await this.openpgp.readSignature({
|
|
195
|
+
armoredSignature: signature,
|
|
196
|
+
});
|
|
197
|
+
const verificationResult = await this.openpgp.verify({
|
|
198
|
+
message: messageObj,
|
|
199
|
+
signature: signatureObj,
|
|
200
|
+
verificationKeys: publicKey,
|
|
201
|
+
});
|
|
202
|
+
const { verified } = verificationResult;
|
|
203
|
+
await verified;
|
|
204
|
+
const result = {
|
|
205
|
+
message,
|
|
206
|
+
signature,
|
|
207
|
+
valid: true,
|
|
208
|
+
keyId: publicKey.getKeyID().toHex(),
|
|
209
|
+
};
|
|
210
|
+
console.log("✅ [PGP] Signature verified");
|
|
211
|
+
return result;
|
|
212
|
+
}
|
|
213
|
+
catch (error) {
|
|
214
|
+
console.error("❌ [PGP] Signature verification failed:", error);
|
|
215
|
+
return {
|
|
216
|
+
message,
|
|
217
|
+
signature,
|
|
218
|
+
valid: false,
|
|
219
|
+
};
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
/**
|
|
223
|
+
* Get key information from armored key
|
|
224
|
+
*/
|
|
225
|
+
async getKeyInfo(keyArmored) {
|
|
226
|
+
this.ensureInitialized();
|
|
227
|
+
try {
|
|
228
|
+
console.log("🔍 [PGP] Getting key information");
|
|
229
|
+
const key = await this.openpgp.readKey({ armoredKey: keyArmored });
|
|
230
|
+
return {
|
|
231
|
+
keyId: key.getKeyID().toHex(),
|
|
232
|
+
fingerprint: key.getFingerprint(),
|
|
233
|
+
algorithm: key.getAlgorithmInfo(),
|
|
234
|
+
created: key.getCreationTime(),
|
|
235
|
+
expires: key.getExpirationTime(),
|
|
236
|
+
isPrivate: key.isPrivate(),
|
|
237
|
+
isPublic: key.isPublic(),
|
|
238
|
+
};
|
|
239
|
+
}
|
|
240
|
+
catch (error) {
|
|
241
|
+
console.error("❌ [PGP] Failed to get key info:", error);
|
|
242
|
+
throw new Error(`PGP key info failed: ${error instanceof Error ? error.message : String(error)}`);
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
/**
|
|
246
|
+
* Export key in different formats
|
|
247
|
+
*/
|
|
248
|
+
async exportKey(keyArmored, format = "armored") {
|
|
249
|
+
this.ensureInitialized();
|
|
250
|
+
try {
|
|
251
|
+
console.log(`📤 [PGP] Exporting key in ${format} format`);
|
|
252
|
+
const key = await this.openpgp.readKey({ armoredKey: keyArmored });
|
|
253
|
+
if (format === "binary") {
|
|
254
|
+
return key.toBytes();
|
|
255
|
+
}
|
|
256
|
+
else {
|
|
257
|
+
return key.armor();
|
|
258
|
+
}
|
|
259
|
+
}
|
|
260
|
+
catch (error) {
|
|
261
|
+
console.error("❌ [PGP] Key export failed:", error);
|
|
262
|
+
throw new Error(`PGP key export failed: ${error instanceof Error ? error.message : String(error)}`);
|
|
263
|
+
}
|
|
264
|
+
}
|
|
265
|
+
/**
|
|
266
|
+
* Import key from different formats
|
|
267
|
+
*/
|
|
268
|
+
async importKey(keyData, format = "armored") {
|
|
269
|
+
this.ensureInitialized();
|
|
270
|
+
try {
|
|
271
|
+
console.log(`📥 [PGP] Importing key in ${format} format`);
|
|
272
|
+
let key;
|
|
273
|
+
if (format === "binary") {
|
|
274
|
+
key = await this.openpgp.readKey({ binaryKey: keyData });
|
|
275
|
+
}
|
|
276
|
+
else {
|
|
277
|
+
key = await this.openpgp.readKey({ armoredKey: keyData });
|
|
278
|
+
}
|
|
279
|
+
return key.armor();
|
|
280
|
+
}
|
|
281
|
+
catch (error) {
|
|
282
|
+
console.error("❌ [PGP] Key import failed:", error);
|
|
283
|
+
throw new Error(`PGP key import failed: ${error instanceof Error ? error.message : String(error)}`);
|
|
284
|
+
}
|
|
285
|
+
}
|
|
286
|
+
/**
|
|
287
|
+
* Clean up resources
|
|
288
|
+
*/
|
|
289
|
+
destroy() {
|
|
290
|
+
this.openpgp = null;
|
|
291
|
+
this.initialized = false;
|
|
292
|
+
console.log("✅ [PGP] Manager destroyed");
|
|
293
|
+
}
|
|
294
|
+
/**
|
|
295
|
+
* Ensure the manager is initialized
|
|
296
|
+
*/
|
|
297
|
+
ensureInitialized() {
|
|
298
|
+
if (!this.initialized) {
|
|
299
|
+
throw new Error("PGP Manager not initialized. Call initialize() first.");
|
|
300
|
+
}
|
|
301
|
+
}
|
|
302
|
+
}
|
|
303
|
+
exports.PGPManager = PGPManager;
|
|
304
|
+
// Factory function for creating PGP managers
|
|
305
|
+
const createPGPManager = async () => {
|
|
306
|
+
const manager = new PGPManager();
|
|
307
|
+
await manager.initialize();
|
|
308
|
+
return manager;
|
|
309
|
+
};
|
|
310
|
+
exports.createPGPManager = createPGPManager;
|
|
311
|
+
// Utility functions for PGP
|
|
312
|
+
const generatePGPKeyPair = async (name, email, passphrase) => {
|
|
313
|
+
const manager = await (0, exports.createPGPManager)();
|
|
314
|
+
return await manager.generateKeyPair(name, email, passphrase);
|
|
315
|
+
};
|
|
316
|
+
exports.generatePGPKeyPair = generatePGPKeyPair;
|
|
317
|
+
const encryptPGPMessage = async (message, publicKey, privateKey, passphrase) => {
|
|
318
|
+
const manager = await (0, exports.createPGPManager)();
|
|
319
|
+
return await manager.encryptMessage(message, publicKey, privateKey, passphrase);
|
|
320
|
+
};
|
|
321
|
+
exports.encryptPGPMessage = encryptPGPMessage;
|
|
322
|
+
const decryptPGPMessage = async (encryptedMessage, privateKey, passphrase) => {
|
|
323
|
+
const manager = await (0, exports.createPGPManager)();
|
|
324
|
+
return await manager.decryptMessage(encryptedMessage, privateKey, passphrase);
|
|
325
|
+
};
|
|
326
|
+
exports.decryptPGPMessage = decryptPGPMessage;
|
|
327
|
+
const signPGPMessage = async (message, privateKey, passphrase) => {
|
|
328
|
+
const manager = await (0, exports.createPGPManager)();
|
|
329
|
+
return await manager.signMessage(message, privateKey, passphrase);
|
|
330
|
+
};
|
|
331
|
+
exports.signPGPMessage = signPGPMessage;
|
|
332
|
+
const verifyPGPSignature = async (message, signature, publicKey) => {
|
|
333
|
+
const manager = await (0, exports.createPGPManager)();
|
|
334
|
+
return await manager.verifySignature(message, signature, publicKey);
|
|
335
|
+
};
|
|
336
|
+
exports.verifyPGPSignature = verifyPGPSignature;
|
|
337
|
+
// Demonstrate PGP functionality
|
|
338
|
+
const demonstratePGP = async () => {
|
|
339
|
+
try {
|
|
340
|
+
console.log("🚀 Starting PGP demonstration...");
|
|
341
|
+
// Create PGP manager
|
|
342
|
+
const manager = await (0, exports.createPGPManager)();
|
|
343
|
+
console.log("✅ PGP manager created");
|
|
344
|
+
// Generate key pairs for Alice and Bob
|
|
345
|
+
const aliceKeys = await manager.generateKeyPair("Alice", "alice@example.com", "alice123");
|
|
346
|
+
const bobKeys = await manager.generateKeyPair("Bob", "bob@example.com", "bob123");
|
|
347
|
+
console.log("✅ Key pairs generated");
|
|
348
|
+
// Alice encrypts a message for Bob
|
|
349
|
+
const message = "Hello Bob! This is a secret message. 🔐";
|
|
350
|
+
const encrypted = await manager.encryptMessage(message, bobKeys.publicKey);
|
|
351
|
+
console.log("✅ Message encrypted");
|
|
352
|
+
// Bob decrypts the message
|
|
353
|
+
const decrypted = await manager.decryptMessage(encrypted.message, bobKeys.privateKey, "bob123");
|
|
354
|
+
console.log("✅ Message decrypted:", decrypted);
|
|
355
|
+
// Alice signs a message
|
|
356
|
+
const signedMessage = "This message is from Alice. ✍️";
|
|
357
|
+
const signature = await manager.signMessage(signedMessage, aliceKeys.privateKey, "alice123");
|
|
358
|
+
console.log("✅ Message signed");
|
|
359
|
+
// Bob verifies Alice's signature
|
|
360
|
+
const verification = await manager.verifySignature(signedMessage, signature.signature, aliceKeys.publicKey);
|
|
361
|
+
console.log("✅ Signature verified:", verification.valid);
|
|
362
|
+
// Get key information
|
|
363
|
+
const aliceKeyInfo = await manager.getKeyInfo(aliceKeys.publicKey);
|
|
364
|
+
const bobKeyInfo = await manager.getKeyInfo(bobKeys.publicKey);
|
|
365
|
+
console.log("✅ Key information retrieved");
|
|
366
|
+
const result = {
|
|
367
|
+
success: true,
|
|
368
|
+
messageDecrypted: decrypted === message,
|
|
369
|
+
signatureValid: verification.valid,
|
|
370
|
+
aliceKeyInfo,
|
|
371
|
+
bobKeyInfo,
|
|
372
|
+
demonstration: {
|
|
373
|
+
keyGeneration: true,
|
|
374
|
+
encryption: true,
|
|
375
|
+
decryption: true,
|
|
376
|
+
signing: true,
|
|
377
|
+
verification: true,
|
|
378
|
+
keyManagement: true,
|
|
379
|
+
},
|
|
380
|
+
};
|
|
381
|
+
console.log("✅ PGP demonstration completed successfully");
|
|
382
|
+
return result;
|
|
383
|
+
}
|
|
384
|
+
catch (error) {
|
|
385
|
+
console.error("❌ PGP demonstration failed:", error);
|
|
386
|
+
throw error;
|
|
387
|
+
}
|
|
388
|
+
};
|
|
389
|
+
exports.demonstratePGP = demonstratePGP;
|
|
390
|
+
exports.default = PGPManager;
|