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(System.Web.SR.GetString("InvalidArgumentValue", new object[] { "passwordFormat" })); } algorithm = MD5.Create(); } return(MachineKeySection.ByteArrayToHexString(algorithm.ComputeHash(Encoding.UTF8.GetBytes(password)), 0)); }
private static string Encrypt(FormsAuthenticationTicket ticket, bool hexEncodedTicket) { if (ticket == null) { throw new ArgumentNullException("ticket"); } Initialize(); byte[] buf = MakeTicketIntoBinaryBlob(ticket); if (buf == null) { return(null); } if ((_Protection == FormsProtectionEnum.All) || (_Protection == FormsProtectionEnum.Validation)) { byte[] src = MachineKeySection.HashData(buf, null, 0, buf.Length); if (src == null) { return(null); } byte[] dst = new byte[src.Length + buf.Length]; Buffer.BlockCopy(buf, 0, dst, 0, buf.Length); Buffer.BlockCopy(src, 0, dst, buf.Length, src.Length); buf = dst; } if ((_Protection == FormsProtectionEnum.All) || (_Protection == FormsProtectionEnum.Encryption)) { buf = MachineKeySection.EncryptOrDecryptData(true, buf, null, 0, buf.Length, false, false, IVType.Random); } if (!hexEncodedTicket) { return(HttpServerUtility.UrlTokenEncode(buf)); } return(MachineKeySection.ByteArrayToHexString(buf, 0)); }
public static string Encode(byte[] data, MachineKeyProtection protectionOption) { if (data == null) { throw new ArgumentNullException("data"); } if ((protectionOption == MachineKeyProtection.All) || (protectionOption == MachineKeyProtection.Validation)) { byte[] src = MachineKeySection.HashData(data, null, 0, data.Length); byte[] dst = new byte[src.Length + data.Length]; Buffer.BlockCopy(data, 0, dst, 0, data.Length); Buffer.BlockCopy(src, 0, dst, data.Length, src.Length); data = dst; } if ((protectionOption == MachineKeyProtection.All) || (protectionOption == MachineKeyProtection.Encryption)) { data = MachineKeySection.EncryptOrDecryptData(true, data, null, 0, data.Length, false, false, IVType.Random, !AppSettings.UseLegacyMachineKeyEncryption); } return(MachineKeySection.ByteArrayToHexString(data, 0)); }