@@ -312,19 +312,20 @@ func (k *KeyServiceClient) MakeRequest(method string, path string, body interfac
312312 return nil
313313}
314314
315- // NormalizeVerificationCode applies canonical cleanup rules to a verification
316- // code to tolerate common user input mistakes before comparison or storage.
317- func NormalizeVerificationCode (code string ) string {
318- code = strings .ToUpper (code )
319- code = strings .ReplaceAll (code , " " , "" )
320- code = strings .ReplaceAll (code , "\t " , "" )
321- code = strings .ReplaceAll (code , "\n " , "" )
322- code = strings .ReplaceAll (code , "\r " , "" )
323- code = strings .ReplaceAll (code , "-" , "" )
324- code = strings .ReplaceAll (code , "1" , "I" )
325- code = strings .ReplaceAll (code , "8" , "B" )
326- code = strings .ReplaceAll (code , "0" , "O" )
327- return code
315+ // VerificationCodeEquals reports whether the user-supplied code matches the
316+ // stored normalized code, using a constant-time comparison to prevent timing
317+ // attacks.
318+ func VerificationCodeEquals (userInput , expected string ) bool {
319+ userInput = strings .ToUpper (userInput )
320+ userInput = strings .ReplaceAll (userInput , " " , "" )
321+ userInput = strings .ReplaceAll (userInput , "\t " , "" )
322+ userInput = strings .ReplaceAll (userInput , "\n " , "" )
323+ userInput = strings .ReplaceAll (userInput , "\r " , "" )
324+ userInput = strings .ReplaceAll (userInput , "-" , "" )
325+ userInput = strings .ReplaceAll (userInput , "1" , "I" )
326+ userInput = strings .ReplaceAll (userInput , "8" , "B" )
327+ userInput = strings .ReplaceAll (userInput , "0" , "O" )
328+ return subtle .ConstantTimeCompare ([]byte (userInput ), []byte (expected )) == 1
328329}
329330
330331func GetRequestLocale (explicitLocale string , r * http.Request ) string {
0 commit comments