Skip to content

Instantly share code, notes, and snippets.

@bjoerntx
Created August 2, 2019 13:30
Show Gist options
  • Save bjoerntx/57329c302a28816f9426589ff236300f to your computer and use it in GitHub Desktop.
Save bjoerntx/57329c302a28816f9426589ff236300f to your computer and use it in GitHub Desktop.
// 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