Copyright notice for new script file
[openemr.git] / library / sha1.js
blobd92905caf334defda0a15261c5cc78d41f42a7f1
1 <script language="javascript">
2 //-------------------------------------------------------------------
3 // This program is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU General Public License
5 // as published by the Free Software Foundation; either version 2
6 // of the License, or (at your option) any later version.
7 //-------------------------------------------------------------------
8 function SHA1 (msg) {
9         function rotate_left(n,s) {
10                 var t4 = ( n<<s ) | (n>>>(32-s));
11                 return t4;
12         };
13         function lsb_hex(val) {
14                 var str="";
15                 var i;
16                 var vh;
17                 var vl;
18                 for( i=0; i<=6; i+=2 ) {
19                         vh = (val>>>(i*4+4))&0x0f;
20                         vl = (val>>>(i*4))&0x0f;
21                         str += vh.toString(16) + vl.toString(16);
22                 }
23                 return str;
24         };
25         function cvt_hex(val) {
26                 var str="";
27                 var i;
28                 var v;
29                 for( i=7; i>=0; i-- ) {
30                         v = (val>>>(i*4))&0x0f;
31                         str += v.toString(16);
32                 }
33                 return str;
34         };
35         function Utf8Encode(string) {
36                 string = string.replace(/\r\n/g,"\n");
37                 var utftext = "";
38                 for (var n = 0; n < string.length; n++) {
39                         var c = string.charCodeAt(n);
40                         if (c < 128) {
41                                 utftext += String.fromCharCode(c);
42                         }
43                         else if((c > 127) && (c < 2048)) {
44                                 utftext += String.fromCharCode((c >> 6) | 192);
45                                 utftext += String.fromCharCode((c & 63) | 128);
46                         }
47                         else {
48                                 utftext += String.fromCharCode((c >> 12) | 224);
49                                 utftext += String.fromCharCode(((c >> 6) & 63) | 128);
50                                 utftext += String.fromCharCode((c & 63) | 128);
51                         }
52                 }
53                 return utftext;
54         };
55         var blockstart;
56         var i, j;
57         var W = new Array(80);
58         var H0 = 0x67452301;
59         var H1 = 0xEFCDAB89;
60         var H2 = 0x98BADCFE;
61         var H3 = 0x10325476;
62         var H4 = 0xC3D2E1F0;
63         var A, B, C, D, E;
64         var temp;
66         msg = Utf8Encode(msg);
67         var msg_len = msg.length;
68         var word_array = new Array();
69         for( i=0; i<msg_len-3; i+=4 ) {
70                 j = msg.charCodeAt(i)<<24 | msg.charCodeAt(i+1)<<16 |
71                 msg.charCodeAt(i+2)<<8 | msg.charCodeAt(i+3);
72                 word_array.push( j );
73         }
74         switch( msg_len % 4 ) {
75                 case 0:
76                         i = 0x080000000;
77                 break;
78                 case 1:
79                         i = msg.charCodeAt(msg_len-1)<<24 | 0x0800000;
80                 break;
82                 case 2:
83                         i = msg.charCodeAt(msg_len-2)<<24 | msg.charCodeAt(msg_len-1)<<16 | 0x08000;
84                 break;
86                 case 3:
87                         i = msg.charCodeAt(msg_len-3)<<24 | msg.charCodeAt(msg_len-2)<<16 | msg.charCodeAt(msg_len-1)<<8        | 0x80;
88                 break;
89         }
91         word_array.push( i );
92         while( (word_array.length % 16) != 14 ) word_array.push( 0 );
93         word_array.push( msg_len>>>29 );
94         word_array.push( (msg_len<<3)&0x0ffffffff );
95         for ( blockstart=0; blockstart<word_array.length; blockstart+=16 ) {
97                 for( i=0; i<16; i++ ) W[i] = word_array[blockstart+i];
98                 for( i=16; i<=79; i++ ) W[i] = rotate_left(W[i-3] ^ W[i-8] ^ W[i-14] ^ W[i-16], 1);
99                 A = H0;
100                 B = H1;
101                 C = H2;
102                 D = H3;
103                 E = H4;
104                 for( i= 0; i<=19; i++ ) {
105                         temp = (rotate_left(A,5) + ((B&C) | (~B&D)) + E + W[i] + 0x5A827999) & 0x0ffffffff;
106                         E = D;
107                         D = C;
108                         C = rotate_left(B,30);
109                         B = A;
110                         A = temp;
111                 }
112                 for( i=20; i<=39; i++ ) {
113                         temp = (rotate_left(A,5) + (B ^ C ^ D) + E + W[i] + 0x6ED9EBA1) & 0x0ffffffff;
114                         E = D;
115                         D = C;
116                         C = rotate_left(B,30);
117                         B = A;
118                         A = temp;
119                 }
120                 for( i=40; i<=59; i++ ) {
121                         temp = (rotate_left(A,5) + ((B&C) | (B&D) | (C&D)) + E + W[i] + 0x8F1BBCDC) & 0x0ffffffff;
122                         E = D;
123                         D = C;
124                         C = rotate_left(B,30);
125                         B = A;
126                         A = temp;
127                 }
128                 for( i=60; i<=79; i++ ) {
129                         temp = (rotate_left(A,5) + (B ^ C ^ D) + E + W[i] + 0xCA62C1D6) & 0x0ffffffff;
130                         E = D;
131                         D = C;
132                         C = rotate_left(B,30);
133                         B = A;
134                         A = temp;
135                 }
136                 H0 = (H0 + A) & 0x0ffffffff;
137                 H1 = (H1 + B) & 0x0ffffffff;
138                 H2 = (H2 + C) & 0x0ffffffff;
139                 H3 = (H3 + D) & 0x0ffffffff;
140                 H4 = (H4 + E) & 0x0ffffffff;
141         }
142         var temp = cvt_hex(H0) + cvt_hex(H1) + cvt_hex(H2) + cvt_hex(H3) + cvt_hex(H4);
143         return temp.toLowerCase();
145 </script>