Java XML Hash hashPassword(char[] password, String salt, String hashAlgo)

Here you can find the source of hashPassword(char[] password, String salt, String hashAlgo)

Description

Hash the given password using given algorithm.

License

Open Source License

Parameter

Parameter Description
password Password to be hashed.
salt Salt to be used to hash the password.
hashAlgo Hashing algorithm to be used.

Exception

Parameter Description
NoSuchAlgorithmException an exception

Return

Hash as a String

Declaration

public static String hashPassword(char[] password, String salt, String hashAlgo)
        throws NoSuchAlgorithmException 

Method Source Code


//package com.java2s;
/*/*  ww  w . ja  va2 s. co m*/
 * Copyright (c) 2016, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import java.nio.CharBuffer;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Arrays;

import javax.xml.bind.DatatypeConverter;

public class Main {
    /**
     * Hash the given password using given algorithm.
     * @param password Password to be hashed.
     * @param salt Salt to be used to hash the password.
     * @param hashAlgo Hashing algorithm to be used.
     * @return Hash as a <code>String</code>
     * @throws NoSuchAlgorithmException
     */
    public static String hashPassword(char[] password, String salt, String hashAlgo)
            throws NoSuchAlgorithmException {

        // Merge the password and salt to a single array.
        char[] saltedPassword = Arrays.copyOf(password, password.length + salt.length());
        System.arraycopy(salt.toCharArray(), 0, saltedPassword, password.length, salt.length());

        MessageDigest messageDigest = MessageDigest.getInstance(hashAlgo);
        byte[] hash = messageDigest.digest(StandardCharsets.UTF_8.encode(CharBuffer.wrap(saltedPassword)).array());

        // Hash is in hex binary. Convert and return.
        return DatatypeConverter.printHexBinary(hash);
    }
}

Related

  1. hash(byte[] bytes)
  2. hash(final String text, final String algorithm)
  3. hash(String data, String salt)
  4. hash512(byte[] data)
  5. hashPass(String plaintext)
  6. hashPassword(String password)
  7. hashToString(byte[] hash)
  8. sha1Hash(String s)
  9. strHash(String value, String method)