sha256_test_brute_force.js 1.3 KB

12345678910111213141516171819202122232425262728293031323334
  1. /**
  2. * Test SHA-256 using an ad-hoc iterative technique.
  3. * This uses a string buffer which has n characters on the nth iteration.
  4. * Each iteration, the buffer is hashed and the hash is converted to a string.
  5. * The first two characters of the string are prepended to the buffer, then the
  6. * last character of the buffer is removed. This way, neither the beginning nor
  7. * the end of the buffer is fixed.
  8. *
  9. * The hashes from each output step are also hashed together into one final hash.
  10. * This is compared against a final hash which was computed with SSL.
  11. */
  12. new sjcl.test.TestCase("SHA-256 iterative", function (cb) {
  13. if (!sjcl.hash.sha256) {
  14. this.unimplemented();
  15. cb && cb();
  16. return;
  17. }
  18. var toBeHashed = "", cumulative = new sjcl.hash.sha256(), hash, thiz=this;
  19. browserUtil.cpsIterate(function (i, cbb) {
  20. for (var n=100*i; n<100*(i+1); n++) {
  21. hash = sjcl.hash.sha256.hash(toBeHashed);
  22. hash = sjcl.codec.hex.fromBits(hash);
  23. cumulative.update(hash);
  24. toBeHashed = (hash.substring(0,2)+toBeHashed).substring(0,n+1);
  25. }
  26. cbb && cbb();
  27. }, 0, 10, true, function () {
  28. hash = sjcl.codec.hex.fromBits(cumulative.finalize());
  29. thiz.require(hash === "f305c76d5d457ddf04f1927166f5e13429407049a5c5f29021916321fcdcd8b4");
  30. cb && cb();
  31. });
  32. });