Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. FormsAuthentication.HashPasswordForStoringInConfigFile implementation

FormsAuthentication.HashPasswordForStoringInConfigFile implementation

Scheduled Pinned Locked Moved C#
csharpjavahelpannouncement
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • V Offline
    V Offline
    vytheese
    wrote on last edited by
    #1

    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

    B 1 Reply Last reply
    0
    • V vytheese

      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

      B Offline
      B Offline
      benjymous
      wrote on last edited by
      #2

      .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!

      V 1 Reply Last reply
      0
      • B benjymous

        .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!

        V Offline
        V Offline
        vytheese
        wrote on last edited by
        #3

        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

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups