com.databasesandlife.util.BCryptPassword Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of java-common Show documentation
Show all versions of java-common Show documentation
Utility classes developed at Adrian Smith Software (A.S.S.)
package com.databasesandlife.util;
import com.databasesandlife.util.gwtsafe.CleartextPassword;
import org.mindrot.jbcrypt.BCrypt;
import javax.annotation.Nonnull;
import java.io.Serializable;
/**
* Wraps a string containing a BCrypt encrypted 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
*/
public class BCryptPassword implements Serializable {
public final @Nonnull String bcrypt;
public BCryptPassword(@Nonnull String b) {
bcrypt = b;
}
public BCryptPassword(@Nonnull CleartextPassword b) {
bcrypt = BCrypt.hashpw(b.getCleartext(), BCrypt.gensalt());
}
public boolean is(@Nonnull CleartextPassword b) {
return BCrypt.checkpw(b.getCleartext(), bcrypt);
}
@Override
public boolean equals(Object o) {
if (o == null || !getClass().equals(o.getClass())) return false;
var that = (BCryptPassword) o;
return bcrypt.equals(that.bcrypt);
}
@Override
public int hashCode() {
return 3452356 + bcrypt.hashCode();
}
}