package keygen import "crypto/subtle" // Reconstructed from legacy/keygen.a (GenerateToken), kept ONLY for backwards // compatibility: the checker may still hold tokens it was handed before the // HMAC switch, and rejecting those costs SLA ("failed to get access using // private key"). // // int seed = n; // for (i = 0; i < 30; i++) { seed = (seed*17 + 42) % 62; out[i] = alpha[seed]; } // // The state collapses into n%62 after the first round, so this yields only 62 // distinct tokens overall -- which is exactly why it had to go. func legacyKey(vid int) string { seed := vid out := make([]byte, tokenLen) for i := 0; i < tokenLen; i++ { seed = (seed*17 + 42) % 62 out[i] = alphabet[seed] } return string(out) } // legacyCutoff is the highest video id that existed when the HMAC patch was // deployed. Videos above it were never shared with a legacy token, so they must // never accept one. 0 disables legacy acceptance entirely. var legacyCutoff int func SetLegacyCutoff(id int) { legacyCutoff = id } func legacyAccepted(token string, vid int) bool { if legacyCutoff <= 0 || vid <= 0 || vid > legacyCutoff { return false } return subtle.ConstantTimeCompare([]byte(token), []byte(legacyKey(vid))) == 1 }