How to hash a password

后端 未结 9 786
佛祖请我去吃肉
佛祖请我去吃肉 2020-11-22 14:49

I\'d like to store the hash of a password on the phone, but I\'m not sure how to do it. I can only seem to find encryption methods. How should the password be hashed properl

9条回答
  •  渐次进展
    2020-11-22 15:37

    I use a hash and a salt for my password encryption (it's the same hash that Asp.Net Membership uses):

    private string PasswordSalt
    {
       get
       {
          var rng = new RNGCryptoServiceProvider();
          var buff = new byte[32];
          rng.GetBytes(buff);
          return Convert.ToBase64String(buff);
       }
    }
    
    private string EncodePassword(string password, string salt)
    {
       byte[] bytes = Encoding.Unicode.GetBytes(password);
       byte[] src = Encoding.Unicode.GetBytes(salt);
       byte[] dst = new byte[src.Length + bytes.Length];
       Buffer.BlockCopy(src, 0, dst, 0, src.Length);
       Buffer.BlockCopy(bytes, 0, dst, src.Length, bytes.Length);
       HashAlgorithm algorithm = HashAlgorithm.Create("SHA1");
       byte[] inarray = algorithm.ComputeHash(dst);
       return Convert.ToBase64String(inarray);
    }
    

提交回复
热议问题