Privacy Score — Go source
One number for your privacy health: your browser fingerprint, a test password's strength and breach exposure, and a site's security headers — four checks, one score, concrete fixes. Runs in your browser; only a 5-character hash prefix ever leaves it.
This is the Go implementation — the same logic the interactive tool runs, in a shareable, citable form.
// Package privacyscore is the Go twin of CosmoDev's
// src/lib/privacy-score.ts (dual source: the web lib is TypeScript, the CLI
// lib is Go — kept in lock-step). Pure + deterministic, never panics. The
// table-driven tests in privacy_score_test.go share vectors with
// src/lib/privacy-score.test.ts so both implementations answer to one
// contract. Scoring engine only: no fingerprint collection, no HIBP call,
// no header parsing — callers bring results, same as the island does.
package privacyscore
import "math"
// HeaderGrade mirrors the web lib's union (also security-headers' grades).
type HeaderGrade byte
// FingerprintInput carries what browser-fingerprint actually produces.
type FingerprintInput struct {
SignalCount int
HighRiskCount int
MediumRiskCount int
}
type PasswordInput struct {
Score int // 0..4 (zxcvbn tiers)
Breached bool
}
// HeadersInput carries the per-header grade string, e.g. "AAAAB" — unknown
// runes are ignored, mirroring the TS `g in counts` guard.
type HeadersInput struct{ Grades string }
type BreachInput struct{ Pwned bool }
// Input mirrors PrivacyScoreInput: every category optional; run categories
// score /25 and the percent renormalizes over what ran.
type Input struct {
Fingerprint *FingerprintInput
Password *PasswordInput
Headers *HeadersInput
Breach *BreachInput
}
// CategoryID mirrors the TS literal union order.
type CategoryID string
const (
IDFingerprint CategoryID = "fingerprint"
IDPassword CategoryID = "password"
IDHeaders CategoryID = "headers"
IDBreach CategoryID = "breach"
)
var toolSlugs = map[CategoryID]string{
IDFingerprint: "browser-fingerprint",
IDPassword: "password-strength-analyser",
IDHeaders: "security-headers",
IDBreach: "breach-checker",
}
// Recommendation appears only for warn/bad categories.
type Recommendation struct {
Text string
ToolSlug string
}
// Category mirrors CategoryResult: Points/25, status bands 20/10.
type Category struct {
ID CategoryID
Label string
Points int
Max int // always 25
Status string // ok | warn | bad
Recommendation *Recommendation
ToolSlug string
}
// Result mirrors PrivacyScoreResult.
type Result struct {
Total int
Max int
Percent int
Letter string // A | B | C | D | —
Categories []Category
}
func clamp25(p float64) int {
return int(math.Max(0, math.Min(25, math.Round(p))))
}
func finalize(id CategoryID, label string, points float64, warnText, badText string) Category {
p := clamp25(points)
status := "ok"
if p < 20 {
status = "warn"
}
if p < 10 {
status = "bad"
}
var rec *Recommendation
if status != "ok" {
text := warnText
if status == "bad" {
text = badText
}
rec = &Recommendation{Text: text, ToolSlug: toolSlugs[id]}
}
return Category{ID: id, Label: label, Points: p, Max: 25, Status: status, Recommendation: rec, ToolSlug: toolSlugs[id]}
}
func fingerprintPoints(signalCount, highRisk, mediumRisk int) float64 {
surface := math.Max(0, float64(signalCount-12)) * 0.5
return 25 - float64(highRisk)*4 - float64(mediumRisk)*1.5 - surface
}
func passwordPoints(score int, breached bool) float64 {
base := math.Max(0, math.Min(4, float64(score))) / 4 * 25
if breached {
// Breached is urgent regardless of strength: ×0.32 puts score-4 at 8 → bad.
return base * 0.32
}
return base
}
func headersPoints(grades string) (float64, int) {
a, b, c, f := 0, 0, 0, 0
for _, g := range grades {
switch g {
case 'A':
a++
case 'B':
b++
case 'C':
c++
case 'F':
f++
}
}
total := a + b + c + f
switch {
case f > 0:
return 0, total
case c > 0:
return 10, total
case b > 0:
return 18, total
case total > 0:
return 25, total
default:
return 0, 0
}
}
func letterFor(percent, max int) string {
if max == 0 {
return "—"
}
switch {
case percent >= 85:
return "A"
case percent >= 70:
return "B"
case percent >= 50:
return "C"
default:
return "D"
}
}
// ScorePrivacy scores the checks that ran, in fixed order:
// fingerprint, password, headers, breach.
func ScorePrivacy(in Input) Result {
var cats []Category
if in.Fingerprint != nil {
cats = append(cats, finalize(IDFingerprint, "Browser fingerprint",
fingerprintPoints(in.Fingerprint.SignalCount, in.Fingerprint.HighRiskCount, in.Fingerprint.MediumRiskCount),
"Some signals make your browser fairly unique.",
"Your browser is highly identifiable — consider hardening."))
}
if in.Password != nil {
cats = append(cats, finalize(IDPassword, "Password strength",
passwordPoints(in.Password.Score, in.Password.Breached),
"The test password could be stronger.",
"Weak or breached — change it everywhere it was reused."))
}
if in.Headers != nil {
p, _ := headersPoints(in.Headers.Grades)
cats = append(cats, finalize(IDHeaders, "Site security headers", p,
"Some headers are weak or missing.",
"A dangerous or missing header undermines the site."))
}
if in.Breach != nil {
p := 25.0
if in.Breach.Pwned {
p = 0
}
cats = append(cats, finalize(IDBreach, "Breach exposure", p,
"The tested password appears in breaches.",
"That password is exposed in known breaches — stop using it now."))
}
total := 0
for _, c := range cats {
total += c.Points
}
max := len(cats) * 25
percent := 0
if max > 0 {
percent = int(math.Round(float64(total) / float64(max) * 100))
}
return Result{Total: total, Max: max, Percent: percent, Letter: letterFor(percent, max), Categories: cats}
}
Also available in 14 other languages
Every CosmoDev tool ships its pure logic in TypeScript (web) and Go (CLI), with authored implementations in a dozen-plus languages — the same contract, ported. Compare all languages side by side →