Example usage for com.liferay.portal.kernel.util Digester ENCODING

List of usage examples for com.liferay.portal.kernel.util Digester ENCODING

Introduction

In this page you can find the example usage for com.liferay.portal.kernel.util Digester ENCODING.

Prototype

String ENCODING

To view the source code for com.liferay.portal.kernel.util Digester ENCODING.

Click Source Link

Usage

From source file:org.joget.plugin.liferay.util.PasswordEncrypt.java

License:Open Source License

protected static String encodePassword(String algorithm, String clearTextPassword, byte[] saltBytes,
        boolean _base64) {
    String result = null;// w ww  .  j  a v  a  2  s .  c o  m

    try {
        if (algorithm.equals(TYPE_BCRYPT)) {
            String salt = new String(saltBytes);

            result = BCrypt.hashpw(clearTextPassword, salt);
        } else if (algorithm.equals(TYPE_CRYPT) || algorithm.equals(TYPE_UFC_CRYPT)) {

            result = Crypt.crypt(saltBytes, clearTextPassword.getBytes(Digester.ENCODING));
        } else if (algorithm.equals(TYPE_SSHA)) {
            byte[] clearTextPasswordBytes = clearTextPassword.getBytes(Digester.ENCODING);

            // Create a byte array of salt bytes appended to password bytes

            byte[] pwdPlusSalt = new byte[clearTextPasswordBytes.length + saltBytes.length];

            System.arraycopy(clearTextPasswordBytes, 0, pwdPlusSalt, 0, clearTextPasswordBytes.length);

            System.arraycopy(saltBytes, 0, pwdPlusSalt, clearTextPasswordBytes.length, saltBytes.length);

            // Digest byte array

            MessageDigest sha1Digest = MessageDigest.getInstance("SHA-1");

            byte[] pwdPlusSaltHash = sha1Digest.digest(pwdPlusSalt);

            // Appends salt bytes to the SHA-1 digest.

            byte[] digestPlusSalt = new byte[pwdPlusSaltHash.length + saltBytes.length];

            System.arraycopy(pwdPlusSaltHash, 0, digestPlusSalt, 0, pwdPlusSaltHash.length);

            System.arraycopy(saltBytes, 0, digestPlusSalt, pwdPlusSaltHash.length, saltBytes.length);
            // Base64 encode and format string
            result = Base64.encode(digestPlusSalt);
        } else {
            result = DigestUtil.digest(algorithm, _base64, new String[] { clearTextPassword });
        }
    } catch (NoSuchAlgorithmException nsae) {
    } catch (UnsupportedEncodingException uee) {
    }
    return result;
}

From source file:org.joget.plugin.liferay.util.PasswordEncrypt.java

License:Open Source License

private static byte[] _getSaltFromCrypt(String cryptString) {

    byte[] saltBytes = null;

    try {/*from  w ww .  j  a v  a  2s  .co m*/
        if (Validator.isNull(cryptString)) {

            // Generate random salt

            Random random = new Random();

            int numSaltChars = SALT_CHARS.length;

            StringBuilder sb = new StringBuilder();

            int x = random.nextInt(Integer.MAX_VALUE) % numSaltChars;
            int y = random.nextInt(Integer.MAX_VALUE) % numSaltChars;

            sb.append(SALT_CHARS[x]);
            sb.append(SALT_CHARS[y]);

            String salt = sb.toString();

            saltBytes = salt.getBytes(Digester.ENCODING);
        } else {

            // Extract salt from encrypted password

            String salt = cryptString.substring(0, 2);

            saltBytes = salt.getBytes(Digester.ENCODING);
        }
    } catch (UnsupportedEncodingException uee) {

    }

    return saltBytes;
}

From source file:org.xcolab.commons.utils.PwdEncryptor.java

License:Open Source License

protected static String encodePassword(String algorithm, String clearTextPassword, byte[] saltBytes)
        throws PwdEncryptorException {

    try {//  www.j a v  a 2  s  . c  o m
        if (algorithm.equals(TYPE_BCRYPT)) {
            String salt = new String(saltBytes);

            return BCrypt.hashpw(clearTextPassword, salt);
        }
        /*else if (algorithm.equals(TYPE_CRYPT) ||
             algorithm.equals(TYPE_UFC_CRYPT)) {
                
        return Crypt.crypt(
            saltBytes, clearTextPassword.getBytes(Digester.ENCODING));
        }*/
        else if (algorithm.equals(TYPE_SSHA)) {
            byte[] clearTextPasswordBytes = clearTextPassword.getBytes(Digester.ENCODING);

            // Create a byte array of salt bytes appended to password bytes

            byte[] pwdPlusSalt = new byte[clearTextPasswordBytes.length + saltBytes.length];

            System.arraycopy(clearTextPasswordBytes, 0, pwdPlusSalt, 0, clearTextPasswordBytes.length);

            System.arraycopy(saltBytes, 0, pwdPlusSalt, clearTextPasswordBytes.length, saltBytes.length);

            // Digest byte array

            MessageDigest sha1Digest = MessageDigest.getInstance("SHA-1");

            byte[] pwdPlusSaltHash = sha1Digest.digest(pwdPlusSalt);

            // Appends salt bytes to the SHA-1 digest.

            byte[] digestPlusSalt = new byte[pwdPlusSaltHash.length + saltBytes.length];

            System.arraycopy(pwdPlusSaltHash, 0, digestPlusSalt, 0, pwdPlusSaltHash.length);

            System.arraycopy(saltBytes, 0, digestPlusSalt, pwdPlusSaltHash.length, saltBytes.length);

            // Base64 encode and format string

            return Base64.encode(digestPlusSalt);
        } else {
            return DigesterUtil.digest(algorithm, clearTextPassword);
        }
    } catch (NoSuchAlgorithmException nsae) {
        throw new PwdEncryptorException(nsae.getMessage());
    } catch (UnsupportedEncodingException uee) {
        throw new PwdEncryptorException(uee.getMessage());
    }
}

From source file:org.xcolab.commons.utils.PwdEncryptor.java

License:Open Source License

private static byte[] _getSaltFromCrypt(String cryptString) throws PwdEncryptorException {

    byte[] saltBytes = null;

    try {//ww  w. j a  va2  s.com
        if (Validator.isNull(cryptString)) {

            // Generate random salt

            Random random = new Random();

            int numSaltChars = SALT_CHARS.length;

            StringBuilder sb = new StringBuilder();

            int x = random.nextInt(Integer.MAX_VALUE) % numSaltChars;
            int y = random.nextInt(Integer.MAX_VALUE) % numSaltChars;

            sb.append(SALT_CHARS[x]);
            sb.append(SALT_CHARS[y]);

            String salt = sb.toString();

            saltBytes = salt.getBytes(Digester.ENCODING);
        } else {

            // Extract salt from encrypted password

            String salt = cryptString.substring(0, 2);

            saltBytes = salt.getBytes(Digester.ENCODING);
        }
    } catch (UnsupportedEncodingException uee) {
        throw new PwdEncryptorException("Unable to extract salt from encrypted password: " + uee.getMessage());
    }

    return saltBytes;
}