const NONCE_LENGTH = 12;
const TAG_LENGTH = 128;
const KEY_LENGTH = 32;
async function importKey(raw) {
  if (raw.byteLength !== KEY_LENGTH) {
    throw new Error(`Encryption key must be exactly ${KEY_LENGTH} bytes, got ${raw.byteLength}`);
  }
  return globalThis.crypto.subtle.importKey("raw", raw, "AES-GCM", false, [
    "encrypt",
    "decrypt"
  ]);
}
async function decrypt(key, data) {
  const minLength = NONCE_LENGTH + TAG_LENGTH / 8;
  if (data.byteLength < minLength) {
    throw new Error(`Encrypted data too short: expected at least ${minLength} bytes, got ${data.byteLength}`);
  }
  const nonce = data.subarray(0, NONCE_LENGTH);
  const ciphertext = data.subarray(NONCE_LENGTH);
  const plaintext = await globalThis.crypto.subtle.decrypt({ name: "AES-GCM", iv: nonce, tagLength: TAG_LENGTH }, key, ciphertext);
  return new Uint8Array(plaintext);
}
export {
  decrypt,
  importKey
};
