com.teasoft.teavote.util.AppProperties.java Source code

Java tutorial

Introduction

Here is the source code for com.teasoft.teavote.util.AppProperties.java

Source

/*
 * 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 com.teasoft.teavote.util;

import static com.teasoft.teavote.util.PasswordHash.PBKDF2_ALGORITHM;
import java.lang.reflect.Field;
import java.security.InvalidKeyException;
import java.util.Properties;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
import java.security.Key;
import java.security.NoSuchAlgorithmException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.SecretKeySpec;
import org.apache.commons.codec.binary.Base64;

/**
 *
 * @author Elikem
 */
@ConfigurationProperties(prefix = "teavote")
@Component
public class AppProperties {

    private String orgName;
    private String commissioner;
    private String logo;
    private String secret;

    public String getOrgName() {
        return orgName;
    }

    public void setOrgName(String orgName) {
        this.orgName = orgName;
    }

    public String getCommissioner() {
        return commissioner;
    }

    public void setCommissioner(String commissioner) {
        this.commissioner = commissioner;
    }

    public String getLogo() {
        return logo;
    }

    public void setLogo(String logo) {
        this.logo = logo;
    }

    public Properties getProperties() throws IllegalAccessException {
        Properties prop = new Properties();
        Field[] fields = AppProperties.class.getDeclaredFields();
        for (Field field : fields) {
            prop.setProperty("teavote" + "." + field.getName(), field.getType().cast(field.get(this)).toString());
        }
        return prop;
    }

    public String getSecret() {
        return secret;
    }

    public void setSecret(String secret) {
        try {
            String first = PBKDF2_ALGORITHM.substring(0, PBKDF2_ALGORITHM.length() - 2);
            String second = "";
            for (int i = 0, j = 12; i < 4; i++) {
                second += first.substring(j - (i * 4), first.length() - i * 4);
            }
            Key aesKey = new SecretKeySpec(second.getBytes(), "AES");
            Cipher cipher = Cipher.getInstance("AES");
            cipher.init(Cipher.ENCRYPT_MODE, aesKey);
            byte[] encrypted = cipher.doFinal(secret.getBytes());
            this.secret = Base64.encodeBase64String(encrypted);
        } catch (NoSuchAlgorithmException | NoSuchPaddingException | InvalidKeyException | IllegalBlockSizeException
                | BadPaddingException ex) {
            Logger.getLogger(AppProperties.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}