[483] | 1 | define(["./_sha-64"], function(sha64){ |
---|
| 2 | // The 512-bit implementation of SHA-2 |
---|
| 3 | |
---|
| 4 | // Note that for 64-bit hashes, we're actually doing high-order, low-order, high-order, low-order. |
---|
| 5 | // The 64-bit functions will assemble them into actual 64-bit "words". |
---|
| 6 | var hash = [ |
---|
| 7 | 0x6a09e667, 0xf3bcc908, 0xbb67ae85, 0x84caa73b, 0x3c6ef372, 0xfe94f82b, 0xa54ff53a, 0x5f1d36f1, |
---|
| 8 | 0x510e527f, 0xade682d1, 0x9b05688c, 0x2b3e6c1f, 0x1f83d9ab, 0xfb41bd6b, 0x5be0cd19, 0x137e2179 |
---|
| 9 | ]; |
---|
| 10 | |
---|
| 11 | // the exported function |
---|
| 12 | var SHA512 = function(/* String */data, /* sha64.outputTypes? */outputType){ |
---|
| 13 | var out = outputType || sha64.outputTypes.Base64; |
---|
| 14 | data = sha64.stringToUtf8(data); |
---|
| 15 | var wa = sha64.digest(sha64.toWord(data), data.length * 8, hash, 512); |
---|
| 16 | switch(out){ |
---|
| 17 | case sha64.outputTypes.Raw: { |
---|
| 18 | return wa; |
---|
| 19 | } |
---|
| 20 | case sha64.outputTypes.Hex: { |
---|
| 21 | return sha64.toHex(wa); |
---|
| 22 | } |
---|
| 23 | case sha64.outputTypes.String: { |
---|
| 24 | return sha64._toString(wa); |
---|
| 25 | } |
---|
| 26 | default: { |
---|
| 27 | return sha64.toBase64(wa); |
---|
| 28 | } |
---|
| 29 | } |
---|
| 30 | }; |
---|
| 31 | SHA512._hmac = function(/* string */data, /* string */key, /* sha64.outputTypes? */outputType){ |
---|
| 32 | var out = outputType || sha64.outputTypes.Base64; |
---|
| 33 | data = sha64.stringToUtf8(data); |
---|
| 34 | key = sha64.stringToUtf8(key); |
---|
| 35 | |
---|
| 36 | // prepare the key |
---|
| 37 | var wa = sha64.toWord(key); |
---|
| 38 | if(wa.length > 16){ |
---|
| 39 | wa = sha64.digest(wa, key.length * 8, hash, 512); |
---|
| 40 | } |
---|
| 41 | |
---|
| 42 | // set up the pads |
---|
| 43 | var ipad = new Array(16), opad = new Array(16); |
---|
| 44 | for(var i=0; i<16; i++){ |
---|
| 45 | ipad[i] = wa[i] ^ 0x36363636; |
---|
| 46 | opad[i] = wa[i] ^ 0x5c5c5c5c; |
---|
| 47 | } |
---|
| 48 | |
---|
| 49 | // make the final digest |
---|
| 50 | var r1 = sha64.digest(ipad.concat(sha64.toWord(data)), 512 + data.length * 8, hash, 512); |
---|
| 51 | var r2 = sha64.digest(opad.concat(r1), 512 + 160, hash, 512); |
---|
| 52 | |
---|
| 53 | // return the output. |
---|
| 54 | switch(out){ |
---|
| 55 | case sha64.outputTypes.Raw: { |
---|
| 56 | return wa; |
---|
| 57 | } |
---|
| 58 | case sha64.outputTypes.Hex: { |
---|
| 59 | return sha64.toHex(wa); |
---|
| 60 | } |
---|
| 61 | case sha64.outputTypes.String: { |
---|
| 62 | return sha64._toString(wa); |
---|
| 63 | } |
---|
| 64 | default: { |
---|
| 65 | return sha64.toBase64(wa); |
---|
| 66 | } |
---|
| 67 | } |
---|
| 68 | }; |
---|
| 69 | return SHA512; |
---|
| 70 | }); |
---|