source: Dev/trunk/src/client/dojox/encoding/digests/SHA256.js

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

Added Dojo 1.9.3 release.

File size: 1.7 KB
Line 
1define(["./_sha-32"], function(sha32){
2        //      The 256-bit implementation of SHA-2
3        var hash = [
4                0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a,
5                0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19
6        ];
7
8        var SHA256 = function(/* String */data, /* sha32.outputTypes? */outputType){
9                var out = outputType || sha32.outputTypes.Base64;
10                data = sha32.stringToUtf8(data);
11                var wa = sha32.digest(sha32.toWord(data), data.length * 8, hash, 256);
12                switch(out){
13                        case sha32.outputTypes.Raw: {
14                                return wa;
15                        }
16                        case sha32.outputTypes.Hex: {
17                                return sha32.toHex(wa);
18                        }
19                        case sha32.outputTypes.String: {
20                                return sha32._toString(wa);
21                        }
22                        default: {
23                                return sha32.toBase64(wa);
24                        }
25                }
26        };
27
28        SHA256._hmac = function(/* String */data, /* String */key, /* sha32.outputTypes? */outputType){
29                var out = outputType || sha32.outputTypes.Base64;
30                data = sha32.stringToUtf8(data);
31                key = sha32.stringToUtf8(key);
32
33                //      prepare the key
34                var wa = sha32.toWord(key);
35                if(wa.length > 16){
36                        wa = sha32.digest(wa, key.length * 8, hash, 256);
37                }
38
39                //      set up the pads
40                var ipad = new Array(16), opad = new Array(16);
41                for(var i=0; i<16; i++){
42                        ipad[i] = wa[i] ^ 0x36363636;
43                        opad[i] = wa[i] ^ 0x5c5c5c5c;
44                }
45
46                //      make the final digest
47                var r1 = sha32.digest(ipad.concat(sha32.toWord(data)), 512 + data.length * 8, hash, 256);
48                var r2 = sha32.digest(opad.concat(r1), 512 + 160, hash, 256);
49
50                //      return the output.
51                switch(out){
52                        case sha32.outputTypes.Raw: {
53                                return wa;
54                        }
55                        case sha32.outputTypes.Hex: {
56                                return sha32.toHex(wa);
57                        }
58                        case sha32.outputTypes.String: {
59                                return sha32._toString(wa);
60                        }
61                        default: {
62                                return sha32.toBase64(wa);
63                        }
64                }
65        };
66
67        return SHA256;
68});
Note: See TracBrowser for help on using the repository browser.