Privatri works locally.

This commit is contained in:
devpotatoes
2025-08-28 09:19:28 +02:00
parent 93842f84a7
commit d218c3b1d0
3 changed files with 134 additions and 96 deletions

View File

@@ -3,72 +3,74 @@ var apx = apx || {};
apx.crypto = apx.crypto || {};
apx.crypto.genKey = async (uuid) => {
return await openpgp.generateKey({
type: "ecc",
curve: "curve25519",
userIDs: [
{
alias: uuid,
},
],
passphrase: "",
format: "armored",
});
return await openpgp.generateKey({
type: "ecc",
curve: "curve25519",
userIDs: [
{
alias: uuid,
},
],
passphrase: "",
format: "armored",
});
};
apx.crypto.encryptMessage = async (message, publicKey) => {
publicKey = await openpgp.readKey({
armoredKey: publicKey,
});
publicKey = await openpgp.readKey({
armoredKey: publicKey,
});
return await openpgp.encrypt({
message: await openpgp.createMessage({
text: message,
}),
encryptionKeys: publicKey,
});
return await openpgp.encrypt({
message: await openpgp.createMessage({
text: message,
}),
encryptionKeys: publicKey,
});
};
apx.crypto.decryptMessage = async (encryptedMessage, privateKey) => {
privateKey = await openpgp.readPrivateKey({
armoredKey: privateKey,
});
privateKey = await openpgp.readPrivateKey({
armoredKey: privateKey,
});
const message = await openpgp.readMessage({
armoredMessage: encryptedMessage,
});
const message = await openpgp.readMessage({
armoredMessage: encryptedMessage,
});
return await openpgp.decrypt({
message,
decryptionKeys: privateKey,
});
return await openpgp.decrypt({
message,
decryptionKeys: privateKey,
});
};
apx.crypto.isSignedby = async (
alias,
publicKey,
detachedSignature,
message
alias,
publicKey,
detachedSignature,
message
) => {
const publickey = await openpgp.readKey({ armoredKey: publicKey });
const msg = await openpgp.createMessage({ text: message });
const signature = await openpgp.readSignature({
armoredSignature: atob(detachedSignature), // parse detached signature
});
const verificationResult = await openpgp.verify({
msg, // Message object
signature,
verificationKeys: publickey,
});
const { verified, keyID } = verificationResult.signatures[0];
try {
await verified; // throws on invalid signature
//console.log("Signed by key id " + keyID.toHex());
return KeyId.toHex().alias == alias;
} catch (e) {
console.log("Signature could not be verified: " + e.message);
return false;
}
const publickey = await openpgp.readKey({ armoredKey: publicKey });
const msg = await openpgp.createMessage({ text: message });
const signature = await openpgp.readSignature({
armoredSignature: atob(detachedSignature), // parse detached signature
});
const verificationResult = await openpgp.verify({
msg, // Message object
signature,
verificationKeys: publickey,
});
const { verified, keyID } = verificationResult.signatures[0];
try {
await verified; // throws on invalid signature
//console.log("Signed by key id " + keyID.toHex());
return KeyId.toHex().alias == alias;
} catch (e) {
console.log("Signature could not be verified: " + e.message);
return false;
}
};
apx.crypto.sign = async (message, privateKey) => {
privateKey = await openpgp.readPrivateKey(
{
@@ -118,4 +120,19 @@ apx.crypto.verifySignature = async (message, signature, publicKey) => {
};
};
export default apx;
apx.crypto.genUUID = () => {
const uuidTemplate = "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx";
return uuidTemplate.replace(/[xy]/g, (char) => {
const random = Math.random() * 16 | 0;
let value;
if (char === "x") {
value = random;
} else {
value = (random & 0x3) | 0x8;
};
return value.toString(16);
});
};