欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Javascript实现的SHA-256加密算法完整实例

程序员文章站 2022-07-20 17:00:20
本文实例讲述了javascript实现的sha-256算法。分享给大家供大家参考,具体如下: /** * * secure hash algorithm (s...

本文实例讲述了javascript实现的sha-256算法。分享给大家供大家参考,具体如下:

/**
*
* secure hash algorithm (sha256)
* http://www.webtoolkit.info/
*
* original code by angel marin, paul johnston.
*
**/
function sha256(s){
  var chrsz = 8;
  var hexcase = 0;
  function safe_add (x, y) {
    var lsw = (x & 0xffff) + (y & 0xffff);
    var msw = (x >> 16) + (y >> 16) + (lsw >> 16);
    return (msw << 16) | (lsw & 0xffff);
  }
  function s (x, n) { return ( x >>> n ) | (x << (32 - n)); }
  function r (x, n) { return ( x >>> n ); }
  function ch(x, y, z) { return ((x & y) ^ ((~x) & z)); }
  function maj(x, y, z) { return ((x & y) ^ (x & z) ^ (y & z)); }
  function sigma0256(x) { return (s(x, 2) ^ s(x, 13) ^ s(x, 22)); }
  function sigma1256(x) { return (s(x, 6) ^ s(x, 11) ^ s(x, 25)); }
  function gamma0256(x) { return (s(x, 7) ^ s(x, 18) ^ r(x, 3)); }
  function gamma1256(x) { return (s(x, 17) ^ s(x, 19) ^ r(x, 10)); }
  function core_sha256 (m, l) {
    var k = new array(0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5, 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174, 0xe49b69c1, 0xefbe4786, 0xfc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da, 0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147, 0x6ca6351, 0x14292967, 0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13, 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85, 0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070, 0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3, 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2);
    var hash = new array(0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a, 0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19);
    var w = new array(64);
    var a, b, c, d, e, f, g, h, i, j;
    var t1, t2;
    m[l >> 5] |= 0x80 << (24 - l % 32);
    m[((l + 64 >> 9) << 4) + 15] = l;
    for ( var i = 0; i<m.length; i+=16 ) {
      a = hash[0];
      b = hash[1];
      c = hash[2];
      d = hash[3];
      e = hash[4];
      f = hash[5];
      g = hash[6];
      h = hash[7];
      for ( var j = 0; j<64; j++) {
        if (j < 16) w[j] = m[j + i];
        else w[j] = safe_add(safe_add(safe_add(gamma1256(w[j - 2]), w[j - 7]), gamma0256(w[j - 15])), w[j - 16]);
        t1 = safe_add(safe_add(safe_add(safe_add(h, sigma1256(e)), ch(e, f, g)), k[j]), w[j]);
        t2 = safe_add(sigma0256(a), maj(a, b, c));
        h = g;
        g = f;
        f = e;
        e = safe_add(d, t1);
        d = c;
        c = b;
        b = a;
        a = safe_add(t1, t2);
      }
      hash[0] = safe_add(a, hash[0]);
      hash[1] = safe_add(b, hash[1]);
      hash[2] = safe_add(c, hash[2]);
      hash[3] = safe_add(d, hash[3]);
      hash[4] = safe_add(e, hash[4]);
      hash[5] = safe_add(f, hash[5]);
      hash[6] = safe_add(g, hash[6]);
      hash[7] = safe_add(h, hash[7]);
    }
    return hash;
  }
  function str2binb (str) {
    var bin = array();
    var mask = (1 << chrsz) - 1;
    for(var i = 0; i < str.length * chrsz; i += chrsz) {
      bin[i>>5] |= (str.charcodeat(i / chrsz) & mask) << (24 - i%32);
    }
    return bin;
  }
  function utf8encode(string) {
    string = string.replace(/\r\n/g,"\n");
    var utftext = "";
    for (var n = 0; n < string.length; n++) {
      var c = string.charcodeat(n);
      if (c < 128) {
        utftext += string.fromcharcode(c);
      }
      else if((c > 127) && (c < 2048)) {
        utftext += string.fromcharcode((c >> 6) | 192);
        utftext += string.fromcharcode((c & 63) | 128);
      }
      else {
        utftext += string.fromcharcode((c >> 12) | 224);
        utftext += string.fromcharcode(((c >> 6) & 63) | 128);
        utftext += string.fromcharcode((c & 63) | 128);
      }
    }
    return utftext;
  }
  function binb2hex (binarray) {
    var hex_tab = hexcase ? "0123456789abcdef" : "0123456789abcdef";
    var str = "";
    for(var i = 0; i < binarray.length * 4; i++) {
      str += hex_tab.charat((binarray[i>>2] >> ((3 - i%4)*8+4)) & 0xf) +
      hex_tab.charat((binarray[i>>2] >> ((3 - i%4)*8 )) & 0xf);
    }
    return str;
  }
  s = utf8encode(s);
  return binb2hex(core_sha256(str2binb(s), s.length * chrsz));
}

更多关于javascript相关内容感兴趣的读者可查看本站专题:《javascript数据结构与算法技巧总结》及《javascript数学运算用法总结

希望本文所述对大家javascript程序设计有所帮助。