Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

conversion from C# to JAVA - SECURITY

807605Aug 28 2007 — edited Aug 29 2007
hi ,
i have the following code in C# that i want to replicate in JAVA .
public static string GetPasswordHash(string password)

              {

                     if(password.Length == 0)

                           return string.Empty;

 

                     MemoryStream stream = new MemoryStream();

                     BinaryWriter bw = new BinaryWriter(stream, Encoding.Unicode);

                     bw.Write(password.ToCharArray());

 

                     SHA1CryptoServiceProvider sha = new SHA1CryptoServiceProvider();

                     byte [] hash = sha.ComputeHash(stream.GetBuffer(), 0, 20);

 

                     return Convert.ToBase64String(hash);

              }
i tried to do the following but the result was not the same , i got a different encrypted string .
can someone help ?
MessageDigest LHashAlgorithm = MessageDigest.getInstance("SHA");
		byte[] LBuffer = password.getBytes(); 
		LHashAlgorithm.update(LBuffer); 
		byte[] LDigestBuffer = LHashAlgorithm.digest(); 
		String LDigest = new sun.misc.BASE64Encoder().encode(LDigestBuffer);
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Sep 26 2007
Added on Aug 28 2007
33 comments
1,143 views