All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.databasesandlife.util.gwtsafe.CleartextPassword Maven / Gradle / Ivy

There is a newer version: 21.0.1
Show newest version
package com.databasesandlife.util.gwtsafe;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonValue;
import com.google.gwt.user.client.rpc.IsSerializable;

import javax.annotation.Nonnull;
import java.io.Serializable;
import java.util.*;

import static java.util.Arrays.asList;

/**
 * Wraps a string containing a cleartext password.
 * The purpose of this class is to increase type-safety over using a plain string.
 *
 * @author This source is copyright Adrian Smith and licensed under the LGPL 3.
 * @see Project on GitHub
 */
@SuppressWarnings("NotNullFieldNotInitialized") // due to no-arg constructor required for serialization
public class CleartextPassword implements Serializable, IsSerializable {

    protected @Nonnull String cleartext;

    @JsonCreator
    public CleartextPassword(@Nonnull String c) {
        cleartext = c;
    }

    @JsonValue
    public @Nonnull String getCleartext() { return cleartext; }
    
    /** @deprecated Do not use, required for GWT */
    @Deprecated private CleartextPassword() { }

    /**
     * Generates a random password.
     * 

A-Z, a-z, 0-9 characters are used, with the exception confusing characters such as uppercase O and digit 0 are not used.

*

The objective is that: *

    *
  • the passwords can be written down (thus confusing characters are removed)
  • *
  • and also that the password can be used in situations where special encoding is needed for example GET requests * (thus there are no special characters.)
  • *
*

Make up for "lack of randomness" by making the password longer.

*/ public static @Nonnull CleartextPassword newRandom(int length) { var chars = new ArrayList<>(); for (var c = 'A'; c <= 'Z'; c++) chars.add(c); for (var c = 'a'; c <= 'z'; c++) chars.add(c); for (var c = '0'; c <= '9'; c++) chars.add(c); chars.removeAll(asList('O', '0')); chars.removeAll(asList('I', 'l', '1')); chars.removeAll(asList('S', '5')); chars.removeAll(asList('2', 'Z')); var random = new Random(); var result = new StringBuilder(length); for (var i = 0; i < length; i++) { var charIdx = random.nextInt(chars.size()); result.append(chars.get(charIdx)); } return new CleartextPassword(result.toString()); } /** Generates a new password of a reasonable length; see {@link #newRandom(int)} */ public static @Nonnull CleartextPassword newRandom() { return newRandom(15); } @Override public boolean equals(Object o) { if (o == null || !getClass().equals(o.getClass())) return false; var that = (CleartextPassword) o; return cleartext.equals(that.cleartext); } @Override public int hashCode() { return 3458 + cleartext.hashCode(); } @Override public String toString() { return getClass().getSimpleName()+"("+cleartext+")"; } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy