Michael Bradley, Jr. - Hash-0.06

Documentation | Source
/*//////////////////////////////////////////////////////////////////////////
 * THIS PROGRAM IS A DERIVATIVE OF AN EXISTING WORK :: 4 September 2010
 * See: http://pajhome.org.uk/crypt/md5/scripts.html
 *
 * Significant but mostly superficial changes have been made to the structure of the
 * source so that it conforms to the Joose3 object system for JavaScript
 * See: http://joose.it/
/*//////////////////////////////////////////////////////////////////////////


Joose.Class('Hash.SHA256', {
    
  /*VERSION*/VERSION : 0.06,
    
  my : {
   
    has : {

      sha256_K : {
        is : 'ro',
        init : [
                 1116352408, 1899447441, -1245643825, -373957723, 961987163, 1508970993,
                 -1841331548, -1424204075, -670586216, 310598401, 607225278, 1426881987,
                 1925078388, -2132889090, -1680079193, -1046744716, -459576895, -272742522,
                 264347078, 604807628, 770255983, 1249150122, 1555081692, 1996064986,
                 -1740746414, -1473132947, -1341970488, -1084653625, -958395405, -710438585,
                 113926993, 338241895, 666307205, 773529912, 1294757372, 1396182291,
                 1695183700, 1986661051, -2117940946, -1838011259, -1564481375, -1474664885,
                 -1035236496, -949202525, -778901479, -694614492, -200395387, 275423344,
                 430227734, 506948616, 659060556, 883997877, 958139571, 1322822218,
                 1537002063, 1747873779, 1955562222, 2024104815, -2067236844, -1933114872,
                 -1866530822, -1538233109, -1090935817, -965641998
               ]
      }    

    },
    
    methods : {

      /*
       * These are the functions you'll usually want to call
       * They take string arguments and return either hex or base-64 encoded strings
       */
      hex_sha256 : function (s) {
        return Encode.rstr2hex(this.rstr_sha256(Encode.str2rstr_utf8(s)))
      },
      
      b64_sha256 : function (s) {
        return Encode.rstr2b64(this.rstr_sha256(Encode.str2rstr_utf8(s)))
      },
      
      any_sha256 : function (s, e) {
        return Encode.rstr2any(this.rstr_sha256(Encode.str2rstr_utf8(s)), e)
      },
      
      hex_hmac_sha256 : function (k, d) {
        return Encode.rstr2hex(this.rstr_hmac_sha256(Encode.str2rstr_utf8(k), Encode.str2rstr_utf8(d)))
      },
      
      b64_hmac_sha256 : function (k, d) {
        return Encode.rstr2b64(this.rstr_hmac_sha256(Encode.str2rstr_utf8(k), Encode.str2rstr_utf8(d)))
      },
      
      any_hmac_sha256 : function (k, d, e) {
        return Encode.rstr2any(this.rstr_hmac_sha256(Encode.str2rstr_utf8(k), Encode.str2rstr_utf8(d)), e)
      },
      
      /*
       * Perform a simple self-test to see if the VM is working
       */
      sha256_vm_test : function () {
        return this.hex_sha256('abc').toLowerCase() == 'ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad'
      },

      /*
       * Calculate the sha256 of a raw string
       */
      rstr_sha256 : function (s) {
        return Encode.binb2rstr(this.binb_sha256(Encode.rstr2binb(s), s.length * 8))
      },

      /*
       * Calculate the HMAC-sha256 of a key and some data (raw strings)
       */
      rstr_hmac_sha256 : function (key, data) {
        var bkey = Encode.rstr2binb(key)
        if(bkey.length > 16) bkey = this.binb_sha256(bkey, key.length * 8)
      
        var ipad = Array(16), opad = Array(16)
        for(var i = 0; i < 16; i++) {
            ipad[i] = bkey[i] ^ 0x36363636
            opad[i] = bkey[i] ^ 0x5C5C5C5C
        }
      
        var hash = this.binb_sha256(ipad.concat(Encode.rstr2binb(data)), 512 + data.length * 8)
        return Encode.binb2rstr(this.binb_sha256(opad.concat(hash), 512 + 256))
      },

      /*
       * Main sha256 function, with its support functions
       */
      sha256_S : function (X, n) {return ( X >>> n ) | (X << (32 - n))},
      
      sha256_R : function (X, n) {return ( X >>> n )},
      
      sha256_Ch : function (x, y, z) {return ((x & y) ^ ((~x) & z))},
      
      sha256_Maj : function (x, y, z) {return ((x & y) ^ (x & z) ^ (y & z))},
      
      sha256_Sigma0256 : function (x) {return (this.sha256_S(x, 2) ^ this.sha256_S(x, 13) ^ this.sha256_S(x, 22))},
      
      sha256_Sigma1256 : function (x) {return (this.sha256_S(x, 6) ^ this.sha256_S(x, 11) ^ this.sha256_S(x, 25))},
      
      sha256_Gamma0256 : function (x) {return (this.sha256_S(x, 7) ^ this.sha256_S(x, 18) ^ this.sha256_R(x, 3))},
      
      sha256_Gamma1256 : function (x) {return (this.sha256_S(x, 17) ^ this.sha256_S(x, 19) ^ this.sha256_R(x, 10))},
      
      sha256_Sigma0512 : function (x) {return (this.sha256_S(x, 28) ^ this.sha256_S(x, 34) ^ this.sha256_S(x, 39))},
      
      sha256_Sigma1512 : function (x) {return (this.sha256_S(x, 14) ^ this.sha256_S(x, 18) ^ this.sha256_S(x, 41))},
      
      sha256_Gamma0512 : function (x) {return (this.sha256_S(x, 1)  ^ this.sha256_S(x, 8) ^ this.sha256_R(x, 7))},
      
      sha256_Gamma1512 : function (x) {return (this.sha256_S(x, 19) ^ this.sha256_S(x, 61) ^ this.sha256_R(x, 6))},
            
      binb_sha256 : function (m, l) {
        var HASH = [ 1779033703, -1150833019, 1013904242, -1521486534, 1359893119, -1694144372, 528734635, 1541459225 ]
        var W = new Array(64)
        var a, b, c, d, e, f, g, h
        var i, j, T1, T2
      
        /* append padding */
        m[l >> 5] |= 0x80 << (24 - l % 32)
        m[((l + 64 >> 9) << 4) + 15] = l
      
        for (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 (j = 0; j < 64; j++) {
              if (j < 16) W[j] = m[j + i]
              else W[j] = this.safe_add(this.safe_add(this.safe_add(this.sha256_Gamma1256(W[j - 2]), W[j - 7]),
                                                                        this.sha256_Gamma0256(W[j - 15])), W[j - 16])
      
              T1 = this.safe_add(this.safe_add(this.safe_add(this.safe_add(h, this.sha256_Sigma1256(e)), this.sha256_Ch(e, f, g)),
                                                                                            this.sha256_K[j]), W[j])
              T2 = this.safe_add(this.sha256_Sigma0256(a), this.sha256_Maj(a, b, c))
              h = g
              g = f
              f = e
              e = this.safe_add(d, T1)
              d = c
              c = b
              b = a
              a = this.safe_add(T1, T2)
            }
      
            HASH[0] = this.safe_add(a, HASH[0])
            HASH[1] = this.safe_add(b, HASH[1])
            HASH[2] = this.safe_add(c, HASH[2])
            HASH[3] = this.safe_add(d, HASH[3])
            HASH[4] = this.safe_add(e, HASH[4])
            HASH[5] = this.safe_add(f, HASH[5])
            HASH[6] = this.safe_add(g, HASH[6])
            HASH[7] = this.safe_add(h, HASH[7])
        }
        return HASH
      },
      
      safe_add : function (x, y) {
        var lsw = (x & 0xFFFF) + (y & 0xFFFF)
        var msw = (x >> 16) + (y >> 16) + (lsw >> 16)
        return (msw << 16) | (lsw & 0xFFFF)
      }

    }
    
  }
  
})


/*///////////////////// ORIGINAL LICENSE BELOW /////////////////////////////////
 * A JavaScript implementation of the Secure Hash Algorithm, SHA-256, as defined
 * in FIPS 180-2
 * Version 2.2 Copyright Angel Marin, Paul Johnston 2000 - 2009.
 * Other contributors: Greg Holt, Andrew Kepert, Ydnar, Lostinet
 * Distributed under the BSD License
 * All rights reserved.
 *
 * See http://pajhome.org.uk/crypt/md5 for details.
 * Also http://anmar.eu.org/projects/jssha2/
 *
 *
 * Redistribution and use in source and binary forms, with or without modification,
 * are permitted provided that the following conditions are met:
 * 
 *  * Redistributions of source code must retain the above copyright notice, this
 *    list of conditions and the following disclaimer.
 *  * Redistributions in binary form must reproduce the above copyright notice,
 *    this list of conditions and the following disclaimer in the documentation
 *    and/or other materials provided with the distribution.
 *  * Neither the name of the <ORGANIZATION> nor the names of its contributors may
 *    be used to endorse or promote products derived from this software without
 *    specific prior written permission.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 * IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
 * OF THE POSSIBILITY OF SUCH DAMAGE.
/*//////////////////////////////////////////////////////////////////////////////