Assignment 15

by sheila massey

HTML

<!-- Begin input div -->
<h1>Creating your own Blockchain</h1>
<div class="divBG">

  <!-- Create user input content-->
  <table border="0" align="center" height="165px">
    <tr>
      <td>
        Name <input type="textbox" id="name"><br />
        Date <input type="date" id="date" value = "2018-01-01"><br />
        Transaction Amount <input type="number" id="amount" min="0" step=".01" value ="0"/><br />
        
      </td>
    </tr>
    <tr>
      <td valign="top">
        <input type="button" class="btnstyle" value="New Blockchain" id="btnNewBlockchain" onClick="newBlockchain();" />
        <input type="button" class="btnstyle" value="Add Block" id="btnAddBlock" onClick="newUserBlock();" />
      </td>
    </tr>
    <tr height="15%">
      <td>
        <!-- Begin div displays -->
        <div id="message">

        </div>
      </td>
    </tr>
  </table>
</div>


<br />


<div class="outputDiv">
  <big><strong>Blockchain Data</strong></big>
  <br />
  <div class="container" id="output">

  </div>
</div>

JavaScript

var sha256 = function sha256(ascii) {
  function rightRotate(value, amount) {
    return (value >>> amount) | (value << (32 - amount));
  }

  var mathPow = Math.pow;
  var maxWord = mathPow(2, 32);
  var lengthProperty = 'length'
  var i, j; // Used as a counter across the whole file
  var result = ''

  var words = [];
  var asciiBitLength = ascii[lengthProperty] * 8;

  //* caching results is optional - remove/add slash from front of this line to toggle
  // Initial hash value: first 32 bits of the fractional parts of the square roots of the first 8 primes
  // (we actually calculate the first 64, but extra values are just ignored)
  var hash = sha256.h = sha256.h || [];
  // Round constants: first 32 bits of the fractional parts of the cube roots of the first 64 primes
  var k = sha256.k = sha256.k || [];
  var primeCounter = k[lengthProperty];
  /*/
  var hash = [], k = [];
  var primeCounter = 0;
  //*/

  var isComposite = {};
  for (var candidate = 2; primeCounter < 64; candidate++) {
    if (!isComposite[candidate]) {
      for (i = 0; i < 313; i += candidate) {
        isComposite[i] = candidate;
      }
      hash[primeCounter] = (mathPow(candidate, 0.5) * maxWord) | 0;
      k[primeCounter++] = (mathPow(candidate, 1 / 3) * maxWord) | 0;
    }
  }

  ascii += '\x80' // Append Ƈ' bit (plus zero padding)
  while (ascii[lengthProperty] % 64 - 56) ascii += '\x00' // More zero padding
  for (i = 0; i < ascii[lengthProperty]; i++) {
    j = ascii.charCodeAt(i);
    if (j >> 8) return; // ASCII check: only accept characters in range 0-255
    words[i >> 2] |= j << ((3 - i) % 4) * 8;
  }
  words[words[lengthProperty]] = ((asciiBitLength / maxWord) | 0);
  words[words[lengthProperty]] = (asciiBitLength)

  // process each chunk
  for (j = 0; j < words[lengthProperty];) {
    var w = words.slice(j, j += 16); // The message is expanded into 64 words as part of the iteration
    var oldHash = hash;
    // This is now the undefinedworking hash", often labelled as...