Asp verification code generation

Source: Internet
Author: User
Tags crc32 hash

<% @ Language = "javascript" %>
<%
/*
* Copyright (c) 2006 Hardway Hou
* Distributed under the BSD License
* Too red from PNGlet http://www.elf.org/pnglets/
*/
Var ASCII = new Array (36 );
ASCII [0] = "1110000111110111101111011110111101111011110111101111011110111101111011110111101111011110111110000111 ";
ASCII [1] = "1111011111110001111111110111111111011111111101111111110111111111011111111101111111110111111100000111 ";
ASCII [2] = "1110000111110111101111011110111111111011111111011111111011111111011111111011111111011110111100000011 ";
ASCII [3] = "1110000111110111101111011110111111110111111100111111111101111111111011110111101111011110111110000111 ";
ASCII [4] = "1111101111111110111111110011111110101111110110111111011011111100000011111110111111111011111111000011 ";
ASCII [5] = "1100000011110111111111011111111101000111110011101111111110111111111011110111101111011110111110000111 ";
ASCII [6] = "1111000111111011101111011111111101111111110100011111001110111101111011110111101111011110111110000111 ";
ASCII [7] = "1100000011110111011111011101111111101111111110111111110111111111011111111101111111110111111111011111 ";
ASCII [8] = "1110000111110111101111011110111101111011111000011111101101111101111011110111101111011110111110000111 ";
ASCII [9] = "1110001111110111011111011110111101111011110111001111100010111111111011111111101111011101111110001111 ";
ASCII [10] = "1111011111111101111111101011111110101111111010111111101011111100000111110111011111011101111000100011 ";
ASCII [11] = "1000000111110111101111011110111101110111110000111111011101111101111011110111101111011110111000000111 ";
ASCII [12] = "1110000011110111101110111110111011111111101111111110111111111011111111101111101111011101111110001111 ";
ASCII [13] = "0000001111110111011111011110111101111011110111101111011110111101111011110111101111011101110000001111 ";
ASCII [14] = "1000000111110111101111011011111101101111110000111111011011111101101111110111111111011110111000000111 ";
ASCII [15] = "1000000111110111101111011011111101101111110000111111011011111101101111110111111111011111111000111111 ";
ASCII [16] = "0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 ";

Var FONT_WIDTH = 10;
Var FONT_HEIGHT = 10;

// Note.
// Assume all the strings are formed by BYTE.
// That is, a single character is a BYTE.
// It will be converted to real BYTE at output phase.

// Using index color, depth 8
Var WIDTH = 160;
Var HEIGHT = 40;
Var DEPTH = 8;
Var MAP = new Array (WIDTH + 1) * HEIGHT );

Function B4 (B ){
Return String. fromCharCode (B> 24) & 0xFF, (B> 16) & 0xFF, (B> 8) & 0xFF, B & 0xFF );
}
Function B2 (B ){
Return String. fromCharCode (B> 8) & 0xFF, B & 0xFF );
}
Function B1 (B ){
Return String. fromCharCode (B & 0xFF );
}

Var CRC_TABLE = new Array (256 );
// Make crc table
Function CRC32_Init (){
Var n = 0, k = 0;
Var c;
For (n = 0; n <256; n ++ ){
C = n;
For (k = 0; k <8; k ++ ){
If (c & 1)
C = 0xedb88320 ^ (c >>> 1 );
Else
C = c >>> 1;
  }
CRC_TABLE [n] = c;
 }
}
CRC32_Init ();

Function CRC32 (str ){
Var c = 0 xFFFFFFFF;
Var n = 0;
For (n = 0; n <str. length; n ++ ){
C = CRC_TABLE [(c ^ str. charCodeAt (n) & 0xFF] ^ (c >>> 8 );
 }
Return c ^ 0 xFFFFFFFF;
}

Function png_signature (){
Return [0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a];
}

Function png_chunk (type, str ){
Var ary = [], I = 0;
Var crc;
 
Crc = CRC32 (type + str );
Str = B4 (str. length) + type + str;
Str = str + B4 (crc );
 
For (I = 0; I <str. length; I ++)
Ary. push (str. charCodeAt (I ));
Return ary;
}

Function png_IHDR (){
Return png_chunk ("IHDR", B4 (WIDTH) + B4 (HEIGHT) + B1 (DEPTH) + B1 (3) + B1 (0) + B1 (0) + B1 (0 ));
}

Function png_PLTE (){
Return png_chunk ("PLTE ",
B1 (255) + B1 (255) + B1 (255) // 0
+ B1 (224) + B1 (224) + B1 (224) // 1
+ B1 (192) + B1 (192) + B1 (192) // 2
+ B1 (160) + B1 (160) + B1 (160) // 3
+ B1 (128) + B1 (128) + B1 (128) // 4
+ B1 (96) + B1 (96) + B1 (96) // 5
+ B1 (64) + B1 (64) + B1 (64) // 6
+ B1 (32) + B1 (32) + B1 (32) // 7
+ B1 (0) + B1 (0) + B1 (0) // 8
);
}

Function png_IDAT (){
Var h = HEIGHT
Var strip = WIDTH + 1;
Var x, y;
  
Var data = ""
For (y = 0; y Data = data + String. fromCharCode (MAP [x + y * strip]);
Var LEN = data. length;
Var NLEN = LEN ^ 0 xFFFFFFFF;
 
// Calculate Simple Adler-32 checksum
Var BASE = 65521, NMAX = 5552;
Var s1 = 1, s2 = 0, n = NMAX;
Var I;
For (I = 0; I <LEN; I ++ ){
S1 + = data. charCodeAt (I );
S2 + = s1;
If (n --) = 0 ){
S1 % = BASE;
S2 % = BASE;
N = NMAX;
  }
 }
S1 % = BASE;
S2 % = BASE;
 
Return png_chunk ("IDAT", B1 (0x78) + B1 (0xDA) + B1 (0x01) +/* uncompressd lz77 */
String. fromCharCode (LEN) & 0xFF, (LEN> 8) & 0xFF, (NLEN) & 0xFF, (NLEN> 8) & 0xFF) +
Data + B4 (s2 <16) | s1 ));
}

Function png_IEND (){
Return png_chunk ("IEND ","");
}

Function png_output (){
Var dat = [], I = 0
Var str = "";
Dat = dat. concat (png_signature (), png_IHDR (), png_PLTE (), png_IDAT (), png_IEND ());
If (dat. length % 2 = 1) dat. push (0); // Fill data to make it 2-byte aligned
For (I = 0; I <dat. length-1; I + = 2 ){
Response. binaryWrite (String. fromCharCode (dat [I] + (dat [I + 1] <8 )));
 }
}

Function Map_Init (){
Var x, y;
Var strip = WIDTH + 1;
For (x = 0; x <strip; x ++) for (y = 0; y <HEIGHT; y ++ ){
If (x = 0)
MAP [x + y * strip] = 0; // scanline indicator
Else
MAP [x + y * strip] = 8; // bgcolor
 }
}

Function putPixel (x, y, c ){
MAP [x + 1 + y * (WIDTH + 1)] = c;
}
Function getPixel (x, y, c ){
Return MAP [x + 1 + y * (WIDTH + 1)];
}

Function putChar (ox, oy, ch ){
Var x, y;
Oy + = Math. floor (Math. random () * 5-5.5 );
For (y = 0; y <FONT_HEIGHT; y ++ ){
Ox + = Math. floor (Math. random () * 2 + 0.5)-1;
For (x = 0; x <FONT_WIDTH; x ++ ){
Var c = 1-parseInt (ASCII [ch]. charAt (x + y * FONT_WIDTH ));
If (c> 0 ){
C = 0;
// Double size, bold and random italic
PutPixel (ox + x * 3, oy + y * 2, c );
PutPixel (ox + x * 3 + 1, oy + y * 2, c );
PutPixel (ox + x * 3 + 2, oy + y * 2, c );
PutPixel (ox + x * 3, oy + y * 2 + 1, c );
PutPixel (ox + x * 3 + 1, oy + y * 2 + 1, c );
PutPixel (ox + x * 3 + 2, oy + y * 2 + 1, c );
   }
  }
 }
}
Function putNoise (n, c ){
Var I, x, y;
For (I = 0; I <n; I ++ ){
X = Math. floor (Math. random () * WIDTH );
Y = Math. floor (Math. random () * HEIGHT );
PutPixel (x, y, c );
 }
}
Function smooth (){
Var x, y;
Var c0, c1, c2, c3, c4; // center, up, right, down, left
For (x = 0; x <WIDTH; x ++) for (y = 0; y <HEIGHT; y ++ ){
C0 = getPixel (x, y );
C1 = (y> 0 )? GetPixel (x, Y-1): c0;
C2 = (x <WIDTH )? GetPixel (x + 1, y): c0;
C3 = (y <HEIGHT )? GetPixel (x, y + 1): c0;
C4 = (x> 0 )? GetPixel (X-1, y): c0;
PutPixel (x, y, Math. floor (c0*6 + c1 + c2 + c3 + c4)/10 + 0.5 ));
 }
}

// MD5
Var v1 = 0; var v2 = ""; var v3 = 8; function hex_md5 (s) {return binl2hex (f2 (f8 (s), s. length * v3);} function b64_md5 (s) {return binl2b64 (f2 (f8 (s), s. length * v3);} function str_md5 (s) {return binl2str (f2 (f8 (s), s. length * v3);} function hex_hmac_md5 (key, data) {return binl2hex (f9 (key, data);} function b64_hmac_md5 (key, data) {return binl2b64 (f9 (key, data);} function str_hmac_md5 (key, data) {return binl2str (f9 (key, data);} func Tion f1 () {return hex_md5 ("abc") = "900150983cd24fb0d6963f7d28e17f72";} function f2 (x, len) {x [len> 5] | = 0x80 <(len) % 32); x [(len + 64) >>> 9) <4) + 14] = len; var a = 1732584193; var B =-271733879; var c =-1732584194; var d = 271733878; for (var I = 0; I <x. length; I + = 16) {var olda = a; var oldb = B; var oldc = c; var oldd = d; a = f4 (a, B, c, d, x [I + 0], 7,-680876936); d = f4 (d, a, B, c, x [I + 1], 12,-389564586 ); c = f4 (c, d, a, B, x [I + 2], 17,606105819); B = f4 (B, c, D, a, x [I + 3], 22,-1044525330); a = f4 (a, B, c, d, x [I + 4], 7, -176418897); d = f4 (d, a, B, c, x [I + 5], 12, 1200080426); c = f4 (c, d, a, B, x [I + 6], 17,-1473231341); B = f4 (B, c, d, a, x [I + 7], 22,-45705983 ); a = f4 (a, B, c, d, x [I + 8], 7,1770035416); d = f4 (d, a, B, c, x [I + 9], 12,-1958414417); c = f4 (c, d, a, B, x [I + 10], 17,-42063 ); B = f4 (B, c, d, a, x [I + 11], 22,-1990404162); a = f4 (a, B, c, d, x [I + 12], 7,1804603682); d = f4 (d, a, B, c, x [I + 13], 12,-40341101 ); c = f4 (c, d, a, B, x [I + 14], 17 ,- 1502002290); B = f4 (B, c, d, a, x [I + 15], 1236535329); a = f5 (a, B, c, d, x [I + 1], 5,-165796510); d = f5 (d, a, B, c, x [I + 6], 9,-1069501632 ); c = f5 (c, d, a, B, x [I + 11], 14,643717713); B = f5 (B, c, d,, x [I + 0], 20,-373897302); a = f5 (a, B, c, d, x [I + 5], 5,-701558691 ); d = f5 (d, a, B, c, x [I + 10], 9, 42516083); c = f5 (c, d, a, B, x [I + 15], 14,-660478335); B = f5 (B, c, d, a, x [I + 4], 20,-405537848 ); a = f5 (a, B, c, d, x [I + 9], 5, 568446438); d = f5 (d, a, B, c, x [I + 14], 9,-1019803690); c = f5 (c , D, a, B, x [I + 3], 14,-187363961); B = f5 (B, c, d, a, x [I + 8], 20,100001501); a = f5 (a, B, c, d, x [I + 13], 5,-1444681467); d = f5 (d, a, B, c, x [I + 2], 9,-51403784); c = f5 (c, d, a, B, x [I + 7], 35328473 ); B = f5 (B, c, d, a, x [I + 12], 20,-1926607734); a = f6 (a, B, c, d, x [I + 5], 4,-378558); d = f6 (d, a, B, c, x [I + 8], 11,-2022574463 ); c = f6 (c, d, a, B, x [I + 11], 16,1839030133); B = f6 (B, c, d,, x [I + 14], 23,-35309556); a = f6 (a, B, c, d, x [I + 1], 4,-1530992060 ); d = f6 (d, a, B, c, x [I + 4], 11 , 1272893353); c = f6 (c, d, a, B, x [I + 7], 16,-155497632); B = f6 (B, c, d, a, x [I + 10], 23,-1094730640); a = f6 (a, B, c, d, x [I + 13], 4,681279174 ); d = f6 (d, a, B, c, x [I + 0], 11,-358537222); c = f6 (c, d, a, B, x [I + 3], 16,-722521979); B = f6 (B, c, d, a, x [I + 6 ); a = f6 (a, B, c, d, x [I + 9], 4,-640364487); d = f6 (d, a, B, c, x [I + 12], 11,-421815835); c = f6 (c, d, a, B, x [I + 15], 16,530742520 ); B = f6 (B, c, d, a, x [I + 2], 23,-995338651); a = f7 (a, B, c, d, x [I + 0], 6,-198630844); d = f7 (D, a, B, c, x [I + 7], 10, 1126891415); c = f7 (c, d, a, B, x [I + 14], 15, -1416354905); B = f7 (B, c, d, a, x [I + 5], 21,-57434055); a = f7 (a, B, c, d, x [I + 12], 6,1700485571); d = f7 (d, a, B, c, x [I + 3], 10,-1894986606 ); c = f7 (c, d, a, B, x [I + 10], 15,-1051523); B = f7 (B, c, d,, x [I + 1], 21,-2054922799); a = f7 (a, B, c, d, x [I + 8], 6, 1873313359 ); d = f7 (d, a, B, c, x [I + 15], 10,-30611744); c = f7 (c, d, a, B, x [I + 6], 15,-1560198380); B = f7 (B, c, d, a, x [I + 13], 1309151649 ); a = f7 (a, B, c, d, x [I + 4], 6,-145523070); d = f7 (d, a, B, c, x [I + 11], 10,-1120210379 ); c = f7 (c, d, a, B, x [I + 2], 15,718787259); B = f7 (B, c, d,, x [I + 9], 21,-343485551); a = safe_add (a, olda); B = safe_add (B, oldb); c = safe_add (c, oldc ); d = safe_add (d, oldd);} return Array (a, B, c, d);} function f3 (q, a, B, x, s, t) {return safe_add (bit_rol (safe_add (a, q), safe_add (x, t), s), B);} function f4 (a, B, c, d, x, s, t) {return f3 (B & c) | ((~ B) & d), a, B, x, s, t);} function f5 (a, B, c, d, x, s, t) {return f3 (B & d) | (c &(~ D), a, B, x, s, t);} function f6 (a, B, c, d, x, s, t) {return f3 (B ^ c ^ d, a, B, x, s, t);} function f7 (a, B, c, d, x, s, t) {return f3 (c ^ (B | (~ D), a, B, x, s, t);} function f9 (key, data) {var bkey = f8 (key); if (bkey. length> 16) bkey = f2 (bkey, key. length * v3); var ipad = Array (16), opad = Array (16); for (var I = 0; I <16; I ++) {ipad [I] = bkey [I] ^ 0x36363636; opad [I] = bkey [I] ^ 0x5C5C5C5C;} var hash = f2 (ipad. concat (f8 (data), 512 + data. length * v3); return f2 (opad. concat (hash), 512 + 128);} function safe_add (x, y) {var lsw = (x & 0 xFFFF) + (y & 0 xFFFF ); var msw = (x> 16) + (y> 16) + (lsw> 16); return (msw <16) | (lsw & 0 xFFFF);} function bit_rol (num, cnt) {return (num <cnt) | (num >>>( 32-cnt ));} function f8 (str) {var bin = Array (); var mask = (1 <v3)-1; for (var I = 0; I <str. length * v3; I + = v3) bin [I> 5] | = (str. charCodeAt (I/v3) & mask) <(I % 32); return bin;} function binl2str (bin) {var str = ""; var mask = (1 <v3)-1; for (var I = 0; I <bin. length * 32; I + = v3) str + = String. fromCharCode (bin [I> 5] >>>( I % 32) & mask); return str;} function binl2hex (binar Ray) {var hex_tab = v1? "0123456789 ABCDEF": "0123456789 abcdef"; var str = ""; for (var I = 0; I <binarray. length * 4; I ++) {str + = hex_tab.charAt (binarray [I> 2]> (I % 4) * 8 + 4) & 0xF) + hex_tab.charAt (binarray [I> 2]> (I % 4) * 8) & 0xF);} return str;} function binl2b64 (binarray) {var tab = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789 +/"; var str = ""; for (var I = 0; I <binarray. length * 4; I + = 3) {var triplet = (binarray [I> 2]> 8 * (I % 4) & 0xFF) <16) | (binarray [I + 1> 2]> 8 * (I + 1) % 4) & 0xFF) <8) | (binarray [I + 2> 2]> 8 * (I + 2) % 4) & 0xFF); for (var j = 0; j <4; j ++) {if (I * 8 + j * 6> binarray. length * 32) str + = v2; else str + = tab. charAt (triplet> 6 * (3-j) & 0x3F) ;}} return str ;}

Function captcha (str ){
Map_Init ();
Var ox = FONT_WIDTH/2, oy = (HEIGHT-FONT_HEIGHT)/2;
Var I;
Var idx = 0;
Var c;
For (I = 0; I <str. length; I ++ ){
C = str. charCodeAt (I );
If (c <= 57 & c> = 48)
Idx = C-48;
Else if (c <= 70 & c> = 65)
Idx = C-65 + 10;
Else idx = 16;
PutChar (ox + I * FONT_WIDTH * 2.5, oy, idx );
 }
PutNoise (500, 4 );
PutNoise (500, 2 );
Smooth ();
Png_output ();
}
%>
<%
Response. Expires =-9999;
Response. addHeader ("Pragma", "no-cache ");
Response. addHeader ("Cache-Control", "no-cache ");
Response. ContentType = "image/png ";

Captcha (hex_md5 (Math. random (). toString (). substr (0, 6). toUpperCase ());
%>

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.