生成校验码

checkcode.jsp

<%@ page contentType="image/jpeg"
	import="java.awt.*,java.awt.image.*,java.util.*,javax.imageio.*"%>
<%!Color getRandColor(int fc, int bc) {
		Random random = new Random();
		if (fc > 255)
			fc = 255;
		if (bc > 255)
			bc = 255;
		int r = fc + random.nextInt(bc - fc);
		int g = fc + random.nextInt(bc - fc);
		int b = fc + random.nextInt(bc - fc);
		return new Color(r, g, b);
	}%>
<%
	out.clear();//è¿å¥é对resinæå¡å¨ï¼å¦ææ¯tomacatå¯ä»¥ä¸è¦è¿å¥
	response.setHeader("Pragma", "No-cache");
	response.setHeader("Cache-Control", "no-cache");
	response.setDateHeader("Expires", 0);

	int width = 60, height = 20;
	BufferedImage image = new BufferedImage(width, height,
			BufferedImage.TYPE_INT_RGB);

	Graphics g = image.getGraphics();
	Random random = new Random();

	g.setColor(getRandColor(200, 250));
	g.fillRect(0, 0, width, height);

	g.setFont(new Font("Times New Roman", Font.PLAIN, 18));

	g.setColor(getRandColor(160, 200));
	for (int i = 0; i < 155; i++) {
		int x = random.nextInt(width);
		int y = random.nextInt(height);
		int xl = random.nextInt(12);
		int yl = random.nextInt(12);
		g.drawLine(x, y, x + xl, y + yl);
	}

	String sRand = "";
	for (int i = 0; i < 4; i++) {
		String rand = String.valueOf(random.nextInt(10));
		sRand += rand;

		g.setColor(new Color(20 + random.nextInt(110), 20 + random
				.nextInt(110), 20 + random.nextInt(110)));
		g.drawString(rand, 13 * i + 6, 16);
	}

	//å°è®¤è¯ç å­å¥SESSION
	session.setAttribute("checkcode", sRand);
	g.dispose();

	ImageIO.write(image, "JPEG", response.getOutputStream());
	response.getOutputStream().close();
	out.clear();
	out = pageContext.pushBody();
%>

    login.jsp

    

<div class="code">&nbsp;验证码:<span><input  maxlength="20" size="13" name="checkcode"  class="code_input" id="checkcode"  onKeyUp="this.value=this.value.replace(/\D*(\d*\.?\d{0,2})\d*\D*/g,'$1')"></span>
							   <span><a href="#">
							   <img src="${ctx}/common/checkcode.jsp" width="60" height="20" border="0" class="code_img" id="checkcode_img" onclick="refreshCode();" /></a></span>
						   </div>

    javascript:

   

function refreshCode(){
		$("#checkcode_img").attr("src","${ctx}/common/checkcode.jsp?random="+Math.random());
	}

 

<think>好的,我现在需要回答用户关于在编译程序时生成校验码的方法或工具的问题。首先,我要理解用户的需求。用户可能在开发过程中需要确保代码或数据的完整性,例如在嵌入式系统或数据传输中使用校验码。接下来,我需要回忆编译过程中生成校验码的常见方法。 首先,编译程序通常指的是将源代码转换为可执行文件的过程,而校验码可能用于验证生成的可执行文件或中间文件的完整性。常见的校验码算法有CRC、MD5、SHA等。用户提到的引用中提到了CRC8和CRC校验码,这可能是一个重点。 然后,我需要考虑在编译过程中如何集成校验码生成。可能有几种方法:使用外部工具在编译后生成校验码,或者通过编译器插件在编译阶段自动生成。例如,GCC编译器有插件系统,可以扩展功能。此外,构建工具如Makefile可以在编译步骤后添加生成校验码的命令,比如调用CRC计算工具。 用户提供的引用[2]提到CRC校验码计算考虑了端序模式,这可能涉及到不同平台的兼容性问题,需要注意。引用[3]提到Linux内核的编译过程,可能涉及Makefile的配置,因此可能需要指导用户如何在Makefile中添加生成校验码的步骤。 此外,可能还需要提到具体的工具,如cksum命令用于计算CRC校验,或者openssl工具生成SHA/MD5哈希。用户可能需要知道如何调用这些工具,并将其集成到编译流程中。 最后,应该考虑生成相关问题,比如如何选择校验算法、如何自动化集成到构建系统,以及如何处理端序问题等。这些问题可以帮助用户进一步深入理解。</think>在编译程序时生成校验码通常通过构建流程集成校验算法实现,主要分为编译时校验码注入和构建后处理两种方式。以下是具体方法和工具: ### 一、编译时校验码注入 1. **编译器扩展功能** GCC等编译器可通过`__attribute__((section))`将校验码写入特定段: ```c const uint32_t crc __attribute__((section(".checksum"))) = 0x12345678; ``` 需配合链接脚本定位该段数据[^3]。 2. **预处理阶段生成** 使用Python脚本在编译前计算源码CRC值: ```python import zlib with open("file.c") as f: crc = zlib.crc32(f.read().encode()) ``` 生成头文件包含该值,编译进程序[^2]。 ### 二、构建后处理工具 1. **GNU Binutils工具集** ```bash objcopy --add-section .checksum=<(printf "%08x" $CRC) output.elf ``` 2. **专用校验工具集成** - CRC32计算器(如引用[1]提到的CRC8工具升级版) ```bash crc32 final.bin >> metadata.bin ``` - OpenSSL哈希工具 ```bash openssl sha256 -binary final.bin | xxd -p -c 256 >> checksum.bin ``` 3. **Makefile集成示例** 在Linux内核式构建系统中添加校验步骤: ```makefile final_image: program.elf objcopy $< -O binary temp.bin crc32 temp.bin > checksum.bin cat temp.bin checksum.bin > $@ ``` ### 三、端序处理要点 使用引用[2]提到的`htonl()`系列函数确保校验码字节序与目标平台一致: ```c uint32_t net_order_crc = htonl(raw_crc); ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值