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

kz.greetgo.security.password.PasswordEncoderBuilder Maven / Gradle / Ivy

There is a newer version: 0.0.12
Show newest version
package kz.greetgo.security.password;

import org.bson.internal.Base64;

import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class PasswordEncoderBuilder {

  PasswordEncoderBuilder() {}

  public static PasswordEncoderBuilder newBuilder() {
    return new PasswordEncoderBuilder();
  }

  String salt = null;

  public PasswordEncoderBuilder setSalt(String salt) {
    checkBuilt();
    this.salt = salt;
    return this;
  }

  private void checkBuilt() {
    if (built) throw new RuntimeException("Already built");
  }

  private boolean built = false;

  public PasswordEncoder build() {
    if (salt == null) throw new RuntimeException("Please, define salt calling method 'setSalt(...)'");
    built = true;
    return new PasswordEncoder() {
      @Override
      public String encode(String password) {
        try {

          MessageDigest digest = MessageDigest.getInstance("SHA-256");

          digest.update(salt.getBytes(StandardCharsets.UTF_8));

          if (password != null) {
            digest.update(password.getBytes(StandardCharsets.UTF_8));
          }

          String base64 = Base64.encode(digest.digest());
          base64 = base64.replace('/', '$');
          base64 = base64.replace('+', '~');
          base64 = base64.substring(0, base64.length() - 1);

          return base64;

        } catch (NoSuchAlgorithmException e) {
          throw new RuntimeException(e);
        }
      }

      @Override
      public boolean verify(String password, String encodedPassword) {
        return encode(password).equals(encodedPassword);
      }
    };
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy