source: Dev/trunk/src/client/dojox/encoding/digests/_sha-32.js

Last change on this file was 483, checked in by hendrikvanantwerpen, 11 years ago

Added Dojo 1.9.3 release.

File size: 4.2 KB
Line 
1define(["./_base"], function(base){
2        //      basic functions for 32-bit word based SHA-2 processing.
3
4        //      create a new object that uses a delegated base.
5        var o = (function(b){
6                var tmp = function(){};
7                tmp.prototype = b;
8                var ret = new tmp();
9                return ret;
10        })(base);
11
12        //      expose the output type conversion functions
13        o.toWord = function(s){
14                var wa = Array(s.length >> 2);
15                for(var i=0; i<wa.length; i++) wa[i] = 0;
16                for(var i=0; i<s.length*8; i+=8)
17                        wa[i>>5] |= (s.charCodeAt(i/8)&0xFF)<<(24-i%32);
18                return wa;
19        };
20
21        o.toHex = function(wa){
22                var h="0123456789abcdef", s=[];
23                for(var i=0, l=wa.length*4; i<l; i++){
24                        s.push(h.charAt((wa[i>>2]>>((3-i%4)*8+4))&0xF), h.charAt((wa[i>>2]>>((3-i%4)*8))&0xF));
25                }
26                return s.join("");      //      string
27        };
28
29        o.toBase64 = function(wa){
30                var p="=", tab="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/", s=[];
31                for(var i=0, l=wa.length*4; i<l; i+=3){
32                        var t=(((wa[i>>2]>>8*(3-i%4))&0xFF)<<16)|(((wa[i+1>>2]>>8*(3-(i+1)%4))&0xFF)<<8)|((wa[i+2>>2]>>8*(3-(i+2)%4))&0xFF);
33                        for(var j=0; j<4; j++){
34                                if(i*8+j*6>wa.length*32){
35                                        s.push(p);
36                                } else {
37                                        s.push(tab.charAt((t>>6*(3-j))&0x3F));
38                                }
39                        }
40                }
41                return s.join("");      //      string
42        };
43
44        o._toString = function(wa){
45                var s = "";
46                for(var i=0; i<wa.length*32; i+=8)
47                        s += String.fromCharCode((wa[i>>5]>>>(24-i%32))&0xFF);
48                return s;
49        };
50
51        //      the encoding functions
52        function S (X, n) {return ( X >>> n ) | (X << (32 - n));}
53        function R (X, n) {return ( X >>> n );}
54        function Ch(x, y, z) {return ((x & y) ^ ((~x) & z));}
55        function Maj(x, y, z) {return ((x & y) ^ (x & z) ^ (y & z));}
56        function Sigma0256(x) {return (S(x, 2) ^ S(x, 13) ^ S(x, 22));}
57        function Sigma1256(x) {return (S(x, 6) ^ S(x, 11) ^ S(x, 25));}
58        function Gamma0256(x) {return (S(x, 7) ^ S(x, 18) ^ R(x, 3));}
59        function Gamma1256(x) {return (S(x, 17) ^ S(x, 19) ^ R(x, 10));}
60        function Sigma0512(x) {return (S(x, 28) ^ S(x, 34) ^ S(x, 39));}
61        function Sigma1512(x) {return (S(x, 14) ^ S(x, 18) ^ S(x, 41));}
62        function Gamma0512(x) {return (S(x, 1)  ^ S(x, 8) ^ R(x, 7));}
63        function Gamma1512(x) {return (S(x, 19) ^ S(x, 61) ^ R(x, 6));}
64
65        //      math alias
66        var add = base.addWords;
67
68        //      constant K array
69        var K = [
70                0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,
71                0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,
72                0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
73                0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967,
74                0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13, 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,
75                0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
76                0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,
77                0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2
78        ];
79
80        //      the exposed function, used internally by SHA-224 and SHA-256
81        o.digest = function(msg, length, hash, depth){
82                //      clone the hash
83                hash = hash.slice(0);
84
85                var w = new Array(64);
86                var a, b, c, d, e, f, g, h;
87                var i, j, T1, T2;
88
89                //      append padding
90                msg[length >> 5] |= 0x80 << (24 - length % 32);
91                msg[((length + 64 >> 9) << 4) + 15] = length;
92
93                //      do the digest
94                for(i = 0; i < msg.length; i += 16){
95                        a = hash[0];
96                        b = hash[1];
97                        c = hash[2];
98                        d = hash[3];
99                        e = hash[4];
100                        f = hash[5];
101                        g = hash[6];
102                        h = hash[7];
103
104                        for(j = 0; j < 64; j++) {
105                                if (j < 16){
106                                        w[j] = msg[j + i];
107                                } else {
108                                        w[j] = add(add(add(Gamma1256(w[j - 2]), w[j - 7]), Gamma0256(w[j - 15])), w[j - 16]);
109                                }
110
111                                T1 = add(add(add(add(h, Sigma1256(e)), Ch(e, f, g)), K[j]), w[j]);
112                                T2 = add(Sigma0256(a), Maj(a, b, c));
113                                h = g;
114                                g = f;
115                                f = e;
116                                e = add(d, T1);
117                                d = c;
118                                c = b;
119                                b = a;
120                                a = add(T1, T2);
121                        }
122
123                        hash[0] = add(a, hash[0]);
124                        hash[1] = add(b, hash[1]);
125                        hash[2] = add(c, hash[2]);
126                        hash[3] = add(d, hash[3]);
127                        hash[4] = add(e, hash[4]);
128                        hash[5] = add(f, hash[5]);
129                        hash[6] = add(g, hash[6]);
130                        hash[7] = add(h, hash[7]);
131                }
132                if(depth == 224){
133                        hash.pop();     //      take off the last word
134                }
135                return hash;
136        }
137        return o;
138});
Note: See TracBrowser for help on using the repository browser.