public static string SignCookie(string input, long timestamp = long.MinValue, IPAddress remoteIp = null, uint validTime = uint.MaxValue) { if (input == null) { throw new ArgumentNullException(); } if (hmacKey == null) { throw new InvalidOperationException(); } var dataLen = cookieEncoding.GetByteCount(input) + DataOffset; var data = new byte[dataLen]; var encodedLength = cookieEncoding.GetBytes(input, 0, input.Length, data, DataOffset); System.Diagnostics.Debug.Assert(encodedLength == (dataLen - DataOffset)); data.PutInt64(TsOffset, timestamp == long.MinValue ? UnixTimestamp.CurrentMillisecondTimestamp : timestamp); data.PutUInt32(ValidityOffset, validTime); if (remoteIp == null) //which means don't care { remoteIp = IPAddress.IPv6Any; //all zeroes } else if (remoteIp.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork) { remoteIp = remoteIp.MapToIPv6(); } Array.Copy(remoteIp.GetAddressBytes(), 0, data, IpAddrOffset, IpAddrLength); Array.Copy(hmac.Value.ComputeHash(data, SigLength, data.Length - SigLength), data, SigLength); return(Convert.ToBase64String(data).Replace('+', '.').Replace('/', '_').Replace('=', '-')); }
private void WriteToStream(Stream stream, string msg) { if (stream != null) { stream.Write(uniEncoding.GetBytes(msg), 0, uniEncoding.GetByteCount(msg)); } }
/// <summary> /// This will look at the start of the file for the BOM and process it /// if it's found. /// </summary> private void CheckforAndHandleBOM() { byte[] utf32be = new byte[] { 0x00, 0x00, 0xfe, 0xff }; byte[] utf32le = new byte[] { 0xff, 0xfe, 0x00, 0x00 }; byte[] utf16be = new byte[] { 0xfe, 0xff }; byte[] utf16le = new byte[] { 0xff, 0xfe }; byte[] utf8 = new byte[] { 0xef, 0xbb, 0xbf }; if (IsCurrentData(utf32le) || IsCurrentData(utf32be)) { m_foundBOM = true; m_BOMLength = utf32be.Length; //m_BOMEncoding = System.Text.Encoding.???; // not currently defined for 32 m_position = m_BOMLength; } else if (IsCurrentData(utf8)) { m_foundBOM = true; m_BOMLength = utf8.Length; m_BOMEncoding = System.Text.Encoding.UTF8; m_position = m_BOMLength; } else if (IsCurrentData(utf16le)) { m_foundBOM = true; m_BOMLength = utf16le.Length; m_BOMEncoding = System.Text.Encoding.Unicode; m_position = m_BOMLength; } else if (IsCurrentData(utf16be)) { m_foundBOM = true; m_BOMLength = utf16be.Length; m_BOMEncoding = System.Text.Encoding.BigEndianUnicode; m_position = m_BOMLength; } if (m_foundBOM) // has one { if (m_BOMEncoding == System.Text.Encoding.UTF8) { // no extra processing needed for UTF8 - this is the default format } else if (m_BOMEncoding == System.Text.Encoding.Unicode || m_BOMEncoding == System.Text.Encoding.BigEndianUnicode) { System.Text.UTF8Encoding utf8Encoder = new System.Text.UTF8Encoding(false, true); try { // decode the wide Unicode byte data to wide chars System.Text.Decoder uniDecoder = m_BOMEncoding.GetDecoder(); int charCount = uniDecoder.GetCharCount(m_FileData, 2, m_FileData.Length-2); char[] chars = new Char[charCount]; int charsDecodedCount = uniDecoder.GetChars(m_FileData, 2, m_FileData.Length-2, chars, 0); // decode the wide chars to utf8 bytes int newLength = utf8Encoder.GetByteCount(chars); m_FileData = new byte[newLength]; utf8Encoder.GetBytes(chars, 0, chars.Length, m_FileData, 0); // log msg for user to see if (m_Log != null) m_Log.AddWarning(String.Format(Sfm2XmlStrings.FileConvertedFrom0To1, m_BOMEncoding.WebName, utf8Encoder.WebName)); } catch (System.Exception e) { if (m_Log != null) { m_Log.AddFatalError(String.Format(Sfm2XmlStrings.CannotConvertFileFrom0To1, m_BOMEncoding.WebName, utf8Encoder.WebName)); m_Log.AddFatalError(String.Format(Sfm2XmlStrings.Exception0, e.Message)); } m_position = 0; m_FileData = new byte[0]; // don't process anything } } else { m_position = 0; m_FileData = new byte[0]; // don't process anything if (m_Log != null) m_Log.AddFatalError(Sfm2XmlStrings.CannotProcessUtf32Files); } } }
/// <summary> /// This will look at the start of the file for the BOM and process it /// if it's found. /// </summary> private void CheckforAndHandleBOM() { byte[] utf32be = new byte[] { 0x00, 0x00, 0xfe, 0xff }; byte[] utf32le = new byte[] { 0xff, 0xfe, 0x00, 0x00 }; byte[] utf16be = new byte[] { 0xfe, 0xff }; byte[] utf16le = new byte[] { 0xff, 0xfe }; byte[] utf8 = new byte[] { 0xef, 0xbb, 0xbf }; if (IsCurrentData(utf32le) || IsCurrentData(utf32be)) { m_foundBOM = true; m_BOMLength = utf32be.Length; //m_BOMEncoding = System.Text.Encoding.???; // not currently defined for 32 m_position = m_BOMLength; } else if (IsCurrentData(utf8)) { m_foundBOM = true; m_BOMLength = utf8.Length; m_BOMEncoding = System.Text.Encoding.UTF8; m_position = m_BOMLength; } else if (IsCurrentData(utf16le)) { m_foundBOM = true; m_BOMLength = utf16le.Length; m_BOMEncoding = System.Text.Encoding.Unicode; m_position = m_BOMLength; } else if (IsCurrentData(utf16be)) { m_foundBOM = true; m_BOMLength = utf16be.Length; m_BOMEncoding = System.Text.Encoding.BigEndianUnicode; m_position = m_BOMLength; } if (m_foundBOM) // has one { if (m_BOMEncoding == System.Text.Encoding.UTF8) { // no extra processing needed for UTF8 - this is the default format } else if (m_BOMEncoding == System.Text.Encoding.Unicode || m_BOMEncoding == System.Text.Encoding.BigEndianUnicode) { System.Text.UTF8Encoding utf8Encoder = new System.Text.UTF8Encoding(false, true); try { // decode the wide Unicode byte data to wide chars System.Text.Decoder uniDecoder = m_BOMEncoding.GetDecoder(); int charCount = uniDecoder.GetCharCount(m_FileData, 2, m_FileData.Length - 2); char[] chars = new Char[charCount]; uniDecoder.GetChars(m_FileData, 2, m_FileData.Length - 2, chars, 0); // decode the wide chars to utf8 bytes int newLength = utf8Encoder.GetByteCount(chars); m_FileData = new byte[newLength]; utf8Encoder.GetBytes(chars, 0, chars.Length, m_FileData, 0); // log msg for user to see if (m_Log != null) { m_Log.AddWarning(String.Format(Sfm2XmlStrings.FileConvertedFrom0To1, m_BOMEncoding.WebName, utf8Encoder.WebName)); } } catch (System.Exception e) { if (m_Log != null) { m_Log.AddFatalError(String.Format(Sfm2XmlStrings.CannotConvertFileFrom0To1, m_BOMEncoding.WebName, utf8Encoder.WebName)); m_Log.AddFatalError(String.Format(Sfm2XmlStrings.Exception0, e.Message)); } m_position = 0; m_FileData = new byte[0]; // don't process anything } } else { m_position = 0; m_FileData = new byte[0]; // don't process anything if (m_Log != null) { m_Log.AddFatalError(Sfm2XmlStrings.CannotProcessUtf32Files); } } } }