PHP SHA 256 Not Producing Expected Output












-1















I have been working a couple of months on some PHP code to generate a hash value from some data input. The task is to take two transaction hashes and hash the two transactions. I am using the sha256 bit algorithm library to do so. The problem I am noticing is the expected result is not coming out.



I tried to:




  1. check if the variables were strings (Note: hash() function requires a string for both the algorithm and input variable.

  2. The code for the hash function input was output correctly.


I am at a loss as of what the problem is.
Also, I am using files to read in and input the results into an array because PHP refreshes the page thus resulting in losing the data.



The function in PHP I am having trouble with is shown below:



function printTransactions($ArrayName, $Name){
$counter = 1;
$BlockNumber = 0;
$HashesFromFileArray = readfileToArray($Name."Hash.txt");
array_pop($HashesFromFileArray);
foreach ($ArrayName as $LineFromFile){
PrintoutToUser("yellow", "3", "Transaction " . $counter . " <font color='white'>" . $LineFromFile . "</font>");
If ($counter % 2 == 0) {
$BlockNumber = $BlockNumber + 1;
if(isset($HashesFromFileArray[$counter -1])=="1" && isset($HashesFromFileArray[$counter -2])=="1" && gettype($HashesFromFileArray[$counter -1])!="NULL"){
if(strlen($HashesFromFileArray[$counter -1])>=66 && strlen($HashesFromFileArray[$counter -2])>=66){
$HashValueOfBothTransactions = GetNewHash($HashesFromFileArray[$counter -2].$HashesFromFileArray[$counter -1], "sha256");
$TransactionInputCounter = $counter-1;
$TransactionInputCounter2 = $counter;
PrintoutToUser("green", "3", "T". $TransactionInputCounter.":" . $HashesFromFileArray[$counter -2]);
PrintoutToUser("green", "3", "T". $TransactionInputCounter2.":" . $HashesFromFileArray[$counter -1]);
PrintoutToUser("blue", "3", "Data Hashed " . $HashesFromFileArray[$counter -2].$HashesFromFileArray[$counter -1]." Length of Array input 1 and 2 is " . strLen($HashesFromFileArray[$counter -2]) . " and " . strLen($HashesFromFileArray[$counter -2]));
PrintoutToUser("red", "3", "End of Block: " .$BlockNumber . ":". $HashValueOfBothTransactions . "<br />");
writeToFileDataPlain("TESTING", $HashValueOfBothTransactions);
}
}
}
$counter = $counter + 1;
}
}


If I was to put in the following:



Blockchain Name: Sean



Sender: Sean



Amount: 2



Receiver: Bob



Nonce: 2



I would expect it to be 6a3ea3befedebacdce2692e91623b23e8e74b9bfff2eedaf4db2470fb3efc6db
after setting two transactions.



I instead get 90189390a229292338f2d6a2e98cec46751ce0ae6b18fa0d3be56681c8cedf44 which is incorrect.



I am not sure why this is occurring and how to fix it at the moment.



My full program can be found at https://github.com/seansanders/PHPSHABlockchain/blob/master/LedgerProgramV4.php



Note, I have left the troubleshooting comments in that are color-coded green for the user.



Some other suspicious things have also occurred in the program:



1) The two transactions hashes being hashed are not producing the correct hash value that is expected.



2) I noticed that the Test File I am writing to is putting more data than expected after submitting three transactions. I think this is a problem directly with the if statements and I am going wrong somewhere.



I have tried my best to adhere to best coding practices and make the code as easy to read as possible. Any help would be greatly appreciated.










share|improve this question

























  • You may need to go to Code Review in StackExchange...

    – Geno Chen
    Dec 28 '18 at 20:09






  • 1





    @GenoChen Code Review is to have experts review working code, his code does not give the expected output, therefore Code Review would not be a good place for this post.

    – GrumpyCrouton
    Dec 28 '18 at 20:12






  • 2





    Your code is very confuse to understand. You could try to debug and narrow down to a few lines of problem. I advise you to read about PHP PSR's and Clean Code. It will really help you to write better code and therefore isolate the logic and eventually some problems.

    – Felippe Duarte
    Dec 28 '18 at 20:16











  • Few people, if any, are going to take the time to review 100+ lines of mostly unrelated code to find the relevant bits. Reduce your code to the absolute minimum required to reproduce the issue. stackoverflow.com/help/mcve

    – Sammitch
    Dec 28 '18 at 21:07


















-1















I have been working a couple of months on some PHP code to generate a hash value from some data input. The task is to take two transaction hashes and hash the two transactions. I am using the sha256 bit algorithm library to do so. The problem I am noticing is the expected result is not coming out.



I tried to:




  1. check if the variables were strings (Note: hash() function requires a string for both the algorithm and input variable.

  2. The code for the hash function input was output correctly.


I am at a loss as of what the problem is.
Also, I am using files to read in and input the results into an array because PHP refreshes the page thus resulting in losing the data.



The function in PHP I am having trouble with is shown below:



function printTransactions($ArrayName, $Name){
$counter = 1;
$BlockNumber = 0;
$HashesFromFileArray = readfileToArray($Name."Hash.txt");
array_pop($HashesFromFileArray);
foreach ($ArrayName as $LineFromFile){
PrintoutToUser("yellow", "3", "Transaction " . $counter . " <font color='white'>" . $LineFromFile . "</font>");
If ($counter % 2 == 0) {
$BlockNumber = $BlockNumber + 1;
if(isset($HashesFromFileArray[$counter -1])=="1" && isset($HashesFromFileArray[$counter -2])=="1" && gettype($HashesFromFileArray[$counter -1])!="NULL"){
if(strlen($HashesFromFileArray[$counter -1])>=66 && strlen($HashesFromFileArray[$counter -2])>=66){
$HashValueOfBothTransactions = GetNewHash($HashesFromFileArray[$counter -2].$HashesFromFileArray[$counter -1], "sha256");
$TransactionInputCounter = $counter-1;
$TransactionInputCounter2 = $counter;
PrintoutToUser("green", "3", "T". $TransactionInputCounter.":" . $HashesFromFileArray[$counter -2]);
PrintoutToUser("green", "3", "T". $TransactionInputCounter2.":" . $HashesFromFileArray[$counter -1]);
PrintoutToUser("blue", "3", "Data Hashed " . $HashesFromFileArray[$counter -2].$HashesFromFileArray[$counter -1]." Length of Array input 1 and 2 is " . strLen($HashesFromFileArray[$counter -2]) . " and " . strLen($HashesFromFileArray[$counter -2]));
PrintoutToUser("red", "3", "End of Block: " .$BlockNumber . ":". $HashValueOfBothTransactions . "<br />");
writeToFileDataPlain("TESTING", $HashValueOfBothTransactions);
}
}
}
$counter = $counter + 1;
}
}


If I was to put in the following:



Blockchain Name: Sean



Sender: Sean



Amount: 2



Receiver: Bob



Nonce: 2



I would expect it to be 6a3ea3befedebacdce2692e91623b23e8e74b9bfff2eedaf4db2470fb3efc6db
after setting two transactions.



I instead get 90189390a229292338f2d6a2e98cec46751ce0ae6b18fa0d3be56681c8cedf44 which is incorrect.



I am not sure why this is occurring and how to fix it at the moment.



My full program can be found at https://github.com/seansanders/PHPSHABlockchain/blob/master/LedgerProgramV4.php



Note, I have left the troubleshooting comments in that are color-coded green for the user.



Some other suspicious things have also occurred in the program:



1) The two transactions hashes being hashed are not producing the correct hash value that is expected.



2) I noticed that the Test File I am writing to is putting more data than expected after submitting three transactions. I think this is a problem directly with the if statements and I am going wrong somewhere.



I have tried my best to adhere to best coding practices and make the code as easy to read as possible. Any help would be greatly appreciated.










share|improve this question

























  • You may need to go to Code Review in StackExchange...

    – Geno Chen
    Dec 28 '18 at 20:09






  • 1





    @GenoChen Code Review is to have experts review working code, his code does not give the expected output, therefore Code Review would not be a good place for this post.

    – GrumpyCrouton
    Dec 28 '18 at 20:12






  • 2





    Your code is very confuse to understand. You could try to debug and narrow down to a few lines of problem. I advise you to read about PHP PSR's and Clean Code. It will really help you to write better code and therefore isolate the logic and eventually some problems.

    – Felippe Duarte
    Dec 28 '18 at 20:16











  • Few people, if any, are going to take the time to review 100+ lines of mostly unrelated code to find the relevant bits. Reduce your code to the absolute minimum required to reproduce the issue. stackoverflow.com/help/mcve

    – Sammitch
    Dec 28 '18 at 21:07
















-1












-1








-1








I have been working a couple of months on some PHP code to generate a hash value from some data input. The task is to take two transaction hashes and hash the two transactions. I am using the sha256 bit algorithm library to do so. The problem I am noticing is the expected result is not coming out.



I tried to:




  1. check if the variables were strings (Note: hash() function requires a string for both the algorithm and input variable.

  2. The code for the hash function input was output correctly.


I am at a loss as of what the problem is.
Also, I am using files to read in and input the results into an array because PHP refreshes the page thus resulting in losing the data.



The function in PHP I am having trouble with is shown below:



function printTransactions($ArrayName, $Name){
$counter = 1;
$BlockNumber = 0;
$HashesFromFileArray = readfileToArray($Name."Hash.txt");
array_pop($HashesFromFileArray);
foreach ($ArrayName as $LineFromFile){
PrintoutToUser("yellow", "3", "Transaction " . $counter . " <font color='white'>" . $LineFromFile . "</font>");
If ($counter % 2 == 0) {
$BlockNumber = $BlockNumber + 1;
if(isset($HashesFromFileArray[$counter -1])=="1" && isset($HashesFromFileArray[$counter -2])=="1" && gettype($HashesFromFileArray[$counter -1])!="NULL"){
if(strlen($HashesFromFileArray[$counter -1])>=66 && strlen($HashesFromFileArray[$counter -2])>=66){
$HashValueOfBothTransactions = GetNewHash($HashesFromFileArray[$counter -2].$HashesFromFileArray[$counter -1], "sha256");
$TransactionInputCounter = $counter-1;
$TransactionInputCounter2 = $counter;
PrintoutToUser("green", "3", "T". $TransactionInputCounter.":" . $HashesFromFileArray[$counter -2]);
PrintoutToUser("green", "3", "T". $TransactionInputCounter2.":" . $HashesFromFileArray[$counter -1]);
PrintoutToUser("blue", "3", "Data Hashed " . $HashesFromFileArray[$counter -2].$HashesFromFileArray[$counter -1]." Length of Array input 1 and 2 is " . strLen($HashesFromFileArray[$counter -2]) . " and " . strLen($HashesFromFileArray[$counter -2]));
PrintoutToUser("red", "3", "End of Block: " .$BlockNumber . ":". $HashValueOfBothTransactions . "<br />");
writeToFileDataPlain("TESTING", $HashValueOfBothTransactions);
}
}
}
$counter = $counter + 1;
}
}


If I was to put in the following:



Blockchain Name: Sean



Sender: Sean



Amount: 2



Receiver: Bob



Nonce: 2



I would expect it to be 6a3ea3befedebacdce2692e91623b23e8e74b9bfff2eedaf4db2470fb3efc6db
after setting two transactions.



I instead get 90189390a229292338f2d6a2e98cec46751ce0ae6b18fa0d3be56681c8cedf44 which is incorrect.



I am not sure why this is occurring and how to fix it at the moment.



My full program can be found at https://github.com/seansanders/PHPSHABlockchain/blob/master/LedgerProgramV4.php



Note, I have left the troubleshooting comments in that are color-coded green for the user.



Some other suspicious things have also occurred in the program:



1) The two transactions hashes being hashed are not producing the correct hash value that is expected.



2) I noticed that the Test File I am writing to is putting more data than expected after submitting three transactions. I think this is a problem directly with the if statements and I am going wrong somewhere.



I have tried my best to adhere to best coding practices and make the code as easy to read as possible. Any help would be greatly appreciated.










share|improve this question
















I have been working a couple of months on some PHP code to generate a hash value from some data input. The task is to take two transaction hashes and hash the two transactions. I am using the sha256 bit algorithm library to do so. The problem I am noticing is the expected result is not coming out.



I tried to:




  1. check if the variables were strings (Note: hash() function requires a string for both the algorithm and input variable.

  2. The code for the hash function input was output correctly.


I am at a loss as of what the problem is.
Also, I am using files to read in and input the results into an array because PHP refreshes the page thus resulting in losing the data.



The function in PHP I am having trouble with is shown below:



function printTransactions($ArrayName, $Name){
$counter = 1;
$BlockNumber = 0;
$HashesFromFileArray = readfileToArray($Name."Hash.txt");
array_pop($HashesFromFileArray);
foreach ($ArrayName as $LineFromFile){
PrintoutToUser("yellow", "3", "Transaction " . $counter . " <font color='white'>" . $LineFromFile . "</font>");
If ($counter % 2 == 0) {
$BlockNumber = $BlockNumber + 1;
if(isset($HashesFromFileArray[$counter -1])=="1" && isset($HashesFromFileArray[$counter -2])=="1" && gettype($HashesFromFileArray[$counter -1])!="NULL"){
if(strlen($HashesFromFileArray[$counter -1])>=66 && strlen($HashesFromFileArray[$counter -2])>=66){
$HashValueOfBothTransactions = GetNewHash($HashesFromFileArray[$counter -2].$HashesFromFileArray[$counter -1], "sha256");
$TransactionInputCounter = $counter-1;
$TransactionInputCounter2 = $counter;
PrintoutToUser("green", "3", "T". $TransactionInputCounter.":" . $HashesFromFileArray[$counter -2]);
PrintoutToUser("green", "3", "T". $TransactionInputCounter2.":" . $HashesFromFileArray[$counter -1]);
PrintoutToUser("blue", "3", "Data Hashed " . $HashesFromFileArray[$counter -2].$HashesFromFileArray[$counter -1]." Length of Array input 1 and 2 is " . strLen($HashesFromFileArray[$counter -2]) . " and " . strLen($HashesFromFileArray[$counter -2]));
PrintoutToUser("red", "3", "End of Block: " .$BlockNumber . ":". $HashValueOfBothTransactions . "<br />");
writeToFileDataPlain("TESTING", $HashValueOfBothTransactions);
}
}
}
$counter = $counter + 1;
}
}


If I was to put in the following:



Blockchain Name: Sean



Sender: Sean



Amount: 2



Receiver: Bob



Nonce: 2



I would expect it to be 6a3ea3befedebacdce2692e91623b23e8e74b9bfff2eedaf4db2470fb3efc6db
after setting two transactions.



I instead get 90189390a229292338f2d6a2e98cec46751ce0ae6b18fa0d3be56681c8cedf44 which is incorrect.



I am not sure why this is occurring and how to fix it at the moment.



My full program can be found at https://github.com/seansanders/PHPSHABlockchain/blob/master/LedgerProgramV4.php



Note, I have left the troubleshooting comments in that are color-coded green for the user.



Some other suspicious things have also occurred in the program:



1) The two transactions hashes being hashed are not producing the correct hash value that is expected.



2) I noticed that the Test File I am writing to is putting more data than expected after submitting three transactions. I think this is a problem directly with the if statements and I am going wrong somewhere.



I have tried my best to adhere to best coding practices and make the code as easy to read as possible. Any help would be greatly appreciated.







php






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 2 at 16:00







seansanders

















asked Dec 28 '18 at 20:05









seansandersseansanders

278




278













  • You may need to go to Code Review in StackExchange...

    – Geno Chen
    Dec 28 '18 at 20:09






  • 1





    @GenoChen Code Review is to have experts review working code, his code does not give the expected output, therefore Code Review would not be a good place for this post.

    – GrumpyCrouton
    Dec 28 '18 at 20:12






  • 2





    Your code is very confuse to understand. You could try to debug and narrow down to a few lines of problem. I advise you to read about PHP PSR's and Clean Code. It will really help you to write better code and therefore isolate the logic and eventually some problems.

    – Felippe Duarte
    Dec 28 '18 at 20:16











  • Few people, if any, are going to take the time to review 100+ lines of mostly unrelated code to find the relevant bits. Reduce your code to the absolute minimum required to reproduce the issue. stackoverflow.com/help/mcve

    – Sammitch
    Dec 28 '18 at 21:07





















  • You may need to go to Code Review in StackExchange...

    – Geno Chen
    Dec 28 '18 at 20:09






  • 1





    @GenoChen Code Review is to have experts review working code, his code does not give the expected output, therefore Code Review would not be a good place for this post.

    – GrumpyCrouton
    Dec 28 '18 at 20:12






  • 2





    Your code is very confuse to understand. You could try to debug and narrow down to a few lines of problem. I advise you to read about PHP PSR's and Clean Code. It will really help you to write better code and therefore isolate the logic and eventually some problems.

    – Felippe Duarte
    Dec 28 '18 at 20:16











  • Few people, if any, are going to take the time to review 100+ lines of mostly unrelated code to find the relevant bits. Reduce your code to the absolute minimum required to reproduce the issue. stackoverflow.com/help/mcve

    – Sammitch
    Dec 28 '18 at 21:07



















You may need to go to Code Review in StackExchange...

– Geno Chen
Dec 28 '18 at 20:09





You may need to go to Code Review in StackExchange...

– Geno Chen
Dec 28 '18 at 20:09




1




1





@GenoChen Code Review is to have experts review working code, his code does not give the expected output, therefore Code Review would not be a good place for this post.

– GrumpyCrouton
Dec 28 '18 at 20:12





@GenoChen Code Review is to have experts review working code, his code does not give the expected output, therefore Code Review would not be a good place for this post.

– GrumpyCrouton
Dec 28 '18 at 20:12




2




2





Your code is very confuse to understand. You could try to debug and narrow down to a few lines of problem. I advise you to read about PHP PSR's and Clean Code. It will really help you to write better code and therefore isolate the logic and eventually some problems.

– Felippe Duarte
Dec 28 '18 at 20:16





Your code is very confuse to understand. You could try to debug and narrow down to a few lines of problem. I advise you to read about PHP PSR's and Clean Code. It will really help you to write better code and therefore isolate the logic and eventually some problems.

– Felippe Duarte
Dec 28 '18 at 20:16













Few people, if any, are going to take the time to review 100+ lines of mostly unrelated code to find the relevant bits. Reduce your code to the absolute minimum required to reproduce the issue. stackoverflow.com/help/mcve

– Sammitch
Dec 28 '18 at 21:07







Few people, if any, are going to take the time to review 100+ lines of mostly unrelated code to find the relevant bits. Reduce your code to the absolute minimum required to reproduce the issue. stackoverflow.com/help/mcve

– Sammitch
Dec 28 '18 at 21:07














1 Answer
1






active

oldest

votes


















0














I have found that the solution to the problem was to simply change to using a CSV file. The reason was that the text file was getting some funny characters that I suspect was due to the /r/n. Thanks to all who have helped.






share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53963765%2fphp-sha-256-not-producing-expected-output%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    I have found that the solution to the problem was to simply change to using a CSV file. The reason was that the text file was getting some funny characters that I suspect was due to the /r/n. Thanks to all who have helped.






    share|improve this answer




























      0














      I have found that the solution to the problem was to simply change to using a CSV file. The reason was that the text file was getting some funny characters that I suspect was due to the /r/n. Thanks to all who have helped.






      share|improve this answer


























        0












        0








        0







        I have found that the solution to the problem was to simply change to using a CSV file. The reason was that the text file was getting some funny characters that I suspect was due to the /r/n. Thanks to all who have helped.






        share|improve this answer













        I have found that the solution to the problem was to simply change to using a CSV file. The reason was that the text file was getting some funny characters that I suspect was due to the /r/n. Thanks to all who have helped.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Jan 5 at 22:07









        seansandersseansanders

        278




        278
































            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53963765%2fphp-sha-256-not-producing-expected-output%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            MongoDB - Not Authorized To Execute Command

            How to fix TextFormField cause rebuild widget in Flutter

            Npm cannot find a required file even through it is in the searched directory