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.common.config.AppConfiguration;
import com.github.aureliano.edocs.common.config.ConfigurationSingleton;
import com.github.aureliano.edocs.common.config.SecureConfiguration;
import com.github.aureliano.edocs.secure.model.Algorithm;
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;
	}
	
	public static final String generateFromAppConfiguration(String password) {
		AppConfiguration appConfiguration = ConfigurationSingleton.instance().getAppConfiguration();
		SecureConfiguration configuration = appConfiguration.getSecureConfiguration();
		
		return PasswordHashGenerator.generate(new PasswordEncryptionModel()
			.withAlgorithm(Algorithm.valueOf(configuration.getAlgorithm()))
			.withHashIterations(configuration.getHashIterations())
			.withSalt(configuration.getSalt())
			.withPassword(password));
	}
	
	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;
	}
}
@@ -46,179 +46,8 @@
 h;%0A%0A
-import java.io.UnsupportedEncodingException;%0Aimport java.security.MessageDigest;%0Aimport java.security.NoSuchAlgorithmException;%0A%0Aimport javax.xml.bind.DatatypeConverter;%0A%0A
 impo
@@ -100,32 +100,32 @@
 pConfiguration;%0A
+
 import com.githu
@@ -588,17 +588,8 @@
 %0A%09%09%0A
-%09%09try %7B%0A%09
 %09%09fo
@@ -647,17 +647,38 @@
 %0A%09%09%09
-%09
 text = 
-h
+HashGenerator.generateH
 ash(
@@ -719,122 +719,8 @@
 ));%0A
-%09%09%09%7D%0A%09%09%7D catch (NoSuchAlgorithmException %7C UnsupportedEncodingException ex) %7B%0A%09%09%09throw new SecurityException(ex);%0A
 %09%09%7D%0A
@@ -1226,24 +1226,24 @@
 .getSalt())%0A
+
 %09%09%09.withPass
@@ -1266,340 +1266,5 @@
 %0A%09%7D%0A
-%09%0A%09private static String hash(String text, String algorithm) throws NoSuchAlgorithmException, UnsupportedEncodingException %7B%0A%09%09MessageDigest md = MessageDigest.getInstance(algorithm);%0A%09%09md.update(text.getBytes(%22UTF-8%22));%0A%09%09%0A%09%09byte bytes%5B%5D = md.digest();%0A%09%09String hash = DatatypeConverter.printBase64Binary(bytes);%0A%09%09%0A%09%09return hash;%0A%09%7D%0A
 %7D

Use hash generator class.