Upgrading cryptographic security in an existing application is one of the most perilous tasks in software engineering.
In FinLedger's earliest v1.0 builds, user authentication PINs were stored using single-pass SHA-256 with a local salt. While certainly better than storing passwords in plaintext, single-pass SHA-256 is fundamentally obsolete for short numeric PINs:
A modern desktop GPU can compute billions of SHA-256 hashes per second. If an attacker gains physical access to a rooted device and extracts the salt and hash from local preferences, a 4-to-6 digit numeric PIN can be brute-forced in less than 15 milliseconds.
During Hardening Sprint H8, we committed to modernizing FinLedger's authentication to the industry gold standard: PBKDF2 (Password-Based Key Derivation Function 2) with HMAC-SHA256, a 128-bit cryptographic salt, and 50,000 computational iterations.
With 50,000 iterations, brute-forcing a 6-digit PIN becomes computationally infeasible on mobile hardware.
However, we faced an immense architectural barrier:
You cannot convert an existing SHA-256 hash into a PBKDF2 hash without knowing the user's plaintext PIN.
And because we are a privacy-first app that never transmits or stores plaintext credentials, we did not have the PIN.
How do you upgrade thousands of active users to a new cryptographic standard without locking a single person out of their financial ledger?
Here is how FinLedger executed the Zero-Lockout Auto-Upgrade Migration Strategy.
The Fatal Trap of Forced Security Resets
Many applications solve cryptographic upgrades through brute administrative force:
flowchart TD subgraph HostileMigration["The Forced Reset Anti-Pattern"] AppUpdate["User Updates App to v2.0"] --> Launch["App Launches"] Launch --> ForceDialog["Intrusive Dialog: 'Security Upgrade: Your old PIN has expired. Please create a new PIN to access your data.'"] ForceDialog --> UserPanic["Immediate User Panic: - Did my data get wiped? - Was there a security breach? - What if I forget the new PIN?"] ForceDialog --> Lockout["Or Worse: User forgets backup password, gets permanently locked out of their life savings!"] end Forcing a user to create a new PIN or re-authenticate via secondary backup channels damages user trust. In a personal finance app holding years of expense records, an unexpected lockout screen feels like data loss.
A great security architecture must be seamless: it should harden security silently without interrupting the user's daily life.
The Zero-Lockout Architecture: Just-In-Time Re-Hashing
The solution lies in leveraging the one fleeting moment when the application legitimately possesses the user's plaintext PIN: the exact moment of a successful login.
In Sprint H8, we designed a dual-algorithm verification engine with automatic opportunistic upgrading (SecurityManager.kt):
flowchart TD UserEnters["User Enters PIN on Unlock Screen"] --> InspectMetadata{"Check Stored Security Metadata: What is the current hashAlgorithm?"} InspectMetadata -->|PBKDF2_50K| ModernVerify["Verify using PBKDF2 (50,000 Iterations): Constant-Time Comparison"] ModernVerify -->|Success| UnlockApp["Unlock Ledger Dashboard"] InspectMetadata -->|LEGACY_SHA256| LegacyVerify["Verify using Legacy SHA-256"] LegacyVerify -->|Failed| Reject["Show Error: 'Invalid PIN'"] LegacyVerify -->|Success!| UpgradeBlock["THE JUST-IN-TIME UPGRADE: Plaintext PIN is currently in volatile RAM!"] UpgradeBlock --> GenerateSalt["1. Generate New 128-Bit Cryptographic Salt (SecureRandom)"] GenerateSalt --> ComputePBKDF2["2. Compute PBKDF2-HMAC-SHA256 (50,000 Iterations)"] ComputePBKDF2 --> SavePrefs["3. Overwrite Encrypted Preferences: Save new hash, new salt, set algorithm = PBKDF2_50K"] SavePrefs --> ZeroRAM["4. Zero-Out Plaintext PIN Buffer in RAM"] ZeroRAM --> UnlockApp Let's trace how this workflow executes seamlessly:
Step 1: Algorithmic Tagging
In our encrypted preferences, every stored credential was updated to include a metadata version tag:
- Legacy users were marked as
hashAlgorithm = "LEGACY_SHA256". - New users creating a PIN for the first time were tagged directly as
hashAlgorithm = "PBKDF2_50K".
Step 2: Runtime Fallback Verification
When an existing user unlocks FinLedger after the update:
- The engine checks the metadata tag and detects
LEGACY_SHA256. - It hashes the entered digits using the legacy single-pass algorithm.
- If the hash does not match, it rejects the attempt normally.
Step 3: Just-In-Time Opportunistic Upgrade
If the legacy hash matches:
- The plaintext PIN is currently present in volatile memory for this single execution frame.
- Before navigating to the dashboard, the engine intercepts the execution:
- Generates a fresh 16-byte cryptographically secure random salt using
SecureRandom. - Passes the plaintext PIN, the new salt, and
iterationCount = 50,000into Android'sSecretKeyFactory.getInstance("PBKDF2WithHmacSHA256"). - Derives a 256-bit key.
- Atomically updates encrypted preferences with the new hash, the new salt, and updates the tag:
hashAlgorithm = "PBKDF2_50K". - The volatile char array containing the plaintext PIN is immediately overwritten with zeros (
Arrays.fill(chars, '0')).
- Generates a fresh 16-byte cryptographically secure random salt using
Step 4: The Result
The user notices zero disruption. They typed their PIN, the app unlocked in under 30 milliseconds, and their account was permanently upgraded to military-grade PBKDF2.
The next time they unlock the app, the engine observes hashAlgorithm = "PBKDF2_50K". The legacy SHA-256 code path is never executed for that user again.
Constant-Time Comparison (MessageDigest.isEqual)
As part of this security hardening, we eliminated side-channel timing attacks.
In naive code, developers compare hashes using standard string equality:
// The Vulnerable Timing-Leak Check if (enteredHash == storedHash) ... // Terminates early on first mismatched byte! Standard string equality returns false the moment it encounters the first non-matching character. An attacker with high-resolution hardware timers can measure execution time differences to guess characters one by one.
In FinLedger, all hash comparisons utilize MessageDigest.isEqual(a, b). It executes in constant time, comparing every byte regardless of where a mismatch occurs, eliminating side-channel timing leaks completely.
Key Takeaway
Improving security should never penalize your users.
By combining algorithmic metadata tagging with opportunistic just-in-time re-hashing, FinLedger upgraded 100% of its active users from legacy SHA-256 to 50,000-iteration PBKDF2 with zero prompts, zero forced resets, and zero lockouts.
FinLedger is a privacy-first, offline-first personal finance application for Android that models bank accounts, credit cards, and cash without double-counting.
Explore FinLedger on Google Play: https://play.google.com/store/apps/details?id=com.devanshufinance.app




