/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package plate.wellmann.shop.business;

import java.security.NoSuchAlgorithmException;
import java.security.spec.InvalidKeySpecException;
import java.util.Arrays;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.PBEKeySpec;
import javax.security.enterprise.identitystore.PasswordHash;
import org.apache.log4j.Logger;
import static plate.wellmann.shop.business.PasswordManager.logger;

/**
 *
 * @author Patrick
 */
public class MyPasswordHash implements PasswordHash{
    
    final static Logger LOG = Logger.getLogger(MyPasswordHash.class);
    
    @Override
    public String generate(char[] chars) {
        try {
            SecretKeyFactory skf = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA512");
            PBEKeySpec spec = new PBEKeySpec(chars, "salz".getBytes(), 3072, 256);
            SecretKey key = skf.generateSecret(spec);
            //this.password = new Password(this.costumer, key.getEncoded());
            logger.debug("Hash created!");
            return Arrays.toString(key.getEncoded());
        } catch (NoSuchAlgorithmException | InvalidKeySpecException e) {
            logger.error("Failure creating hash with:" + e);
            return null;
            //throw new RuntimeException( e );
        }
    }

    @Override
    public boolean verify(char[] chars, String string) {
        String pw = generate(chars);
        return pw.equals(string);
    }
    
}
