Skip to content

Commit

Permalink
f
Browse files Browse the repository at this point in the history
  • Loading branch information
etorth committed Jan 15, 2025
1 parent beecc6f commit b519556
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 0 deletions.
55 changes: 55 additions & 0 deletions common/src/pwdf.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
#include <array>
#include "fflerror.hpp"
#include "pwdf.hpp"

int pwdf::strength(const char *s)
{
fflassert(s);

int length = 0;
int digits = 0;
int lowers = 0;
int uppers = 0;
int specials = 0;

std::array<uint8_t, 256> counts{};
for(; s[length] != '\0'; ++length){
if (s[length] >= '0' && s[length] <= '9') ++digits;
else if(s[length] >= 'a' && s[length] <= 'z') ++lowers;
else if(s[length] >= 'A' && s[length] <= 'Z') ++uppers;
else if(s[length] == '!' || s[length] == '@' || s[length] == '#' ||
s[length] == '$' || s[length] == '%' || s[length] == '^' ||
s[length] == '&' || s[length] == '*' || s[length] == '(' ||
s[length] == ')' || s[length] == '-' || s[length] == '+') ++specials;

counts[s[length]]++;
if(length >= 255){
break;
}
}

if(length < 8 || digits == 0 || lowers == 0 || uppers == 0 || specials == 0){
return pwdf::INVALID;
}

const auto unichars = std::count_if(counts.begin(), counts.end(), [](const auto &x)
{
return x > 0;
});

if(unichars * 2 < length){
return pwdf::WEAK;
}

int score = 0;

if(length >= 12) score++;
if(uppers >= 2) score++;
if(specials >= 2) score++;

if(unichars * 4 >= length * 3) score++;

if (score >= 4) return pwdf::STRONG;
else if(score >= 2) return pwdf::MODERATE;
else return pwdf::WEAK;
}
21 changes: 21 additions & 0 deletions common/src/pwdf.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#pragma once
namespace pwdf
{
// A valid password should satisfy the following conditions:
//
// It contains at least one digit.
// It contains at least one lowercase character.
// It contains at least one uppercase character.
// It contains at least one special character, the special characters are: !@#$%^&*()-+
// Its length is at least 8.

enum
{
INVALID = 0,
WEAK = 1,
MODERATE = 2,
STRONG = 3,
};

int strength(const char *);
}

0 comments on commit b519556

Please sign in to comment.