// Chromis POS - The New Face of Open Source POS
// Copyright (c) (c) 2015-2016
// http://www.chromis.co.uk
//
// This file is part of Chromis POS
//
// Chromis POS 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.
//
// Chromis POS 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 Chromis POS. If not, see <http://www.gnu.org/licenses/>.
package uk.chromis.pos.util;
import java.awt.Component;
import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import javax.swing.ImageIcon;
import javax.swing.JOptionPane;
import uk.chromis.beans.JPasswordDialog;
import uk.chromis.pos.forms.AppLocal;
/**
*
*
*/
public class Hashcypher {
/** Creates a new instance of Hashcypher */
public Hashcypher() {
}
/**
*
* @param sPassword
* @param sHashPassword
* @return
*/
public static boolean authenticate(String sPassword, String sHashPassword) {
if (sHashPassword == null || sHashPassword.equals("") || sHashPassword.startsWith("empty:")) {
return sPassword == null || sPassword.equals("");
} else if (sHashPassword.startsWith("sha1:")) {
return sHashPassword.equals(hashString(sPassword));
} else if (sHashPassword.startsWith("plain:")) {
return sHashPassword.equals("plain:" + sPassword);
} else {
return sHashPassword.equals(sPassword);
}
}
/**
*
* @param sPassword
* @return
*/
public static String hashString(String sPassword) {
if (sPassword == null || sPassword.equals("")) {
return "empty:";
} else {
try {
MessageDigest md = MessageDigest.getInstance("SHA-1");
md.update(sPassword.getBytes("UTF-8"));
byte[] res = md.digest();
return "sha1:" + StringUtils.byte2hex(res);
} catch (NoSuchAlgorithmException e) {
return "plain:" + sPassword;
} catch (UnsupportedEncodingException e) {
return "plain:" + sPassword;
}
}
}
/**
*
* @param parent
* @return
*/
public static String changePassword(Component parent) {
// Show the changePassword dialogs but do not check the old password
String sPassword = JPasswordDialog.showEditPassword(parent,
AppLocal.getIntString("Label.Password"),
AppLocal.getIntString("label.passwordnew"),
new ImageIcon(Hashcypher.class.getResource("/uk/chromis/images/password.png")));
if (sPassword != null) {
String sPassword2 = JPasswordDialog.showEditPassword(parent,
AppLocal.getIntString("Label.Password"),
AppLocal.getIntString("label.passwordrepeat"),
new ImageIcon(Hashcypher.class.getResource("/uk/chromis/images/password.png")));
if (sPassword2 != null) {
if (sPassword.equals(sPassword2)) {
return Hashcypher.hashString(sPassword);
} else {
JOptionPane.showMessageDialog(parent, AppLocal.getIntString("message.changepassworddistinct"), AppLocal.getIntString("message.title"), JOptionPane.WARNING_MESSAGE);
}
}
}
return null;
}
/**
*
* @param parent
* @param sOldPassword
* @return
*/
public static String changePassword(Component parent, String sOldPassword) {
String sPassword = JPasswordDialog.showEditPassword(parent,
AppLocal.getIntString("Label.Password"),
AppLocal.getIntString("label.passwordold"),
new ImageIcon(Hashcypher.class.getResource("/uk/chromis/images/password.png")));
if (sPassword != null) {
if (Hashcypher.authenticate(sPassword, sOldPassword)) {
return changePassword(parent);
} else {
JOptionPane.showMessageDialog(parent, AppLocal.getIntString("message.BadPassword"), AppLocal.getIntString("message.title"), JOptionPane.WARNING_MESSAGE);
}
}
return null;
}
}