id=prowseir-vaultguard-passwords name=VaultGuard — Encrypted Passwords + Google Auth 2FA version=1.0.0 author=roman_arco description=Zero-knowledge AES-256-GCM + PBKDF2-SHA256 (250k iterations) password vault isolated inside a closed Shadow DOM that demands a live 6-digit Google Authenticator (RFC 6238 TOTP) code (plus optional Google Account Sign-In binding) to unlock, reveal, copy, or autofill credentials. Includes 1-click login autofill, 20-char CSPRNG password generator, 120s auto-lock, and portable encrypted .pvault export/import. site_pattern=* global=1 enabled=1 ---CODE--- // VaultGuard — Guarded AES-256-GCM Password Manager + Mandatory Google Auth (TOTP + Optional Google Sign-In) // Runs inside a closed Shadow DOM so host page scripts cannot inspect inputs, secrets, or unlocked entries. // Creator: by roman a. s. arco (function () { if (location.protocol === "prowseir:" || location.protocol === "about:") return; if (window.__prowseirVaultGuardMounted) return; window.__prowseirVaultGuardMounted = true; const STORAGE_KEY = "__prowseir_vaultguard_v1__"; const PBKDF2_ITERATIONS = 250000; const AUTO_LOCK_MS = 120000; // 2 minutes inactivity auto-lock const B32_ALPHABET = "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567"; const enc = new TextEncoder(); const dec = new TextDecoder(); // Ephemeral closure-only state (never attached to window or host DOM) let sessionMaster = ""; let sessionSecretB32 = ""; let sessionMeta = null; let unlockedEntries = null; let lastVerifiedTotpStep = -1; let googleVerifiedEmail = ""; let autoLockTimer = null; let modalOpen = false; let revealedIds = new Set(); function esc(s) { return String(s || "").replace(/[&<>"']/g, (c) => ({ "&": "&", "<": "<", ">": ">", '"': """, "'": "'" }[c])); } function toB64(bytes) { let bin = ""; const u8 = bytes instanceof Uint8Array ? bytes : new Uint8Array(bytes); for (let i = 0; i < u8.length; i++) bin += String.fromCharCode(u8[i]); return btoa(bin); } function fromB64(b64) { const bin = atob(String(b64 || "").trim()); const out = new Uint8Array(bin.length); for (let i = 0; i < bin.length; i++) out[i] = bin.charCodeAt(i); return out; } function generateBase32Secret(byteLen = 20) { const raw = new Uint8Array(byteLen); crypto.getRandomValues(raw); let bits = 0; let val = 0; let out = ""; for (let i = 0; i < raw.length; i++) { val = (val << 8) | raw[i]; bits += 8; while (bits >= 5) { out += B32_ALPHABET[(val >>> (bits - 5)) & 31]; bits -= 5; } } if (bits > 0) out += B32_ALPHABET[(val << (5 - bits)) & 31]; return out; } function formatBase32Groups(b32) { return String(b32 || "").replace(/\s+/g, "").replace(/(.{4})/g, "$1 ").trim(); } function base32ToBytes(b32) { const clean = String(b32 || "").toUpperCase().replace(/[^A-Z2-7]/g, ""); let bits = 0; let val = 0; const out = []; for (let i = 0; i < clean.length; i++) { const idx = B32_ALPHABET.indexOf(clean[i]); if (idx < 0) continue; val = (val << 5) | idx; bits += 5; if (bits >= 8) { out.push((val >>> (bits - 8)) & 0xff); bits -= 8; } } return new Uint8Array(out); } // RFC 6238 Google Authenticator 6-Digit TOTP (HMAC-SHA1, 30s step) async function computeTotpAtStep(secretB32, step) { const keyBytes = base32ToBytes(secretB32); if (!keyBytes.length) return ""; const buf = new ArrayBuffer(8); const view = new DataView(buf); view.setUint32(0, Math.floor(step / 0x100000000), false); view.setUint32(4, step >>> 0, false); const key = await crypto.subtle.importKey( "raw", keyBytes, { name: "HMAC", hash: "SHA-1" }, false, ["sign"] ); const hmac = new Uint8Array(await crypto.subtle.sign("HMAC", key, buf)); const offset = hmac[hmac.length - 1] & 0x0f; const code = (((hmac[offset] & 0x7f) << 24) | ((hmac[offset + 1] & 0xff) << 16) | ((hmac[offset + 2] & 0xff) << 8) | (hmac[offset + 3] & 0xff)) % 1000000; return String(code).padStart(6, "0"); } function timingSafeEq(a, b) { const sa = String(a || ""); const sb = String(b || ""); if (sa.length !== sb.length) return false; let diff = 0; for (let i = 0; i < sa.length; i++) diff |= sa.charCodeAt(i) ^ sb.charCodeAt(i); return diff === 0; } async function verifyGoogleAuthTotp(secretB32, rawCode, allowSameWindow = false) { const code = String(rawCode || "").replace(/\s+/g, ""); if (!/^\d{6}$/.test(code)) return { ok: false, step: -1 }; const nowStep = Math.floor(Date.now() / 1000 / 30); for (const drift of [0, -1, 1]) { const candidateStep = nowStep + drift; const expected = await computeTotpAtStep(secretB32, candidateStep); if (timingSafeEq(code, expected)) { if (!allowSameWindow && candidateStep <= lastVerifiedTotpStep) { return { ok: false, replay: true, step: candidateStep }; } return { ok: true, step: candidateStep }; } } return { ok: false, step: -1 }; } async function deriveAesKey(secretMaterial, saltBytes, domainTag) { const baseKey = await crypto.subtle.importKey( "raw", enc.encode(domainTag + ":" + secretMaterial), "PBKDF2", false, ["deriveKey"] ); return crypto.subtle.deriveKey( { name: "PBKDF2", salt: saltBytes, iterations: PBKDF2_ITERATIONS, hash: "SHA-256" }, baseKey, { name: "AES-GCM", length: 256 }, false, ["encrypt", "decrypt"] ); } async function encryptJson(obj, secretMaterial, domainTag) { const salt = new Uint8Array(16); const iv = new Uint8Array(12); crypto.getRandomValues(salt); crypto.getRandomValues(iv); const key = await deriveAesKey(secretMaterial, salt, domainTag); const ct = await crypto.subtle.encrypt( { name: "AES-GCM", iv }, key, enc.encode(JSON.stringify(obj)) ); return { s: toB64(salt), iv: toB64(iv), ct: toB64(ct) }; } async function decryptJson(box, secretMaterial, domainTag) { if (!box || !box.s || !box.iv || !box.ct) throw new Error("Invalid encrypted envelope."); const key = await deriveAesKey(secretMaterial, fromB64(box.s), domainTag); const pt = await crypto.subtle.decrypt( { name: "AES-GCM", iv: fromB64(box.iv) }, key, fromB64(box.ct) ); return JSON.parse(dec.decode(pt)); } function loadStoredBlob() { try { const raw = localStorage.getItem(STORAGE_KEY); return raw ? JSON.parse(raw) : null; } catch (_) { return null; } } function saveStoredBlob(blob) { localStorage.setItem(STORAGE_KEY, JSON.stringify(blob)); } async function persistVault(masterPass, secretB32, meta, entries) { const authBox = await encryptJson( { secretB32, meta }, masterPass, "prowseir-vaultguard-auth-v1" ); const dataBox = await encryptJson( { v: 1, updatedAt: new Date().toISOString(), entries }, masterPass + "::" + secretB32, "prowseir-vaultguard-data-v1" ); const full = { format: "prowseir-pvault-v1", kdf: "PBKDF2-SHA256-250000", cipher: "AES-256-GCM", authBox, dataBox }; saveStoredBlob(full); return full; } function generateStrongPassword(len = 20) { const chars = "ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz23456789!@#$%^&*()-_=+"; const rnd = new Uint32Array(len); crypto.getRandomValues(rnd); let out = ""; for (let i = 0; i < len; i++) out += chars[rnd[i] % chars.length]; return out; } function touchActivity() { if (autoLockTimer) clearTimeout(autoLockTimer); if (unlockedEntries) { autoLockTimer = setTimeout(() => { lockVault("Vault auto-locked after 2 minutes of inactivity."); }, AUTO_LOCK_MS); } } function lockVault(msg = "Vault locked. Master password + Google Auth required.") { sessionMaster = ""; sessionSecretB32 = ""; sessionMeta = null; unlockedEntries = null; googleVerifiedEmail = ""; revealedIds.clear(); if (autoLockTimer) { clearTimeout(autoLockTimer); autoLockTimer = null; } updatePill(); if (modalOpen) renderModal(msg, false); } // Optional Google Identity Services (OAuth ID Token verification) when user binds a Google Client ID async function verifyGoogleIdToken(idToken, expectedClientId) { const resp = await fetch("https://oauth2.googleapis.com/tokeninfo?id_token=" + encodeURIComponent(idToken)); if (!resp.ok) throw new Error("Google ID token rejected by Google tokeninfo endpoint."); const info = await resp.json(); if (expectedClientId && info.aud !== expectedClientId) { throw new Error("Google Client ID mismatch (aud check failed)."); } if (info.email_verified !== "true" && info.email_verified !== true) { throw new Error("Google account email is not verified."); } return String(info.email || info.sub || "verified-google-user"); } // Closed Shadow DOM UI Host const hostEl = document.createElement("div"); hostEl.setAttribute("data-prowseir-ext", (typeof prowseirExt !== "undefined" && prowseirExt.id) ? prowseirExt.id : "prowseir-vaultguard-passwords"); const shadow = hostEl.attachShadow({ mode: "closed" }); (document.documentElement || document.body).appendChild(hostEl); const styleEl = document.createElement("style"); styleEl.textContent = ` :host { all: initial; } * { box-sizing: border-box; font-family: 'JetBrains Mono', Consolas, monospace; } .vg-pill { position: fixed; left: 14px; bottom: 14px; z-index: 2147483644; background: #090B10; color: #FFD400; border: 2px solid #FFD400; padding: 6px 11px; font-size: 11px; font-weight: 800; cursor: pointer; letter-spacing: 0.8px; opacity: 0.86; box-shadow: 0 4px 14px rgba(0,0,0,0.65); } .vg-pill.unlocked { border-color: #00E5A8; color: #00E5A8; } .vg-pill:hover { opacity: 1; background: #141924; } .vg-backdrop { position: fixed; inset: 0; z-index: 2147483645; background: rgba(5,7,12,0.84); display: none; align-items: center; justify-content: center; padding: 16px; } .vg-backdrop.open { display: flex; } .vg-box { width: 100%; max-width: 620px; max-height: 88vh; overflow-y: auto; background: #0E121B; color: #FFFFFF; border: 2px solid #FFD400; padding: 18px; box-shadow: 0 12px 38px rgba(0,0,0,0.85); font-size: 12px; line-height: 1.5; } .vg-top { display: flex; justify-content: space-between; align-items: center; gap: 8px; border-bottom: 2px solid #FFFFFF; padding-bottom: 10px; margin-bottom: 12px; } .vg-title { margin: 0; font-size: 15px; color: #FFD400; letter-spacing: 1px; } .vg-sub { color: #B6BFCC; font-size: 11px; margin-bottom: 10px; } .vg-card { background: #090B10; border: 1px solid #30384A; padding: 12px; margin-bottom: 10px; } .vg-row { display: flex; gap: 8px; flex-wrap: wrap; align-items: center; margin-bottom: 8px; } label { display: block; font-size: 10.5px; color: #B6BFCC; text-transform: uppercase; font-weight: 700; margin-bottom: 3px; } input, textarea { width: 100%; background: #141925; color: #FFFFFF; border: 1px solid #3A455C; padding: 7px 9px; font-size: 12px; font-family: inherit; } input:focus, textarea:focus { outline: none; border-color: #FFD400; } .vg-btn { background: #141925; color: #FFFFFF; border: 1px solid #FFFFFF; padding: 6px 10px; font-size: 11px; font-weight: 700; cursor: pointer; } .vg-btn:hover { background: #FFFFFF; color: #090B10; } .vg-btn.acc { background: #FFD400; color: #000000; border-color: #FFD400; } .vg-btn.ok { border-color: #00E5A8; color: #00E5A8; } .vg-btn.ok:hover { background: #00E5A8; color: #000000; } .vg-btn.warn { border-color: #FF5555; color: #FF5555; } .vg-btn.warn:hover { background: #FF5555; color: #FFFFFF; } .vg-msg { padding: 7px 10px; margin-bottom: 10px; font-size: 11.5px; font-weight: 700; border: 1px solid #00E5A8; color: #00E5A8; background: #091512; } .vg-msg.err { border-color: #FF5555; color: #FF5555; background: #190B0E; } .vg-secret { font-size: 13px; letter-spacing: 1.5px; color: #00E5A8; font-weight: 800; word-break: break-all; padding: 8px; background: #06080C; border: 1px dashed #00E5A8; margin: 6px 0; } .vg-entry { border: 1px solid #30384A; border-left: 3px solid #FFD400; padding: 10px; margin-bottom: 8px; background: #0B0E16; } .vg-entry.match { border-left-color: #00E5A8; } `; const pillBtn = document.createElement("button"); pillBtn.type = "button"; pillBtn.className = "vg-pill"; pillBtn.title = "Open VaultGuard Encrypted Password Manager (Alt+Shift+V)"; pillBtn.textContent = "VAULT [LOCKED · 2FA]"; const backdrop = document.createElement("div"); backdrop.className = "vg-backdrop"; const box = document.createElement("div"); box.className = "vg-box"; backdrop.appendChild(box); shadow.appendChild(styleEl); shadow.appendChild(pillBtn); shadow.appendChild(backdrop); function updatePill() { if (unlockedEntries) { pillBtn.className = "vg-pill unlocked"; pillBtn.textContent = `VAULT [UNLOCKED: ${unlockedEntries.length}]`; } else { pillBtn.className = "vg-pill"; pillBtn.textContent = "VAULT [LOCKED · 2FA]"; } } function autofillOnPage(username, password) { const passInputs = Array.from(document.querySelectorAll('input[type="password"]')).filter( (el) => el.offsetParent !== null || el.getClientRects().length > 0 ); if (!passInputs.length) return false; const pEl = passInputs[0]; const form = pEl.closest("form") || document.body; const candidates = Array.from( form.querySelectorAll('input[type="email"], input[type="text"], input:not([type])') ).filter((el) => el !== pEl && !el.readOnly && !el.disabled); const setVal = (el, val) => { const setter = Object.getOwnPropertyDescriptor(HTMLInputElement.prototype, "value")?.set; if (setter) setter.call(el, val); else el.value = val; el.dispatchEvent(new Event("input", { bubbles: true })); el.dispatchEvent(new Event("change", { bubbles: true })); }; if (candidates.length && username) setVal(candidates[candidates.length - 1], username); setVal(pEl, password); return true; } let pendingSetupSecret = ""; function renderModal(statusText = "", isError = false) { touchActivity(); const stored = loadStoredBlob(); const banner = statusText ? `
${esc(location.hostname || "local")}. Both your Master Passphrase and a live Google Authenticator 6-digit code are required to decrypt.
${shown ? esc(item.password) : "••••••••••••••••"}
${item.notes ? ` · ${esc(item.notes)}` : ""}