Obsfucate password :
Rreplace everything except first and last character by asterisk ( * ), if the password is less than ALLOWED_LENGTH characters long obfuscate it entirely i.e., prints asterisksCode :
//Test
System.out.println( getObfuscatedPassword( "" ) ); //returns null
System.out.println( getObfuscatedPassword( "pwdd" ) ); //returns ****
System.out.println( getObfuscatedPassword( "mySecurePassword" ) ); // returns m**************d
//Method
public static String getObfuscatedPassword( String password ) {
int ALLOWED_LENGTH = 5;
if ( null == password || "".equals( password.trim( ) ) ) {
return null;
}
StringBuilder builder = new StringBuilder( );
if ( password.length( ) < ALLOWED_LENGTH ) {
for ( int i = password.length( ); i != 0; i-- ) {
builder.append( "*" );
}
return builder.toString( );
}
builder.append( password.charAt( 0 ) );
for ( int i = password.length( ) - 2; i != 0; i-- ) {
builder.append( "*" );
}
return builder.append( password.substring( password.length( ) - 1 ) ).toString( );
}
No comments:
Post a Comment
Your Comment and Question will help to make this blog better...