Java Hash Calculate getHash(String password, String salt)

Here you can find the source of getHash(String password, String salt)

Description

Gets a salted hash code.

License

Open Source License

Declaration

public static String getHash(String password, String salt) 

Method Source Code


//package com.java2s;
/*/* ww  w .  j a  v  a  2s  .  com*/
 * This file is part of "U Turismu" project. 
 * 
 * U Turismu is an enterprise application in support of calabrian tour operators.
 * This system aims to promote tourist services provided by the operators
 * and to develop and improve tourism in Calabria.
 *
 * Copyright (C) 2012 "LagrecaSpaccarotella" team.
 *
 * 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/>.
 */

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Formatter;

public class Main {
    private static final String SHA_512 = "SHA-512";

    /**
     * Gets a salted hash code.
     */
    public static String getHash(String password, String salt) {
        try {
            // a message digest object is created;
            MessageDigest messageDigest = null;
            messageDigest = MessageDigest.getInstance(SHA_512);
            messageDigest.reset();
            messageDigest.update(salt.getBytes("UTF-8"));
            messageDigest.update(password.getBytes("UTF-8"));
            // a message digest is generated
            byte[] input = messageDigest.digest();
            // the message digest is hashed 1000 times
            for (int i = 0; i < 1000; i++) {
                messageDigest.reset();
                input = messageDigest.digest(input);
            }
            // the message digest is formatted in hexadecimal format
            Formatter formatter = new Formatter();
            for (byte i : input) {
                formatter.format("%02x", i);
            }
            return formatter.toString();
        } catch (NoSuchAlgorithmException e) {
            return "";
        } catch (UnsupportedEncodingException e) {
            return "";
        }
    }
}

Related

  1. getHash(String message)
  2. getHash(String pass, String algorithm)
  3. getHash(String password, byte[] salt)
  4. getHash(String password, byte[] salt)
  5. getHash(String password, String bsalt)
  6. getHash(String password, String salt)
  7. getHash(String phrase, String algorithm)
  8. getHash(String s)
  9. getHash(String str, String algorithm)