Where do your 2FA code keys really live, and what happens if you lose your phone? Piko2fa, a personal project written in Go, stores them in a single encrypted file on your machine and generates codes directly from the terminal, with no accounts or cloud services required.
Two-factor authentication has become part of our daily routine: almost everyone has an app on their phone spitting out six-digit codes every 30 seconds. It works well, but that convenience ties your keys to a physical device and to a third party’s decisions. Piko2fa is my response to that. I wrote it in Go and published it as open source. In this article, I’ll walk you through what it does, how it protects your keys, and who it makes sense for.
This is the fourth installment in our Go series. Previously, we looked at why Go is the way it is, built a concurrent URL checker, and learned how to read source code. Now it’s time to see Go applied to an actual project.
Why a Local 2FA Manager
The initial premise is simple: your 2FA keys should live in a file you control, not in a third party’s cloud account. Piko2fa stores them encrypted on your machine, generates codes from the command line, and allows you to migrate accounts seamlessly from Google Authenticator.
It is a personal project, so it only includes what I actually needed. It does not try to compete with full-blown password managers: I wanted to do one thing and do it transparently. If you’re interested in using it, you might need to add features that weren’t necessary for my workflow but are for yours; below, I outline what it leaves out.
I designed it for someone who:
- spends most of their day in the terminal and prefers not to pull out their phone every time they log in;
- wants to know exactly where their secrets are stored and how they are protected;
- is looking to move away from Google Authenticator without having to reconfigure every single account from scratch.
How It Protects Your Secrets
Everything revolves around a single file—the vault—encrypted with XChaCha20-Poly1305. The key that unlocks it is never stored: it is derived from your master password using Argon2id, configured with 3 iterations, 64 MiB of memory, and 4 threads. That deliberate computational cost makes brute-forcing passwords slow and expensive.
Because the encryption is authenticated, any modification to the file prevents it from opening. If someone tampers with the vault, you will know immediately instead of silently receiving corrupted data.
These are intentional design decisions:
- Nothing sensitive in command arguments. Passwords and URIs are never accepted as command-line arguments, because they would linger in your shell history and process lists.
- A strict master password. Minimum 12 characters and ASCII only: non-ASCII characters can encode differently across operating systems and keyboard layouts, which might prevent the vault from opening on another machine. Ideally, use a unique, randomly generated password from a password manager.
- Auto-lock. The interface locks after 5 minutes of inactivity, as well as upon waking the machine if that duration has passed. When locked, it zeroes out secrets in memory and prompts for the password again.
- Clean clipboard. Copied codes are wiped upon expiration, locking, or exit, unless you have copied something else in the meantime.
- Fail-safe saves. A power cut or write error should never leave the vault corrupted or unreadable.
- Strict permissions. On Linux and macOS, files are created with 600 permissions and directories with 700.
The vault is stored in the standard configuration directory for each platform:
| OS | Location |
|---|---|
| Windows | %LocalAppData%\piko2fa\ |
| Linux | ~/.config/piko2fa/ (or $XDG_CONFIG_HOME) |
| macOS | ~/Library/Application Support/piko2fa/ |
Next to vault.bin, you will find vault.bin.bak (the previous version), vault.bin.lock (to prevent race conditions between processes), and, following a restore operation, vault.bin.pre-restore-<date>, which is never deleted automatically.
Getting Started
Piko2fa compiles without CGO for Windows, Linux, and macOS, following the same standalone binary approach we explored in the URL checker. With Go installed, simply run this from the project folder:
go install ./cmd/piko2fa
The binary will be placed in the bin directory of $(go env GOPATH). Go 1.24 or later is required; I develop it with Go 1.27.2.
Two practical heads-ups:
- On Windows, use PowerShell or Windows Terminal. In Git Bash (mintty), the application cannot read passwords without echoing them to the terminal, so it halts with an error unless you invoke it via
winpty. - On Linux, you need
xcliporxselif you plan to use the--copyflag.
The core commands are few and easy to remember:
| Command | Description |
|---|---|
piko2fa init | Creates the vault. |
piko2fa add | Adds an account: otpauth:// URI via hidden prompt or stdin, or manual entry. |
piko2fa list [filter] | Displays accounts without revealing secrets or codes. |
piko2fa code <query> | Displays the current code and remaining seconds. |
piko2fa code <query> --copy | Shows the code, copies it, and clears the clipboard upon expiration. |
piko2fa code --all | Displays codes for all accounts. |
piko2fa tui | Opens the interactive interface: search, view live codes, and copy. |
piko2fa rm <query> | Deletes an account, after confirmation. |
piko2fa import-ga | Imports otpauth-migration:// URIs via hidden prompt or stdin. |
piko2fa import-ga --file uris.txt | Imports from a file containing one URI per line. |
piko2fa import-ga --qr qr\*.png | Imports from QR code images (PNG or JPEG). |
piko2fa passwd | Changes the master password. |
piko2fa export --encrypted <file> | Creates an encrypted backup. |
piko2fa export --plaintext <file> | Exports all keys in plain text as URIs. |
piko2fa restore <file> | Restores an encrypted backup or the .bak file. |
A typical workflow consists of initializing the vault, adding your accounts, and requesting the code whenever you need it (the account name below is just an example):
piko2fa init
piko2fa add
piko2fa code github --copy
If a query matches multiple accounts, Piko2fa lists them alongside their IDs, allowing you to narrow the search using Issuer:account or the ID directly.
Migrating from Google Authenticator
Starting from scratch means reconfiguring 2FA on every single service, which is enough to discourage anyone. That is why Piko2fa includes the import-ga command, which supports multiple input methods:
otpauth-migration://URIs—the export format used by Google Authenticator—via hidden input or stdin;- a file containing one URI per line, using
--file; - QR code images in PNG or JPEG format, using
--qr, in case you only have screenshots.
The import itself is a single command:
piko2fa import-ga --qr qr\*.png
One detail to keep in mind: Google Authenticator does not export the validity period, so imported accounts default to 30 seconds, which is standard practice.
However, migrating properly is a process, not just a single command. Here is the step-by-step procedure I recommend in the README:
- Prepare your machine. Verify that full-disk encryption is active (BitLocker on Windows) and disable automated photo syncing.
- Test with a throwaway account first. Add it to Google Authenticator, export only that one, and verify that
piko2fa codegenerates identical codes. - Export your accounts. This will produce multiple QR codes. If your phone blocks screenshots, photograph the screen with another device or webcam, and transfer the images over a USB cable—without cloud services. Check beforehand where your camera stores images: on Windows,
Picturesmight be synced to OneDrive. - Import. Review the preview for any missing QRs, conflicts, or rejected accounts (HOTP and MD5 are unsupported). You can repeat the import in batches without generating duplicates.
- Delete the images and empty the trash/recycle bin. They contain all your keys in plain text. On an SSD, deletion does not guarantee complete erasure, which is why full-disk encryption is critical.
- Compare codes for a few days between
piko2fa code --alland Google Authenticator. - Create an encrypted backup, store it off-device, and verify the restore procedure.
- Only then delete the accounts from Google Authenticator.
Always keep the recovery codes provided by each service stored in a safe, separate location.
The Interactive Interface
If you prefer not to type a command for every code, piko2fa tui prompts for your password once and displays all accounts alongside their live codes. Type to filter and press Enter to copy the code; if 5 seconds or fewer remain, it waits for the next cycle to ensure you have enough time to paste it. Pressing Ctrl+L locks it immediately. The interface is read-only: to add or remove accounts, you use the standard CLI commands, which continue to operate even while the TUI is open.
Backups: The Step You Cannot Skip
The price of not depending on the cloud is that responsibility falls entirely on your shoulders. If you forget your master password, there is no recovery option. If you lose the file and have no backup, your keys are gone forever.
That is why Piko2fa provides export and restore:
- Encrypted export (
export --encrypted <file>): the recommended method for creating safe backups. - Plaintext export (
export --plaintext <file>): dumps all keys as unprotected URIs. Avoid this unless you have a specific, immediate need for it. - Restore (
restore <file>): accepts an encrypted backup or the.bakfile. The vault being replaced is preserved asvault.bin.pre-restore-<date>.
Keep in mind: vault.bin.bak resides on the same drive, so it will not help you if your machine is lost or damaged. Store backups on a separate device and keep these points in mind:
- Create a backup after adding accounts. An older backup will not include newer accounts.
- Test your restore process. An untested backup is not a backup. You can test it without touching your working vault by setting
LOCALAPPDATA(orXDG_CONFIG_HOME) to a temporary folder. - Each backup keeps its original password. It unlocks with whatever password the vault had when exported: changing your vault password with
passwddoes not update existing backups. - If
vault.binbecomes corrupted, restore from the.bakfile by passing its path topiko2fa restore.
Threat Model and Limitations
I wanted to be fully transparent about what Piko2fa protects against and what it does not. I prefer you understand its boundaries before trusting it with your accounts.
What it protects against:
- Someone obtaining a copy of
vault.bin, a.bak, or a backup, provided your master password is strong. - File tampering: any single-bit alteration prevents the vault from opening, leaving no partial data accessible.
- Power outages or write errors corrupting the vault beyond repair.
- Secret leakage via shell history, process arguments, or application error messages.
What it does NOT protect against:
- Malware in your user session. A keylogger can capture your master password, and any process running under your user context can inspect memory. No local application can prevent this.
- A weak or reused password. The vault can be attacked offline.
- Forgetting your password. There is zero recovery mechanism.
- Residual data in memory or on disk. Secrets are zeroed out after use, but this is a best-effort defense: the Go runtime may duplicate buffers in memory, and that memory can be written to a system swap file.
- Secrets in memory while the TUI is open. With
piko2fa tui, keys remain decrypted until locked or exited. Lock it with Ctrl+L if you step away from your desk: in the meantime, codes remain visible on screen. - Clipboard history managers. While
--copyand the TUI clear the clipboard, tools like Win+V may retain the copied code until manually cleared. - Other user accounts on Windows. On Windows, the app does not alter filesystem ACLs: security relies on vault encryption and standard user profile separation.
Additionally, there are functional limitations to consider before migrating:
- It only supports TOTP with SHA1, SHA256, or SHA512, using 6 or 8 digits. HOTP and Steam Guard are unsupported.
- Google Authenticator does not export periods, so imported accounts default to 30 seconds.
- QR images must be PNG or JPEG: HEIC is not supported.
--copyrequiresxcliporxselon Linux.- Portions of the command help text are in English, generated by Cobra.
This is a personal project, so treat it accordingly. If you plan to trust it with critical accounts, inspect the source code and always maintain an independent recovery path, such as offline backup codes provided by each service.
Two final warnings: Keeping your passwords and your 2FA on the same machine reduces factor separation. And never store the 2FA secret for your primary password manager inside Piko2fa if that same manager holds Piko2fa’s master password: if you lose access to one, you lose access to both.
Cross-Compiling and Verifying Code
Because Piko2fa has zero CGO dependencies, cross-compiling takes just a single command per platform, exactly as we saw with the URL checker. Passing -ldflags="-s -w" strips debug information and symbol tables, resulting in smaller binaries:
# Linux (64-bit)
CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -ldflags="-s -w" -o piko2fa-linux-amd64 ./cmd/piko2fa
# Windows (64-bit)
CGO_ENABLED=0 GOOS=windows GOARCH=amd64 go build -ldflags="-s -w" -o piko2fa-windows-amd64.exe ./cmd/piko2fa
# macOS (Apple Silicon / ARM64)
CGO_ENABLED=0 GOOS=darwin GOARCH=arm64 go build -ldflags="-s -w" -o piko2fa-darwin-arm64 ./cmd/piko2fa
To maintain code quality, the project relies on four tools:
go test ./...runs the test suite across all packages (append-raceto detect race conditions).go vet ./...inspects for subtle bugs and suspicious constructs.staticcheckflags subtle bugs, dead code, and deprecated API usage.govulncheckaudits the code and its dependencies against the official Go vulnerability database.
go run honnef.co/go/tools/cmd/staticcheck@latest ./...
go run golang.org/x/vuln/cmd/govulncheck@latest ./...
What Is Piko2fa?
Piko2fa is a lightweight, transparent, local 2FA code manager written in Go. It does not try to be the ultimate password manager: it does one thing and does it well, is upfront about its limitations, and leaves you in full control of your keys. If you live in the terminal and want your codes stored in an encrypted file that belongs solely to you, give it a shot. And if you’re curious about how XChaCha20-Poly1305 and Argon2id come together in a real-world Go project, the source code is open.
In the next article, we’ll pop open Piko2fa’s hood and review its code, dissecting its architectural choices and areas for improvement, just like we did with the standard library in our guide to reading source code.
I am releasing it under the MIT license, with no warranty: I take no responsibility for bugs or lost account access. Back up your data and test it first with non-critical accounts.
Repository: github.com/jure-ve/piko2fa
Do you use a local alternative for your 2FA? Let us know in the comments.
Keep coding.