Java Hash Code Calculate generateHash(String plaintext)

Here you can find the source of generateHash(String plaintext)

Description

generate Hash

License

Open Source License

Declaration

public static String generateHash(String plaintext) 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class Main {
    public static String generateHash(String plaintext) {
        try {/*  w  ww  .j  av a  2s . c  om*/
            StringBuilder hash = new StringBuilder();
            MessageDigest sha = MessageDigest.getInstance("SHA-1");
            byte[] hashedBytes = sha.digest(plaintext.getBytes());
            char[] digits = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
            for (int idx = 0; idx < hashedBytes.length; ++idx) {
                byte b = hashedBytes[idx];
                hash.append(digits[(b & 0xf0) >> 4]);
                hash.append(digits[b & 0x0f]);
            }
            return hash.toString();
        } catch (NoSuchAlgorithmException e) {
            throw new RuntimeException(e);
        }
    }
}

Related

  1. generateHash(final String msg, final String hashAlgorithm)
  2. generateHash(String algo, byte[]... bytes)
  3. generateHash(String input)
  4. generateHash(String input, String salt)
  5. generateHash(String item)
  6. generateHash(String plainText, String hashType)
  7. generateHash(String serviceUri, String username, String password)
  8. generateHash(String target)
  9. generateHash(String tcString)