Created
August 2, 2019 13:30
-
-
Save bjoerntx/57329c302a28816f9426589ff236300f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// this method validates a document with the last block on the blockchain | |
[System.Web.Http.HttpPost] | |
public bool ValidateDocument([FromBody] string Document, [FromBody] string UniqueId) | |
{ | |
if (Document == null || UniqueId == null) | |
return false; | |
// calculate the MD5 of the uploaded document | |
string sChecksum = Checksum.CalculateMD5(Convert.FromBase64String(Document)); | |
Blockchain newDocument; | |
// load the associated blockchain | |
if (System.IO.File.Exists( | |
Server.MapPath("~/App_Data/Blockchains/" + UniqueId + ".bc"))) | |
{ | |
string bc = System.IO.File.ReadAllText( | |
Server.MapPath("~/App_Data/Blockchains/" + UniqueId + ".bc")); | |
newDocument = JsonConvert.DeserializeObject<Blockchain>(bc); | |
// get the SignedDocument object from the block | |
SignedDocument signedDocument = | |
JsonConvert.DeserializeObject<SignedDocument>(newDocument.GetCurrentBlock().Data); | |
// compare the checksum in the stored block | |
// with the checksum of the uploaded document | |
if (signedDocument.Checksum == sChecksum) | |
return true; // document valid | |
else | |
return false; // not valid | |
} | |
else | |
return false; // blockchain doesn't exist | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment