Files
objects/wco/crypto/crypto.js
2025-08-11 09:26:06 +02:00

110 lines
2.3 KiB
JavaScript

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",
}
);
};
apx.crypto.encryptMessage = async (message, publicKey) => {
publicKey = await openpgp.readKey(
{
armoredKey: 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
}
);
const message = await openpgp.readMessage(
{
armoredMessage: encryptedMessage
}
);
return await openpgp.decrypt(
{
message,
decryptionKeys: privateKey
}
);
};
apx.crypto.sign = async (message, privateKey) => {
privateKey = await openpgp.readPrivateKey(
{
armoredKey: privateKey
}
);
return await openpgp.sign(
{
message: await openpgp.createMessage(
{
text: message
}
),
signingKeys: privateKey
}
);
};
apx.crypto.verifySignature = async (message, signature, publicKey) => {
publicKey = await openpgp.readKey(
{
armoredKey: publicKey
}
);
const verified = await openpgp.verify(
{
message: await openpgp.createMessage(
{
text: message
}
),
signature: await openpgp.readSignature(
{
armoredSignature: signature
}
),
verificationKeys: publicKey
}
);
if (await verified.signatures[0].verified) {
return true;
} else {
return false;
};
};
export default apx;