internal void ParseExistentData() { if (0 == _workBuffer.Size) { return; } DataChunk[] chunks = DataChunk.ParseData(_workBuffer.Data, _workBuffer.Size); int chunksNum = chunks.Length, i = 0; try { for (; i < chunksNum; i++) { LineInfo line = GetCurrentLine(); if (LineInfoState.Completed == line.State) { MoveToNextLine(); line = GetCurrentLine(); } try { line.PutChunk(_workBuffer.Data, chunks[i]); } catch (LineException e) { e.Line = GetCurrentLineIndex(); throw; } if (LineInfoState.Completed == line.State) { OnNewLine(line, GetCurrentLineIndex()); if (true == SingleLineMode && (i != chunksNum - 1)) { break; } } } } catch { _invalidState = true; throw; } if (chunksNum == i) //all working data was parsed? { _workBuffer.Clear(); } else { _workBuffer.CutHead(chunks[i].Start + chunks[i].Length); } }
public EBMLElement(Matroska.File _file, ulong position) { if (_file == null) { throw new ArgumentNullException("file"); } if (position > (ulong)(_file.Length - 4)) { throw new ArgumentOutOfRangeException("position"); } file = _file; file.Seek((long)position); ByteVector vector = file.ReadBlock(1); Byte header_byte = vector[0]; Byte mask = 0x80, id_length = 1; while (id_length <= 4 && (header_byte & mask) == 0) { id_length++; mask >>= 1; } if (id_length > 4) { throw new CorruptFileException("invalid EBML id size"); } if (id_length > 1) { vector.Add(file.ReadBlock(id_length - 1)); } ebml_id = vector.ToUInt(); vector.Clear(); vector = file.ReadBlock(1); header_byte = vector[0]; mask = 0x80; Byte size_length = 1; while (size_length <= 8 && (header_byte & mask) == 0) { size_length++; mask >>= 1; } if (size_length > 8) { throw new CorruptFileException("invalid EBML element size"); } vector[0] &= (Byte)(mask - 1); if (size_length > 1) { vector.Add(file.ReadBlock(size_length - 1)); } ebml_size = vector.ToULong(); offset = position; data_offset = offset + id_length + size_length; }
/// <summary> /// Read EBML header and data-size if it is an abstract one. /// It then becomes a non abstract EBML. /// </summary> /// <param name="throwException">Throw exception on invalid EBML read if true (Default: false).</param> /// <returns>True if successful.</returns> public bool Read(bool throwException = false) { if (!Abstract) { return(true); } if (file == null) { throw new ArgumentNullException("file"); } try { var ex = new InvalidOperationException("Invalid EBML format Read"); if (offset >= (ulong)(file.Length) - 1) { throw ex; } // Prepare for Consitency check uint ebml_id_check = ebml_id; ulong ebml_size_check = Size; file.Seek((long)offset); // Get the header byte ByteVector vector = file.ReadBlock(1); byte header_byte = vector[0]; // Define a mask byte mask = 0x80, id_length = 1; // Figure out the size in bytes while (id_length <= 4 && (header_byte & mask) == 0) { id_length++; mask >>= 1; } if (id_length > 4) { throw ex; } // Now read the rest of the EBML ID if (id_length > 1) { vector.Add(file.ReadBlock(id_length - 1)); } ebml_id = vector.ToUInt(); vector.Clear(); // Get the size length vector = file.ReadBlock(1); header_byte = vector[0]; mask = 0x80; Byte size_length = 1; // Iterate through various possibilities while (size_length <= 8 && (header_byte & mask) == 0) { size_length++; mask >>= 1; } if (size_length > 8) { size_length = 1; // Special: Empty element (all zero state) } else { vector[0] &= (Byte)(mask - 1); // Clear the marker bit } // Now read the rest of the EBML element size if (size_length > 1) { vector.Add(file.ReadBlock(size_length - 1)); } ebml_size = vector.ToULong(); // Special: Auto-size (0xFF byte) if (size_length == 1 && ebml_size == 0x7F) { // Resolve auto-size to fill in to its containing element ulong bound = parent == null ? (ulong)file.Length : parent.Offset + parent.Size; ebml_size = bound - offset - (ulong)(id_length + size_length); } data_offset = offset + id_length + size_length; // Consistency check: Detect descrepencies between read data and abstract data if (ebml_id_check != 0 && ebml_id_check != ebml_id) { throw ex; } if (ebml_size_check != 0 && ebml_size_check != Size) { throw ex; } return(true); } catch (Exception ex) { if (throwException) { throw ex; } return(false); } }
/// <summary> /// Constructs a <see cref="EBMLElement" /> parsing from provided /// file data. /// </summary> /// <param name="_file"><see cref="File" /> instance to read from.</param> /// <param name="position">Position to start reading from.</param> public EBMLElement(Matroska.File _file, ulong position) { if (_file == null) { throw new ArgumentNullException("file"); } if (position > (ulong)(_file.Length - 4)) { throw new ArgumentOutOfRangeException("position"); } // Keep a reference to the file file = _file; file.Seek((long)position); // Get the header byte ByteVector vector = file.ReadBlock(1); Byte header_byte = vector [0]; // Define a mask Byte mask = 0x80, id_length = 1; // Figure out the size in bytes while (id_length <= 4 && (header_byte & mask) == 0) { id_length++; mask >>= 1; } if (id_length > 4) { throw new CorruptFileException("invalid EBML id size"); } // Now read the rest of the EBML ID if (id_length > 1) { vector.Add(file.ReadBlock(id_length - 1)); } ebml_id = vector.ToUInt(); vector.Clear(); // Get the size length vector = file.ReadBlock(1); header_byte = vector [0]; mask = 0x80; Byte size_length = 1; // Iterate through various possibilities while (size_length <= 8 && (header_byte & mask) == 0) { size_length++; mask >>= 1; } if (size_length > 8) { throw new CorruptFileException("invalid EBML element size"); } // Clear the marker bit vector [0] &= (Byte)(mask - 1); // Now read the rest of the EBML element size if (size_length > 1) { vector.Add(file.ReadBlock(size_length - 1)); } ebml_size = vector.ToULong(); offset = position; data_offset = offset + id_length + size_length; }
public static string MD5Encode(string pw, string salt) { // ��� �������� �������� �����: // $apr1$Vs5.....$iSQlpTkND9RjL7iAMTjDt. // ��� ������������� ������ �������� ��������� ������ - ���� string password; byte[] final; // ����� ����, ���� � �������� ���� ������ ��� ��� // 1. ����� ���������� $apr1$ if (salt.StartsWith(MD5_MAGIC_PREFIX)) { salt = salt.Substring(MD5_MAGIC_PREFIX.Length); } // 2. ����� ���� �� ������� '$' ��� 8 �������� int sp = salt.IndexOf('$'); if (sp < 0 || sp > 8) { sp = 8; } salt = salt.Substring(0, sp); //Debug.WriteLine(string.Format("salt [{0}]", salt)); ByteVector s = new ByteVector(); ByteVector s1 = new ByteVector(); s.Add(pw); s.Add(MD5_MAGIC_PREFIX); s.Add(salt); s1.Add(pw); s1.Add(salt); s1.Add(pw); final = s1.GetMD5Hash(); for (int i = pw.Length; i > 0; i -= MD5_DIGESTSIZE) { s.Add(final, 0, (i > MD5_DIGESTSIZE) ? MD5_DIGESTSIZE : i); } for (int i = 0; i < final.Length; i++) { final[i] = 0; } for (int i = pw.Length; i != 0; i >>= 1) { // (i & 1) � ����� if ((i & 0x01) == 1) { s.Add(final, 0, 1); } else { s.Add(pw.Substring(0, 1)); } } final = s.GetMD5Hash(); for (int i = 0; i < 1000; i++) { s1.Clear(); if ((i & 1) != 0) { s1.Add(pw); } else { s1.Add(final); } if ((i % 3) != 0) { s1.Add(salt); } if ((i % 7) != 0) { s1.Add(pw); } if ((i & 1) != 0) { s1.Add(final); } else { s1.Add(pw); } final = s1.GetMD5Hash(); } password = ""; ulong l; l = ((ulong)final[0] << 16) | ((ulong)final[6] << 8) | ((ulong)final[12]); password += PasswdHelper.to64(l, 4); l = ((ulong)final[1] << 16) | ((ulong)final[7] << 8) | ((ulong)final[13]); password += PasswdHelper.to64(l, 4); l = ((ulong)final[2] << 16) | ((ulong)final[8] << 8) | ((ulong)final[14]); password += PasswdHelper.to64(l, 4); l = ((ulong)final[3] << 16) | ((ulong)final[9] << 8) | ((ulong)final[15]); password += PasswdHelper.to64(l, 4); l = ((ulong)final[4] << 16) | ((ulong)final[10] << 8) | ((ulong)final[5]); password += PasswdHelper.to64(l, 4); l = ((ulong)final[11]); password += PasswdHelper.to64(l, 2); password = string.Format("{0}{1}${2}", MD5_MAGIC_PREFIX, salt, password); return(password); }
public static string MD5Encode(string pw, string salt) { // для проверки приходит такое: // $apr1$Vs5.....$iSQlpTkND9RjL7iAMTjDt. // для генерирования пароля приходит случайная строка - соль string password; byte[] final; // найдём соль, если в качестве соли пришёл уже хэш // 1. Уберём магический $apr1$ if (salt.StartsWith(MD5_MAGIC_PREFIX)) { salt = salt.Substring(MD5_MAGIC_PREFIX.Length); } // 2. Найдём соль до первого '$' Или 8 символов int sp = salt.IndexOf('$'); if (sp < 0 || sp > 8) { sp = 8; } salt = salt.Substring(0, sp); //Debug.WriteLine(string.Format("salt [{0}]", salt)); ByteVector s = new ByteVector(); ByteVector s1 = new ByteVector(); s.Add(pw); s.Add(MD5_MAGIC_PREFIX); s.Add(salt); s1.Add(pw); s1.Add(salt); s1.Add(pw); final = s1.GetMD5Hash(); for (int i = pw.Length; i > 0; i -= MD5_DIGESTSIZE) { s.Add(final, 0, (i > MD5_DIGESTSIZE) ? MD5_DIGESTSIZE : i); } for (int i = 0; i < final.Length; i++) { final[i] = 0; } for (int i = pw.Length; i != 0; i >>= 1) { // (i & 1) в апаче if ((i & 0x01) == 1) { s.Add(final, 0, 1); } else { s.Add(pw.Substring(0, 1)); } } final = s.GetMD5Hash(); for (int i = 0; i < 1000; i++) { s1.Clear(); if ((i & 1) != 0) { s1.Add(pw); } else { s1.Add(final); } if ((i % 3) != 0) { s1.Add(salt); } if ((i % 7) != 0) { s1.Add(pw); } if ((i & 1) != 0) { s1.Add(final); } else { s1.Add(pw); } final = s1.GetMD5Hash(); } password = ""; ulong l; l = ((ulong)final[0] << 16) | ((ulong)final[6] << 8) | ((ulong)final[12]); password += PasswdHelper.to64(l, 4); l = ((ulong)final[1] << 16) | ((ulong)final[7] << 8) | ((ulong)final[13]); password += PasswdHelper.to64(l, 4); l = ((ulong)final[2] << 16) | ((ulong)final[8] << 8) | ((ulong)final[14]); password += PasswdHelper.to64(l, 4); l = ((ulong)final[3] << 16) | ((ulong)final[9] << 8) | ((ulong)final[15]); password += PasswdHelper.to64(l, 4); l = ((ulong)final[4] << 16) | ((ulong)final[10] << 8) | ((ulong)final[5]); password += PasswdHelper.to64(l, 4); l = ((ulong)final[11]); password += PasswdHelper.to64(l, 2); password = string.Format("{0}{1}${2}", MD5_MAGIC_PREFIX, salt, password); return(password); }
public static string MD5Encode(string pw, string salt) { // для проверки приходит такое: // $apr1$Vs5.....$iSQlpTkND9RjL7iAMTjDt. // для генерирования пароля приходит случайная строка - соль string password; byte[] final; // найдём соль, если в качестве соли пришёл уже хэш // 1. Уберём магический $apr1$ if (salt.StartsWith(MD5_MAGIC_PREFIX)) { salt = salt.Substring(MD5_MAGIC_PREFIX.Length); } // 2. Найдём соль до первого '$' Или 8 символов int sp = salt.IndexOf('$'); if (sp < 0 || sp > 8) sp = 8; salt = salt.Substring(0, sp); //Debug.WriteLine(string.Format("salt [{0}]", salt)); ByteVector s = new ByteVector(); ByteVector s1 = new ByteVector(); s.Add(pw); s.Add(MD5_MAGIC_PREFIX); s.Add(salt); s1.Add(pw); s1.Add(salt); s1.Add(pw); final = s1.GetMD5Hash(); for (int i = pw.Length; i > 0; i -= MD5_DIGESTSIZE) { s.Add(final, 0, (i > MD5_DIGESTSIZE) ? MD5_DIGESTSIZE : i); } for (int i = 0; i < final.Length; i++) final[i] = 0; for (int i = pw.Length; i != 0; i >>= 1) { // (i & 1) в апаче if ((i & 0x01) == 1) { s.Add(final, 0, 1); } else { s.Add(pw.Substring(0, 1)); } } final = s.GetMD5Hash(); for (int i = 0; i < 1000; i++) { s1.Clear(); if ((i & 1) != 0) { s1.Add(pw); } else { s1.Add(final); } if ((i % 3) != 0) { s1.Add(salt); } if ((i % 7) != 0) { s1.Add(pw); } if ((i & 1) != 0) { s1.Add(final); } else { s1.Add(pw); } final = s1.GetMD5Hash(); } password = ""; ulong l; l = ((ulong)final[0] << 16) | ((ulong)final[6] << 8) | ((ulong)final[12]); password += PasswdHelper.to64(l, 4); l = ((ulong)final[1] << 16) | ((ulong)final[7] << 8) | ((ulong)final[13]); password += PasswdHelper.to64(l, 4); l = ((ulong)final[2] << 16) | ((ulong)final[8] << 8) | ((ulong)final[14]); password += PasswdHelper.to64(l, 4); l = ((ulong)final[3] << 16) | ((ulong)final[9] << 8) | ((ulong)final[15]); password += PasswdHelper.to64(l, 4); l = ((ulong)final[4] << 16) | ((ulong)final[10] << 8) | ((ulong)final[5]); password += PasswdHelper.to64(l, 4); l = ((ulong)final[11]); password += PasswdHelper.to64(l, 2); password = string.Format("{0}{1}${2}", MD5_MAGIC_PREFIX, salt, password); return password; }