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

de.thksystems.util.crypto.PasswordUtils Maven / Gradle / Ivy

There is a newer version: 1.1.1
Show newest version
/*
 * tksCommons
 * 
 * Author  : Thomas Kuhlmann (ThK-Systems, http://www.thk-systems.de)
 * License : LGPL (https://www.gnu.org/licenses/lgpl.html)
 */
package de.thksystems.util.crypto;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

import org.bouncycastle.util.encoders.Base64;

import de.thksystems.exception.ServiceRuntimeException;

/**
 * Tools to encrypt passwords.
 */
public final class PasswordUtils {

	protected static final String PASSWORD_HASHALGO = "SHA-256";

	private PasswordUtils() {
	}

	/**
	 * Encrypts given password using SHA-256 and encodes Base64 .
	 */
	public static String encryptAsPassword(String password) {
		if (password == null || password.length() == 0) {
			return null;
		}
		try {
			MessageDigest md = MessageDigest.getInstance(PASSWORD_HASHALGO);
			byte[] digest = md.digest(password.getBytes());
			String encryptedPassword = new String(Base64.encode(digest));
			return encryptedPassword;
		} catch (NoSuchAlgorithmException e) {
			throw new ServiceRuntimeException(e);
		}
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy