コード例 #1
0
        private async void startVerifyButton_Click(object sender, EventArgs e)
        {
            progressBar1.Value = 25;
            List <FullStudent> students = await GetAllFullStudents();

            progressBar1.Value = 50;

            Console.WriteLine($"Total records: {students.Count}");

            string student1 = "";
            string student2 = "";


            numOfRecords.Text = students.Count.ToString();

            // now that we have the fullstudent records, we can start the verfication of them all
            BasicStudent genesisStudent = StudentMapper.GenesisStudentNode();


            // start at the end of the records and then recalculate the hash of the node and check if it matches with the one on record
            // then recalculate the hash of the previous node and check to see if it matches with the currents previous on record
            // if both are valid, then the record is considered to be valid
            // otherwise, the record is invalid


            // Print some debug info alongside
            bool valid = true;

            for (int i = students.Count - 1; i >= 0; i--)
            {
                bool currentHashMatch      = true;
                bool previousHashMatch     = true;
                bool previousFullHashMatch = true;

                FullStudent currentFullStudent = students[i];

                Console.WriteLine($"Current Student: {currentFullStudent.FirstName}");
                string recalculatedCurrentNodeHash = CalculateCurrentFullStudentHash(currentFullStudent);

                if (recalculatedCurrentNodeHash != currentFullStudent.CurrentNodeHash)
                {
                    currentHashMatch = false;
                }

                if (i == 0)
                {
                    Console.WriteLine($"Genesis Node Previous: Test");
                    string previousGenesisHash = Hash.GetHashString("Test");

                    if (currentFullStudent.PreviousRecordHash != previousGenesisHash)
                    {
                        previousHashMatch = false;
                    }

                    if (previousGenesisHash != currentFullStudent.PreviousFullRecordHash)
                    {
                        previousFullHashMatch = false;
                    }
                }
                else
                {
                    FullStudent previousFullStudent = students[i - 1];
                    Console.WriteLine($"Previous Student: {previousFullStudent.FirstName}");
                    string recalculatedPreviousNodeHash = CalculateCurrentFullStudentHash(previousFullStudent);

                    if (currentFullStudent.PreviousRecordHash != recalculatedPreviousNodeHash)
                    {
                        previousHashMatch = false;
                        student2          = previousFullStudent.FirstName;
                    }
                    // new
                    string previousFullHashObject = JsonConvert.SerializeObject(previousFullStudent);
                    string previousFullHash       = Hash.GetHashString(previousFullHashObject);

                    if (previousFullHash != currentFullStudent.PreviousFullRecordHash)
                    {
                        previousFullHashMatch = false;
                    }
                }

                if (!currentHashMatch || !previousHashMatch || !previousFullHashMatch)    // also check the fullnodehashmatch
                {
                    valid    = false;
                    student1 = currentFullStudent.FirstName;
                    break;
                }
            }
            progressBar1.Value = 100;

            if (valid)
            {
                MessageBox.Show("Hash Verified and Unmodified", "Success");
            }
            else
            {
                if (student2.Length == 0)
                {
                    MessageBox.Show($"Hash mismatch on {student1}", "Error");
                }
                else
                {
                    MessageBox.Show($"Hash mismatch on {student1} and {student2}", "Error");
                }
            }
        }