Hash a password with SHA1 in java

I want to insert a password to my database using SHA1 hash I do it manually in phpmyadmin by choosing the function sha1 but how to do this using Java ??

Any Idea ? Thank you!


If you must use java:

import java.io.ByteArrayInputStream;
import java.security.MessageDigest;

public class SHACheckSumExample 
    public static void main(String[] args)throws Exception
        MessageDigest md = MessageDigest.getInstance("SHA-1");
        ByteArrayInputStream fis = new ByteArrayInputStream(args[1].getBytes());

        byte[] dataBytes = new byte[1024];

        int nread = 0; 
        while ((nread = fis.read(dataBytes)) != -1) {
          md.update(dataBytes, 0, nread);
        byte[] mdbytes = md.digest();

        //convert the byte to hex format method 1
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < mdbytes.length; i++) {
          sb.append(Integer.toString((mdbytes[i] & 0xff) + 0x100, 16).substring(1));

        System.out.println("Hex format : " + sb.toString());

       //convert the byte to hex format method 2
        StringBuffer hexString = new StringBuffer();
        for (int i=0;i<mdbytes.length;i++) {
          hexString.append(Integer.toHexString(0xFF & mdbytes[i]));

        System.out.println("Hex format : " + hexString.toString());

I would, for performance reasons, suggest seeing if your database has SHA support. I know Postgres does, not sure about other systems.

Need Your Help

Lucene: How to construct a date range query programmatically?


Using Lucene 4.2.1 API, I need an equivalent of

ReadKey in Console Application c#

c# console-application

I want to do something when user presses for example Alt+G keys, how can I do it? I tried this but didn't work:

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.