Click here to Skip to main content
15,884,099 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have done this in c# Now i want to check my password in java .How can i do
Java
public string HashSHA1(string value)
     {
         var sha1 = System.Security.Cryptography.SHA1.Create();
         var inputBytes = Encoding.ASCII.GetBytes(value);
         var hash = sha1.ComputeHash(inputBytes);

         var sb = new StringBuilder();
         for (var i = 0; i < hash.Length; i++)
         {
             sb.Append(hash[i].ToString("X2"));
         }
         return sb.ToString();
         //  return value;
     }
Posted
Updated 25-Sep-14 7:31am
v3
Comments
Sergey Alexandrovich Kryukov 24-Sep-14 21:37pm    
So, C# or Java.
—SA
Sergey Alexandrovich Kryukov 24-Sep-14 21:39pm    
And?...
—SA
dineshkumar51 24-Sep-14 22:02pm    
I want to check password in java so need java version
Sergey Alexandrovich Kryukov 25-Sep-14 9:40am    
This is not encryption, but cryptographic hash function.
You need to use the algorithm from SHA-2 family, or SHA-3 — SHA-1 was found broken.
Store only the hash, compare hash with hash.
—SA
Afzaal Ahmad Zeeshan 25-Sep-14 14:12pm    
Since you're new to this process. You can as a matter of fact, convert the passwords to a base-64 string, and use it in your application, conversion of it is easy. You can then revert it back. String is just a representation of bytes, so you can convert bytes to string and string to bytes is an easy way.

1 solution

I did it like this:

Java
private String getEncryptedPassword(String input){
    String output = "";
    try{
	byte [] hash = java.security.MessageDigest.getInstance("MD5").digest(input.getBytes());
	StringBuilder builder = new StringBuilder("");
	for(int i = 0; i < hash.length; i++){
		builder.append(Integer.toHexString(hash[i] & 0xFF | 0x100).substring(1, 3));
	}								//end for
	output = builder.toString();
    }									//end try
    catch(Exception ex){
		//log error here
    }									//end catch
    return output;
}



Hope this helps...
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900