uppercaseVariations property

double get uppercaseVariations

Guess multiplier for capital letters.

Implementation

double get uppercaseVariations {
  // Clean words of non alpha characters to remove the reward effect to
  // capitalize the first letter https://github.com/dropbox/zxcvbn/issues/232
  final String cleanedWord = token.replaceAll(_alphaInverted, '');
  if (_allLowerInverted.hasMatch(cleanedWord) ||
      cleanedWord.toLowerCase() == cleanedWord) {
    return 1;
  }
  // A capitalized word is the most common capitalization scheme,
  // so it only doubles the search space (uncapitalized + capitalized).
  // All caps and end-capitalized are common enough too, underestimate as 2x
  // factor to be safe.
  final List<RegExp> commonCases = <RegExp>[
    _startUpper,
    _endUpper,
    _allUpperInverted,
  ];
  for (final RegExp regExp in commonCases) {
    if (regExp.hasMatch(cleanedWord)) return 2;
  }
  // Otherwise calculate the number of ways to capitalize uppercase+lowercase
  // letters with upperCaseCount uppercase letters or less. Or, if there's
  // more uppercase than lower (for eg. PASSwORD), the number of ways to
  // lowercase uppercase+lowercase letters with lowerCaseCount lowercase
  // letters or less.
  final int upperCaseCount = _oneUpper.allMatches(cleanedWord).length;
  final int lowerCaseCount = _oneLower.allMatches(cleanedWord).length;
  double variations = 0;
  for (int i = 1; i <= min(upperCaseCount, lowerCaseCount); i++) {
    variations += nCk(upperCaseCount + lowerCaseCount, i);
  }
  return variations;
}