package com.github.aureliano.edocs.secure.hash;

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

import javax.xml.bind.DatatypeConverter;

import com.github.aureliano.edocs.secure.model.PasswordEncryptionModel;

public final class PasswordHashGenerator {

	private PasswordHashGenerator() {}
	
	public static final String generate(PasswordEncryptionModel pwd) {
		String text = pwd.getPassword() + pwd.getSalt();
		
		try {
			for (short i = 0; i < pwd.getHashIterations(); i++) {
				text = hash(text, pwd.getAlgorithm().getLabel());
			}
		} catch (NoSuchAlgorithmException | UnsupportedEncodingException ex) {
			throw new SecurityException(ex);
		}
		
		return text;
	}
	
	private static String hash(String text, String algorithm) throws NoSuchAlgorithmException, UnsupportedEncodingException {
		MessageDigest md = MessageDigest.getInstance(algorithm);
		md.update(text.getBytes("UTF-8"));
		
		byte bytes[] = md.digest();
		String hash = DatatypeConverter.printBase64Binary(bytes);
		
		return hash;
	}
}
@@ -213,16 +213,281 @@
 erter;%0A%0A
+import com.github.aureliano.edocs.common.config.AppConfiguration;%0Aimport com.github.aureliano.edocs.common.config.ConfigurationSingleton;%0Aimport com.github.aureliano.edocs.common.config.SecureConfiguration;%0Aimport com.github.aureliano.edocs.secure.model.Algorithm;%0A
 import c
@@ -1015,16 +1015,538 @@
 t;%0A%09%7D%0A%09%0A
+%09public static final String generateFromAppConfiguration(String password) %7B%0A%09%09AppConfiguration appConfiguration = ConfigurationSingleton.instance().getAppConfiguration();%0A%09%09SecureConfiguration configuration = appConfiguration.getSecureConfiguration();%0A%09%09%0A%09%09return PasswordHashGenerator.generate(new PasswordEncryptionModel()%0A%09%09%09.withAlgorithm(Algorithm.valueOf(configuration.getAlgorithm()))%0A%09%09%09.withHashIterations(configuration.getHashIterations())%0A%09%09%09.withSalt(configuration.getSalt())%0A%09%09%09.withPassword(password));%0A%09%7D%0A%09%0A
 %09private

Add method to generate passowrd hash with configuration properties.