//计算文件MD5,SHA1,CRC32
//Author: lujjjh  
import win.ui;
/*DSG{{*/
var winform = win.form( text="AAuto Hasher - 计算文件的MD5,SHA1,CRC32校验值";acceptfiles=1;bottom=300;parent=...;right=479;border="resizable" )
winform.add( 
progress={ dr=1;dl=1;bottom=284;max=100;right=472;left=8;min=0;font=LOGFONT( name='宋体' );z=1;db=1;top=264;edge=1;cls="progress" };
static={ bottom=21;text="请从外部拖动文件到下面的文本框中:";left=13;dt=1;top=5;transparent=1;right=336;z=3;cls="static" };
richedit={ dr=1;dl=1;vscroll=1;right=472;left=8;dt=1;db=1;cls="richedit";bottom=256;wrap=1;multiline=1;top=25;font=LOGFONT( name='宋体' );z=2;edge=1;readonly=1 }
)
/*}}*/

import crypt
getHash = function (szPath) {
	winform.modifyStyleEx( 0x10/*_WS_EX_ACCEPTFILES*/);   
	winform.richedit.appendText( "正在计算..." , szPath , '\n');
 
 	//定义哈希变量
	var crc32;
	var md5 = crypt().createHashByMd5();
	var sha1 = crypt().createHashBySha1()	
	
	//打开文件
	var file,err =  io.open(szPath, "rb");
	if(!file) error("打开文件失败" + err,2 ) 
 
 	//计算哈希值
 	bufsize = 1024 * 1024;
	var dwSize = file.size(bufsize) 
	var data = file.read(bufsize);  
	winform.progress.step = 1;
	winform.progress.max = dwSize;
	 
	while ( data ){  
		md5.hashData(data);
		sha1.hashData(data); 
		crc32 = string.crc32(data,crc32)
		
		win.peekPumpMessage()
		winform.progress.stepIt()
		data = file.read(bufsize);
	};
	file.close();
	
	//显示结果
	winform.richedit.appendText(
		'MD5:\t' , md5.getHexValue() , '\n',
		'SHA1:\t', sha1.getHexValue(), '\n',
		'CRC32:\t', ..string.format("%X",crc32 ), '\n',
		string.repeat(40, "-") , '\n'
	);
	winform.modifyStyleEx( ,0x10/*_WS_EX_ACCEPTFILES*/);
}

winform.wndproc = function(hwnd,message,wParam,lParam){
	select(message) {//判断消息类型
		case 0x233/*_WM_DROPFILES*/ {
			var szPath = win.getDropFile(wParam);
			for (k,v in szPath) {
				getHash(v);
			}
		}
	}
}

winform.progress.bottom = winform.progress.bottom - 8;
winform.show();
win.loopMessage();
