05/18 Enhanced codes and functionalities 4
This commit is contained in:
@@ -151,6 +151,38 @@ const SharingCrypto = (() => {
|
||||
return JSON.parse(new TextDecoder().decode(plaintext));
|
||||
}
|
||||
|
||||
/**
|
||||
* Encrypt a display name string with the ECDH shared key.
|
||||
* Uses the same AES-256-GCM primitive as encryptForShare but operates on
|
||||
* a plain string rather than a JSON-serialised object.
|
||||
*/
|
||||
async function encryptName(sharedKey, name) {
|
||||
const iv = window.crypto.getRandomValues(new Uint8Array(12));
|
||||
const plaintext = new TextEncoder().encode(name);
|
||||
const ciphertext = await subtle.encrypt({ name: 'AES-GCM', iv }, sharedKey, plaintext);
|
||||
return {
|
||||
enc_name: bytesToBase64(new Uint8Array(ciphertext)),
|
||||
iv_name: bytesToBase64(iv),
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Decrypt an encrypted display name.
|
||||
* Returns null on failure (e.g. legacy share with no enc_name).
|
||||
*/
|
||||
async function decryptName(sharedKey, enc_name, iv_name) {
|
||||
try {
|
||||
const plaintext = await subtle.decrypt(
|
||||
{ name: 'AES-GCM', iv: base64ToBytes(iv_name) },
|
||||
sharedKey,
|
||||
base64ToBytes(enc_name),
|
||||
);
|
||||
return new TextDecoder().decode(plaintext);
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
// ── Public API ────────────────────────────────────────────────────────────
|
||||
|
||||
return {
|
||||
@@ -162,6 +194,8 @@ const SharingCrypto = (() => {
|
||||
deriveSharedKey,
|
||||
encryptForShare,
|
||||
decryptShare,
|
||||
encryptName,
|
||||
decryptName,
|
||||
};
|
||||
})();
|
||||
|
||||
|
||||
Reference in New Issue
Block a user