FormsAuthentication.HashPasswordForStoringInConfigFile implementation
-
Hi, Can anyone help me by explaining the implementation of the below method, FormsAuthentication.HashPasswordForStoringInConfigFile("password", "SHA1"); since I have to implement the same hashing function in java1.5. below is my equivalent java code (but it is not returning the same result as .NET counterpart): public static String encryptPassword(String password) { String enyPass; try { MessageDigest messageDigest = MessageDigest.getInstance("SHA-1"); messageDigest.update(password.getBytes("UTF8")); //enyPass = new sun.misc.BASE64Encoder().encode(messageDigest.digest()); enyPass = new BigInteger(messageDigest.digest()).toString(16); messageDigest.reset(); } catch(Exception ex) { enyPass=null; } return enyPass; } Thanks in advance, Regards, Vythees
-
Hi, Can anyone help me by explaining the implementation of the below method, FormsAuthentication.HashPasswordForStoringInConfigFile("password", "SHA1"); since I have to implement the same hashing function in java1.5. below is my equivalent java code (but it is not returning the same result as .NET counterpart): public static String encryptPassword(String password) { String enyPass; try { MessageDigest messageDigest = MessageDigest.getInstance("SHA-1"); messageDigest.update(password.getBytes("UTF8")); //enyPass = new sun.misc.BASE64Encoder().encode(messageDigest.digest()); enyPass = new BigInteger(messageDigest.digest()).toString(16); messageDigest.reset(); } catch(Exception ex) { enyPass=null; } return enyPass; } Thanks in advance, Regards, Vythees
.NET reflector[^] is useful in cases like this - it gives the source as:
public static string HashPasswordForStoringInConfigFile(string password, string passwordFormat)
{
HashAlgorithm algorithm;
if (password == null)
{
throw new ArgumentNullException("password");
}
if (passwordFormat == null)
{
throw new ArgumentNullException("passwordFormat");
}
if (StringUtil.EqualsIgnoreCase(passwordFormat, "sha1"))
{
algorithm = SHA1.Create();
}
else
{
if (!StringUtil.EqualsIgnoreCase(passwordFormat, "md5"))
{
throw new ArgumentException(SR.GetString("InvalidArgumentValue", new object[] { "passwordFormat" }));
}
algorithm = MD5.Create();
}
return MachineKeySection.ByteArrayToHexString(algorithm.ComputeHash(Encoding.UTF8.GetBytes(password)), 0);
}-- Help me! I'm turning into a grapefruit! Buzzwords!
-
.NET reflector[^] is useful in cases like this - it gives the source as:
public static string HashPasswordForStoringInConfigFile(string password, string passwordFormat)
{
HashAlgorithm algorithm;
if (password == null)
{
throw new ArgumentNullException("password");
}
if (passwordFormat == null)
{
throw new ArgumentNullException("passwordFormat");
}
if (StringUtil.EqualsIgnoreCase(passwordFormat, "sha1"))
{
algorithm = SHA1.Create();
}
else
{
if (!StringUtil.EqualsIgnoreCase(passwordFormat, "md5"))
{
throw new ArgumentException(SR.GetString("InvalidArgumentValue", new object[] { "passwordFormat" }));
}
algorithm = MD5.Create();
}
return MachineKeySection.ByteArrayToHexString(algorithm.ComputeHash(Encoding.UTF8.GetBytes(password)), 0);
}-- Help me! I'm turning into a grapefruit! Buzzwords!
Thank you for your help, I managed to replicate the same. Here is the answer : ------------- public static String encryptPassword(String password) { try { if(messageDigest == null) messageDigest = MessageDigest.getInstance("SHA-1"); messageDigest.reset(); messageDigest.update(password.getBytes("UTF8")); byte[] hash = messageDigest.digest(); StringBuffer hexString = new StringBuffer(); for (int i=0;i