net.alegen.datpass.library.Generator.java Source code

Java tutorial

Introduction

Here is the source code for net.alegen.datpass.library.Generator.java

Source

/*
 * EasyHash - command line utility for creating hashes
 * Copyright (C) 2013 - 2014 Alexandru Geana (alegen)
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

package net.alegen.datpass.library;

import net.alegen.datpass.library.configure.FieldManager;
import net.alegen.datpass.library.crypto.CryptoManager;
import net.alegen.datpass.library.crypto.KeyDerivationFunctions;
import org.apache.commons.codec.binary.Base64;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.crypto.SecretKey;
import java.io.UnsupportedEncodingException;

public class Generator {

    private static final Logger log = LoggerFactory.getLogger(Generator.class);
    private static Generator instance = null;

    public static Generator getInstance() {
        if (instance == null)
            instance = new Generator();
        return instance;
    }

    private Profile currentProfile;

    private Generator() {
    };

    public void setCurrentProfile(Profile profile) {
        if (profile == null)
            throw new NullPointerException();
        this.currentProfile = profile;
    }

    public Profile getCurrentProfile() {
        return this.currentProfile;
    }

    public void reset() {
        this.currentProfile = null;
    }

    public String password(String input, int length) throws GeneratorException {
        if (this.currentProfile == null) {
            log.error("Cannot generate a password without having loaded a profile first.");
            throw new GeneratorException();
        }
        try {
            // calculate bit length
            int bitLength = (int) (length * 4.0 / 3 * 8); // take into account length for base64 encoding
            if (bitLength % 8 != 0)
                bitLength = (bitLength / 8) * 8; // ensure multiple of 8

            // generate password
            SecretKey secretKey = CryptoManager.getInstance().derivateKey(
                    KeyDerivationFunctions.fromString(this.currentProfile.getValue(FieldManager.FUNCTION_FIELD)),
                    input, this.currentProfile.getValue(FieldManager.SALT_FIELD).getBytes("UTF-8"), bitLength,
                    Integer.parseInt(this.currentProfile.getValue(FieldManager.ITER_FIELD)));
            byte[] encodedPassword = Base64.encodeBase64(secretKey.getEncoded());
            String retval = new String(encodedPassword, "UTF-8");

            // trim to desired length
            if (retval.length() != length)
                retval = retval.substring(0, length);

            // substitute characters to have just a-z, A-z, 0-9
            retval = retval.replace('/', retval.charAt(retval.length() / 2));
            retval = retval.replace('+', retval.charAt(retval.length() / 3));
            retval = retval.replace('=', retval.charAt(retval.length() / 5));
            return retval;
        } catch (UnsupportedEncodingException e) {
            log.error("UTF-8 encoding seems to not be supported?!");
            e.printStackTrace();
            throw new RuntimeException("An internall error occured and the operation failed.");
        }
    }

    public static class GeneratorException extends Exception {
        private static final long serialVersionUID = 8341559842873521405L;

        public GeneratorException() {
            super();
        }
    }
}