JavaScript implementation of SHA-256 encryption algorithm Complete Example _ basic knowledge

Source: Internet
Author: User
Tags hash

This example describes the SHA-256 algorithm for JavaScript implementations. Share to everyone for your reference, specific as follows:

/** * 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 >>) + (y >>) + (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, 22));}
  function Sigma1256 (x) {return (S (x, 6) ^ s (x, one) ^ s (x, 25));}
  function Gamma0256 (x) {return (S (x, 7) ^ s (x, +) ^ R (x, 3));}
  function Gamma1256 (x) {return (s (x,) ^ s (x,) ^ 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 + >> 9) << 4) +] = 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 <) 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) &L
    t;< (24-I%32);
  } return bin;
    The 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));

 }

More readers interested in JavaScript-related content can view this site: "JavaScript data structure and algorithm skills summary" and "JavaScript Mathematical operation Summary"

I hope this article will help you with JavaScript programming.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.