package marketing.heyday.hartmann.fotodocumentation;

import static org.junit.jupiter.api.Assertions.assertNotNull;

import java.nio.charset.Charset;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.util.Base64;
import java.util.Base64.Encoder;

import org.apache.commons.lang3.RandomStringUtils;
import org.junit.jupiter.api.Test;

/**
 * 
 * <p>Copyright: Copyright (c) 2024</p>
 * <p>Company: heyday Marketing GmbH</p>
 * @author <a href="mailto:p.verboom@heyday.marketing">Patrick Verboom</a>
 * @version 1.0
 * 
 * created: 20 Jan 2026
 */

public class SecurityGenerator {
	private static final int GENERATE_LENGTH = 10;
	private static final int SALT_LENGTH = 32;

	@java.lang.SuppressWarnings("java:S2245")
	public String generatePassword() {
		return RandomStringUtils.randomAlphanumeric(GENERATE_LENGTH);
	}

	public byte[] createSalt() {
		byte[] salt = new byte[SALT_LENGTH];
		SecureRandom random = new SecureRandom();
		random.nextBytes(salt);
		return salt;

	}

	public byte[] createPassword(String password, byte[] salt) throws NoSuchAlgorithmException {
		MessageDigest md = MessageDigest.getInstance("SHA-256");
		byte[] passwordBytes = password.getBytes(Charset.forName("utf-8"));
		md.update(passwordBytes);
		md.update(salt);

		return md.digest();
	}

	public String encode(byte[] hash) {
		Encoder encoder = Base64.getEncoder();
		return encoder.encodeToString(hash);
	}
	
	@Test
	public void test() throws NoSuchAlgorithmException {
		String password = generatePassword();
		assertNotNull(password);
		
		byte[] salt = createSalt();
		String saltHash = encode(salt);
		
		byte[] digest = createPassword(password, salt);
		String passwordHash = encode(digest);
		
		System.out.println("Password " + password);
		System.out.println("PasswordHash " + passwordHash);
		System.out.println("saltHash " + saltHash);
	}

}
