diff --git a/PasswordChanger1C/DatabaseAccess838.vb b/PasswordChanger1C/DatabaseAccess838.vb index 11f954c..53cf0d6 100644 --- a/PasswordChanger1C/DatabaseAccess838.vb +++ b/PasswordChanger1C/DatabaseAccess838.vb @@ -238,23 +238,29 @@ Module DatabaseAccess838 End Sub - Private Function GetCleanDataFromBlob(Dataindex As Integer, Datasize As Integer, bytesBlock As Byte()) As Byte() + Private Function GetCleanDataFromBlob(Dataindex As Integer, Datasize As Integer, bytesBlock As Byte(), Optional ByRef DataPositions As Integer() = Nothing) As Byte() Dim NextBlock = 999 'any number gt 0 Dim Pos = Dataindex * 256 Dim ByteBlock() As Byte = New Byte(Datasize - 1) {} Dim i = 0 + Dim BlocksCount = 0 + While NextBlock > 0 NextBlock = BitConverter.ToInt32(bytesBlock, Pos) Dim BlockSize = BitConverter.ToInt16(bytesBlock, Pos + 4) + ReDim Preserve DataPositions(BlocksCount) + DataPositions(BlocksCount) = Pos + 6 + For j = 0 To BlockSize - 1 ByteBlock(i) = bytesBlock(Pos + 6 + j) i = i + 1 Next Pos = NextBlock * 256 + BlocksCount = BlocksCount + 1 End While @@ -343,21 +349,60 @@ Module DatabaseAccess838 Dim BlobPage As PageParams = ReadObjectPageDefinition(reader, BytesBlobBlock, PageSize) BlobPage.BinaryData = ReadAllStoragePagesForObject(reader, BlobPage) + reader.Close() - Dim BytesValTemp = GetCleanDataFromBlob(DataPos, DataSize, BlobPage.BinaryData) + Dim DataPositions As Integer() = Nothing + Dim BytesValTemp = GetCleanDataFromBlob(DataPos, DataSize, BlobPage.BinaryData, DataPositions) If BytesValTemp.SequenceEqual(OldData) Then + If OldData.Count = NewData.Count Then + + Dim CurrentByte = 0 + 'Data is stored in 256 bytes blocks (6 bytes reserved for next block number and size) + For Each Position In DataPositions + For i = 0 To 249 + If CurrentByte > NewData.Count - 1 Then + Exit For + End If + + Dim NewPosition = Position + i + BlobPage.BinaryData(NewPosition) = NewData(CurrentByte) + + CurrentByte = CurrentByte + 1 + Next + Next + + 'Blob page(s) has been modified. Let's write it back to database + fs = New FileStream(FileName, FileMode.Open, FileAccess.ReadWrite, FileShare.Write) + Dim writer As New BinaryWriter(fs) + + CurrentByte = 0 + For Each Position In BlobPage.PagesNum + + Dim TempBlock() As Byte = New Byte(PageSize - 1) {} + For j = 0 To PageSize - 1 + TempBlock(j) = BlobPage.BinaryData(CurrentByte) + CurrentByte = CurrentByte + 1 + Next + + writer.Seek(Position * PageSize, SeekOrigin.Begin) + writer.Write(TempBlock) + + Next + writer.Close() + Else + Throw New System.Exception("Новый байтовый массив должен совпадать по размерам со старым массивом (т.к. мы только заменяем хэши одинаковой длины)." + + vbNewLine + "Сообщите пожалуйста об этой ошибке!") + End If + Else - reader.Close() Throw New System.Exception("Информация в БД была изменена другим процессом! Прочитайте список пользователей заново.") End If - reader.Close() - End Sub diff --git a/PasswordChanger1C/Form1.vb b/PasswordChanger1C/Form1.vb index 4d43295..aaf8446 100644 --- a/PasswordChanger1C/Form1.vb +++ b/PasswordChanger1C/Form1.vb @@ -98,8 +98,6 @@ Public Class MainForm For Each Row In TableParams.Records - - If Row("NAME").ToString = "" Then Row.Add("UserGuidStr", "") Row.Add("UserPassHash", "") @@ -115,11 +113,12 @@ Public Class MainForm Row.Add("UserGuidStr", G.ToString) + 'pretty crapy code here.. If AuthStructure(0)(7) = "0" Then Row.Add("UserPassHash", "") Row.Add("UserPassHash2", "") Else - If AuthStructure(0).Count = 17 Then + If AuthStructure(0).Count = 17 Or TableParams.DatabaseVersion = "8.3.8" Then Row.Add("UserPassHash", AuthStructure(0)(11)) Row.Add("UserPassHash2", AuthStructure(0)(12)) Else @@ -440,11 +439,12 @@ Public Class MainForm Str = Str + vbNewLine + Row("NAME").ToString Dim NewHash = CommonModule.EncryptStringSHA1(NewPassword.Text.Trim) + Dim NewHash2 = CommonModule.EncryptStringSHA1(NewPassword.Text.Trim.ToUpper) Dim OldDataBinary = Row("DATA_BINARY") Dim OldData = Row("DATA").ToString Dim NewData = OldData.Replace(Row("UserPassHash"), """" + NewHash + """") - NewData = NewData.Replace(Row("UserPassHash2"), """" + NewHash + """") + NewData = NewData.Replace(Row("UserPassHash2"), """" + NewHash2 + """") Dim NewBytes = CommonModule.EncodePasswordStructure(NewData, Row("DATA_KEYSIZE"), Row("DATA_KEY"))