Coder Social home page Coder Social logo

oscarito9410 / androidpasswordstrengthmeter Goto Github PK

View Code? Open in Web Editor NEW

This project forked from gustavaa/androidpasswordstrengthmeter

0.0 1.0 0.0 159 KB

An easy-to-implement and fully customizable password strength indicator for Android

License: Apache License 2.0

Java 100.00%

androidpasswordstrengthmeter's Introduction

Android Password Strength Meter

Password strength meter is an easy-to-implement and flexible password strength indicator for Android. It is fully customizable and features an animated strength indicator and a matching label.

Usage

Project level build.gradle

allprojects {
    repositories {
        mavenCentral()
    }
}

App level build.gradle

dependencies {
    implementation 'nu.aaro.gustav:passwordstrengthmeter:0.4'
}

Examples

XML

PasswordStrengthMeter can be initialized by defining it in a layout XML file, for example:

    <nu.aaro.gustav.passwordstrengthmeter.PasswordStrengthMeter
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/passwordInputMeter"
        app:strengthBarHeight="5dp" 
        app:animateChanges="true"
        app:showStrengthLabel="true"
        app:showStrengthBar="true"
        app:animationDuration="300"
        app:labelTextSize="12sp"/>

Then all you have to do is to set it up like this in your activity of fragment

PasswordStrengthMeter meter = findViewById(R.id.passwordInputMeter);
meter.setEditText(passwordInputEditText);

XML properties

  • showStrengthBar: Define whether or not the view should show the strength indicator bar. Default: true
  • strengthBarHeight: The height of the strength indicator bar. Default: 5dp
  • animateChanges: Define whether or not the bar should animate changes. Default: true
  • animationDuration: The duration of the password strength level change animation in ms. Default: 300ms.
  • showStrengthLabel: Define whether or not the view should show a label which show the current strength level display name. Default: true
  • labelTextSize: The label text size. Default: 12sp

Java

PasswordStrengthMeter can also be initalized programatically:

PasswordStrengthMeter meter = new PasswordStrengthMeter(this);
meter.setEditText(passwordInput);
linearLayout.addView(meter);

// And some customization can be done by: 
meter.setAnimationDuration(300); 
meter.setShowStrengthIndicator(true); 
meter.setShowStrengthLabel(true);

Customization

Apart from the basic cosmetic customization described above, PasswordStrengthMeter can also be customized in other aspects.

Password strength calculation algorithm

PasswordStrengthMeter implements a default algorithm for password strength estimation, although it is very basic and should not be considered the main contribution of this library. Instead, I recommend you to implement an algorithm that fits your system the best using the PasswordStrengthCalculatorinterface:

meter.setPasswordStrengthCalculator(new PasswordStrengthCalculator() {
        @Override
        public int calculatePasswordSecurityLevel(String password) {
            // Do some calculation and return an int corresponding to the "points" or "level" the user password got
            return points;
        }

        @Override
        public int getMinimumLength() {
            // Define the minimum length of a password. Anything below this should always yield a score of 0
            return 8;
        }

        @Override
        public boolean passwordAccepted(int level) {
            // Define whether or not the level is an accepted level or not. 
            return level > 3;
        }

        @Override
        public void onPasswordAccepted(String password) {
          // Called when the password entered meets your requirements of length and strength levels
        }
    });

Password strength levels

PasswordStrengthMeter has 5 (or 6 if you count level 0) default password strength levels. These are simply PasswordStrengthLevel objects that defines a display name and a color associated with the level. These are stored in an array, where the index corresponds to the numerical "level" or "score" from the password strength calculation algorithm.

The default levels are defined as follows:

PasswordStrengthLevel[] strengthLevels = {
    new PasswordStrengthLevel("Too short", android.R.color.darker_gray), // level 0
    new PasswordStrengthLevel("Weak", android.R.color.holo_red_dark), // level 1
    new PasswordStrengthLevel("Fair", android.R.color.holo_orange_dark), // level 2
    new PasswordStrengthLevel("Good", android.R.color.holo_orange_light), // level 3
    new PasswordStrengthLevel("Strong", android.R.color.holo_blue_light), // level 4
    new PasswordStrengthLevel("Very strong", android.R.color.holo_green_dark)}; // level 5

These levels can easily be changed by simply creating a similar array and call setStrengthLevels:

PasswordStrengthMeter meter = new PasswordStrengthMeter(this);
meter.setStrengthLevels(new PasswordStrengthLevel[]{
        new PasswordStrengthLevel("Level 0", android.R.color.white),
        new PasswordStrengthLevel("Level 1", android.R.color.holo_red_light),
        new PasswordStrengthLevel("Level 2", android.R.color.holo_orange_light),
        new PasswordStrengthLevel("Level 3", android.R.color.holo_green_light)});

NOTE that for the best result, you should provide the meter with a number of levels that is adapted for the strength estimation algorithm used. I.e. if the algorithm yields a level between 0 and 5 (as the default implementation), you should provide the meter with a total of 6 strength levels.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.