AWIS(Alexa Web Information Service)是Amazon提供的一种web服务.简介如下:
The Alexa Web Information Service makes Alexa’s vast repository of information about the traffic and structure of the web available to developers.
Service Highlights
- Gather information about web sites, including traffic data, contact information, related links and more.
- Access historical traffic data for web sites to analyze growth and understand the effects of specific events on web site traffic
- Build a web directory into your web site or service using an Alexa enhanced DMOZ-based browse service
- Access the list of sites linking to any given site
这是要说的是使用asp调用此服务时会遇到的问题.也许是出于ASP过于陈旧的原因,网站并没有提供ASP调用此服务的示例代码.在生成用于验证身份的签名信息时遇到了些问题:
- ASP中取UTC时间的问题
- ASP中调用 HMAC-SHA1算法的问题
第一个不知道用VBS如何实现,第二个则实现起来难度比较大.幸运的是在网上找到一个JS版本的相同的函数,又知道了JS提供了UTC相关的函数.索性UTC函数也用JS实现,然后在VBS中调用.下面是调用的代码
Code
utc = GetUtcTime()
utc = GetUtcTime()
Signature
= b64_hmac_sha1(SecretAccessKey, "UrlInfo" & Timestamp) & "="下面再给出HMAC-SHA1算法的JS实现代码:
Code
1 <script language="javascript" type="text/javascript" runat="server">
2 function GetUtcTime(){
3 var od = new Date();
4
5 var sResult = "";
6
7 var year, month, day, hour, minute, second, milSec;
8 year = od.getUTCFullYear();
9 month = od.getUTCMonth() + 1;
10 day = od.getUTCDate();
11 hour = od.getUTCHours();
12 minute = od.getUTCMinutes();
13 second = od.getUTCSeconds();
14 milSec = od.getUTCMilliseconds();
15
16 sResult = year + "-"
17 + GetFormatString(month, 2) + "-"
18 + GetFormatString(day, 2) + "T"
19 + GetFormatString(hour, 2) + ":"
20 + GetFormatString(minute, 2) + ":"
21 + GetFormatString(second, 2) + "."
22 + GetFormatString(milSec, 3)
23 + "Z";
24
25 return sResult;
26 }
27
28 function GetFormatString(iValue, iNum){
29 var sResult = "";
30 var sValue = iValue.toString()
31 if (sValue.length >= iNum){
32 sResult = sValue;
33 }else{
34 for (var i = 0;i < iNum - sValue.length; i++){
35 sResult += "0";
36 }
37 sResult += sValue;
38 }
39
40 return sResult;
41 }
42
43 /*sha1
44 * A JavaScript implementation of the Secure Hash Algorithm, SHA-1, as defined
45 * in FIPS PUB 180-1
46 * Version 2.1a Copyright Paul Johnston 2000 - 2002.
47 * Other contributors: Greg Holt, Andrew Kepert, Ydnar, Lostinet
48 * Distributed under the BSD License
49 * See http://pajhome.org.uk/crypt/md5 for details.
50 */
51
52 /*
53 * Configurable variables. You may need to tweak these to be compatible with
54 * the server-side, but the defaults work in most cases.
55 */
56 var hexcase = 0; /* hex output format. 0 - lowercase; 1 - uppercase */
57 var b64pad = ""; /* base-64 pad character. "=" for strict RFC compliance */
58 var chrsz = 8; /* bits per input character. 8 - ASCII; 16 - Unicode */
59
60 /*
61 * These are the functions you'll usually want to call
62 * They take string arguments and return either hex or base-64 encoded strings
63 */
64 function hex_sha1(s){return binb2hex(core_sha1(str2binb(s),s.length * chrsz));}
65 function b64_sha1(s){return binb2b64(core_sha1(str2binb(s),s.length * chrsz));}
66 function str_sha1(s){return binb2str(core_sha1(str2binb(s),s.length * chrsz));}
67 function hex_hmac_sha1(key, data){ return binb2hex(core_hmac_sha1(key, data));}
68 function b64_hmac_sha1(key, data){ return binb2b64(core_hmac_sha1(key, data));}
69 function str_hmac_sha1(key, data){ return binb2str(core_hmac_sha1(key, data));}
70
71 /*
72 * Perform a simple self-test to see if the VM is working
73 */
74 function sha1_vm_test()
75 {
76 return hex_sha1("abc") == "a9993e364706816aba3e25717850c26c9cd0d89d";
77 }
78
79 /*
80 * Calculate the SHA-1 of an array of big-endian words, and a bit length
81 */
82 function core_sha1(x, len)
83 {
84 /* append padding */
85 x[len >> 5] |= 0x80 << (24 - len % 32);
86 x[((len + 64 >> 9) << 4) + 15] = len;
87
88 var w = Array(80);
89 var a = 1732584193;
90 var b = -271733879;
91 var c = -1732584194;
92 var d = 271733878;
93 var e = -1009589776;
94
95 for(var i = 0; i < x.length; i += 16)
96 {
97 var olda = a;
98 var oldb = b;
99 var oldc = c;
100 var oldd = d;
101 var olde = e;
102
103 for(var j = 0; j < 80; j++)
104 {
105 if(j < 16) w[j] = x[i + j];
106 else w[j] = rol(w[j-3] ^ w[j-8] ^ w[j-14] ^ w[j-16], 1);
107 var t = safe_add(safe_add(rol(a, 5), sha1_ft(j, b, c, d)),
108 safe_add(safe_add(e, w[j]), sha1_kt(j)));
109 e = d;
110 d = c;
111 c = rol(b, 30);
112 b = a;
113 a = t;
114 }
115
116 a = safe_add(a, olda);
117 b = safe_add(b, oldb);
118 c = safe_add(c, oldc);
119 d = safe_add(d, oldd);
120 e = safe_add(e, olde);
121 }
122 return Array(a, b, c, d, e);
123
124 }
125
126 /*
127 * Perform the appropriate triplet combination function for the current
128 * iteration
129 */
130 function sha1_ft(t, b, c, d)
131 {
132 if(t < 20) return (b & c) | ((~b) & d);
133 if(t < 40) return b ^ c ^ d;
134 if(t < 60) return (b & c) | (b & d) | (c & d);
135 return b ^ c ^ d;
136 }
137
138 /*
139 * Determine the appropriate additive constant for the current iteration
140 */
141 function sha1_kt(t)
142 {
143 return (t < 20) ? 1518500249 : (t < 40) ? 1859775393 :
144 (t < 60) ? -1894007588 : -899497514;
145 }
146
147 /*
148 * Calculate the HMAC-SHA1 of a key and some data
149 */
150 function core_hmac_sha1(key, data)
151 {
152 var bkey = str2binb(key);
153 if(bkey.length > 16) bkey = core_sha1(bkey, key.length * chrsz);
154
155 var ipad = Array(16), opad = Array(16);
156 for(var i = 0; i < 16; i++)
157 {
158 ipad[i] = bkey[i] ^ 0x36363636;
159 opad[i] = bkey[i] ^ 0x5C5C5C5C;
160 }
161
162 var hash = core_sha1(ipad.concat(str2binb(data)), 512 + data.length * chrsz);
163 return core_sha1(opad.concat(hash), 512 + 160);
164 }
165
166 /*
167 * Add integers, wrapping at 2^32. This uses 16-bit operations internally
168 * to work around bugs in some JS interpreters.
169 */
170 function safe_add(x, y)
171 {
172 var lsw = (x & 0xFFFF) + (y & 0xFFFF);
173 var msw = (x >> 16) + (y >> 16) + (lsw >> 16);
174 return (msw << 16) | (lsw & 0xFFFF);
175 }
176
177 /*
178 * Bitwise rotate a 32-bit number to the left.
179 */
180 function rol(num, cnt)
181 {
182 return (num << cnt) | (num >>> (32 - cnt));
183 }
184
185 /*
186 * Convert an 8-bit or 16-bit string to an array of big-endian words
187 * In 8-bit function, characters >255 have their hi-byte silently ignored.
188 */
189 function str2binb(str)
190 {
191 var bin = Array();
192 var mask = (1 << chrsz) - 1;
193 for(var i = 0; i < str.length * chrsz; i += chrsz)
194 bin[i>>5] |= (str.charCodeAt(i / chrsz) & mask) << (32 - chrsz - i%32);
195 return bin;
196 }
197
198 /*
199 * Convert an array of big-endian words to a string
200 */
201 function binb2str(bin)
202 {
203 var str = "";
204 var mask = (1 << chrsz) - 1;
205 for(var i = 0; i < bin.length * 32; i += chrsz)
206 str += String.fromCharCode((bin[i>>5] >>> (32 - chrsz - i%32)) & mask);
207 return str;
208 }
209
210 /*
211 * Convert an array of big-endian words to a hex string.
212 */
213 function binb2hex(binarray)
214 {
215 var hex_tab = hexcase ? "0123456789ABCDEF" : "0123456789abcdef";
216 var str = "";
217 for(var i = 0; i < binarray.length * 4; i++)
218 {
219 str += hex_tab.charAt((binarray[i>>2] >> ((3 - i%4)*8+4)) & 0xF) +
220 hex_tab.charAt((binarray[i>>2] >> ((3 - i%4)*8 )) & 0xF);
221 }
222 return str;
223 }
224
225 /*
226 * Convert an array of big-endian words to a base-64 string
227 */
228 function binb2b64(binarray)
229 {
230 var tab = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
231 var str = "";
232 for(var i = 0; i < binarray.length * 4; i += 3)
233 {
234 var triplet = (((binarray[i >> 2] >> 8 * (3 - i %4)) & 0xFF) << 16)
235 | (((binarray[i+1 >> 2] >> 8 * (3 - (i+1)%4)) & 0xFF) << 8 )
236 | ((binarray[i+2 >> 2] >>
1 <script language="javascript" type="text/javascript" runat="server">
2 function GetUtcTime(){
3 var od = new Date();
4
5 var sResult = "";
6
7 var year, month, day, hour, minute, second, milSec;
8 year = od.getUTCFullYear();
9 month = od.getUTCMonth() + 1;
10 day = od.getUTCDate();
11 hour = od.getUTCHours();
12 minute = od.getUTCMinutes();
13 second = od.getUTCSeconds();
14 milSec = od.getUTCMilliseconds();
15
16 sResult = year + "-"
17 + GetFormatString(month, 2) + "-"
18 + GetFormatString(day, 2) + "T"
19 + GetFormatString(hour, 2) + ":"
20 + GetFormatString(minute, 2) + ":"
21 + GetFormatString(second, 2) + "."
22 + GetFormatString(milSec, 3)
23 + "Z";
24
25 return sResult;
26 }
27
28 function GetFormatString(iValue, iNum){
29 var sResult = "";
30 var sValue = iValue.toString()
31 if (sValue.length >= iNum){
32 sResult = sValue;
33 }else{
34 for (var i = 0;i < iNum - sValue.length; i++){
35 sResult += "0";
36 }
37 sResult += sValue;
38 }
39
40 return sResult;
41 }
42
43 /*sha1
44 * A JavaScript implementation of the Secure Hash Algorithm, SHA-1, as defined
45 * in FIPS PUB 180-1
46 * Version 2.1a Copyright Paul Johnston 2000 - 2002.
47 * Other contributors: Greg Holt, Andrew Kepert, Ydnar, Lostinet
48 * Distributed under the BSD License
49 * See http://pajhome.org.uk/crypt/md5 for details.
50 */
51
52 /*
53 * Configurable variables. You may need to tweak these to be compatible with
54 * the server-side, but the defaults work in most cases.
55 */
56 var hexcase = 0; /* hex output format. 0 - lowercase; 1 - uppercase */
57 var b64pad = ""; /* base-64 pad character. "=" for strict RFC compliance */
58 var chrsz = 8; /* bits per input character. 8 - ASCII; 16 - Unicode */
59
60 /*
61 * These are the functions you'll usually want to call
62 * They take string arguments and return either hex or base-64 encoded strings
63 */
64 function hex_sha1(s){return binb2hex(core_sha1(str2binb(s),s.length * chrsz));}
65 function b64_sha1(s){return binb2b64(core_sha1(str2binb(s),s.length * chrsz));}
66 function str_sha1(s){return binb2str(core_sha1(str2binb(s),s.length * chrsz));}
67 function hex_hmac_sha1(key, data){ return binb2hex(core_hmac_sha1(key, data));}
68 function b64_hmac_sha1(key, data){ return binb2b64(core_hmac_sha1(key, data));}
69 function str_hmac_sha1(key, data){ return binb2str(core_hmac_sha1(key, data));}
70
71 /*
72 * Perform a simple self-test to see if the VM is working
73 */
74 function sha1_vm_test()
75 {
76 return hex_sha1("abc") == "a9993e364706816aba3e25717850c26c9cd0d89d";
77 }
78
79 /*
80 * Calculate the SHA-1 of an array of big-endian words, and a bit length
81 */
82 function core_sha1(x, len)
83 {
84 /* append padding */
85 x[len >> 5] |= 0x80 << (24 - len % 32);
86 x[((len + 64 >> 9) << 4) + 15] = len;
87
88 var w = Array(80);
89 var a = 1732584193;
90 var b = -271733879;
91 var c = -1732584194;
92 var d = 271733878;
93 var e = -1009589776;
94
95 for(var i = 0; i < x.length; i += 16)
96 {
97 var olda = a;
98 var oldb = b;
99 var oldc = c;
100 var oldd = d;
101 var olde = e;
102
103 for(var j = 0; j < 80; j++)
104 {
105 if(j < 16) w[j] = x[i + j];
106 else w[j] = rol(w[j-3] ^ w[j-8] ^ w[j-14] ^ w[j-16], 1);
107 var t = safe_add(safe_add(rol(a, 5), sha1_ft(j, b, c, d)),
108 safe_add(safe_add(e, w[j]), sha1_kt(j)));
109 e = d;
110 d = c;
111 c = rol(b, 30);
112 b = a;
113 a = t;
114 }
115
116 a = safe_add(a, olda);
117 b = safe_add(b, oldb);
118 c = safe_add(c, oldc);
119 d = safe_add(d, oldd);
120 e = safe_add(e, olde);
121 }
122 return Array(a, b, c, d, e);
123
124 }
125
126 /*
127 * Perform the appropriate triplet combination function for the current
128 * iteration
129 */
130 function sha1_ft(t, b, c, d)
131 {
132 if(t < 20) return (b & c) | ((~b) & d);
133 if(t < 40) return b ^ c ^ d;
134 if(t < 60) return (b & c) | (b & d) | (c & d);
135 return b ^ c ^ d;
136 }
137
138 /*
139 * Determine the appropriate additive constant for the current iteration
140 */
141 function sha1_kt(t)
142 {
143 return (t < 20) ? 1518500249 : (t < 40) ? 1859775393 :
144 (t < 60) ? -1894007588 : -899497514;
145 }
146
147 /*
148 * Calculate the HMAC-SHA1 of a key and some data
149 */
150 function core_hmac_sha1(key, data)
151 {
152 var bkey = str2binb(key);
153 if(bkey.length > 16) bkey = core_sha1(bkey, key.length * chrsz);
154
155 var ipad = Array(16), opad = Array(16);
156 for(var i = 0; i < 16; i++)
157 {
158 ipad[i] = bkey[i] ^ 0x36363636;
159 opad[i] = bkey[i] ^ 0x5C5C5C5C;
160 }
161
162 var hash = core_sha1(ipad.concat(str2binb(data)), 512 + data.length * chrsz);
163 return core_sha1(opad.concat(hash), 512 + 160);
164 }
165
166 /*
167 * Add integers, wrapping at 2^32. This uses 16-bit operations internally
168 * to work around bugs in some JS interpreters.
169 */
170 function safe_add(x, y)
171 {
172 var lsw = (x & 0xFFFF) + (y & 0xFFFF);
173 var msw = (x >> 16) + (y >> 16) + (lsw >> 16);
174 return (msw << 16) | (lsw & 0xFFFF);
175 }
176
177 /*
178 * Bitwise rotate a 32-bit number to the left.
179 */
180 function rol(num, cnt)
181 {
182 return (num << cnt) | (num >>> (32 - cnt));
183 }
184
185 /*
186 * Convert an 8-bit or 16-bit string to an array of big-endian words
187 * In 8-bit function, characters >255 have their hi-byte silently ignored.
188 */
189 function str2binb(str)
190 {
191 var bin = Array();
192 var mask = (1 << chrsz) - 1;
193 for(var i = 0; i < str.length * chrsz; i += chrsz)
194 bin[i>>5] |= (str.charCodeAt(i / chrsz) & mask) << (32 - chrsz - i%32);
195 return bin;
196 }
197
198 /*
199 * Convert an array of big-endian words to a string
200 */
201 function binb2str(bin)
202 {
203 var str = "";
204 var mask = (1 << chrsz) - 1;
205 for(var i = 0; i < bin.length * 32; i += chrsz)
206 str += String.fromCharCode((bin[i>>5] >>> (32 - chrsz - i%32)) & mask);
207 return str;
208 }
209
210 /*
211 * Convert an array of big-endian words to a hex string.
212 */
213 function binb2hex(binarray)
214 {
215 var hex_tab = hexcase ? "0123456789ABCDEF" : "0123456789abcdef";
216 var str = "";
217 for(var i = 0; i < binarray.length * 4; i++)
218 {
219 str += hex_tab.charAt((binarray[i>>2] >> ((3 - i%4)*8+4)) & 0xF) +
220 hex_tab.charAt((binarray[i>>2] >> ((3 - i%4)*8 )) & 0xF);
221 }
222 return str;
223 }
224
225 /*
226 * Convert an array of big-endian words to a base-64 string
227 */
228 function binb2b64(binarray)
229 {
230 var tab = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
231 var str = "";
232 for(var i = 0; i < binarray.length * 4; i += 3)
233 {
234 var triplet = (((binarray[i >> 2] >> 8 * (3 - i %4)) & 0xFF) << 16)
235 | (((binarray[i+1 >> 2] >> 8 * (3 - (i+1)%4)) & 0xFF) << 8 )
236 | ((binarray[i+2 >> 2] >>