以编程方式更改文件的散列而不会破坏它 [英] Programatically changing hash of a file without corrupting it

查看:142
本文介绍了以编程方式更改文件的散列而不会破坏它的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我读过关于在文件末尾追加一个空字节的问题,从而改变了文件的结尾MD5不会破坏它。任何人都有任何信息?



我希望这样做的语言是PHP。



谢谢。

p>

解决方案

这取决于应用程序在读取此文件时的期望值。例如,如果它是一个文本文件,您可以简单地在其中一个段落之后插入一个空格。这不会改变人们对文件的可读性,但它会改变MD5。



对于基本的HTML文件或源文件,例如C或PHP,无关紧要(只要你将空间插入语法无关紧要的区域,例如不在字符串常量内)。在最后加入一些空格或添加换行符,您会发现网页的行为不会发生变化。



然而,这不太可能适用于一个可执行文件,因为它可能会在运行时崩溃和烧毁(如果确实它加载 - 一些加载器可能会使用加载部分的校验和)。

您需要详细说明在你谈论的情况下腐败意味着什么。



更新:

例如,在JPEG文件中,可能很简单,就是用一个唯一的COM部分替换EOI标记,然后用一个EOI标记替换。 EOI标记是图像的结尾,您应该能够在它之前插入注释部分(带有独特的注释)。这将使每个JPEG具有不同的MD5,而stil呈现相同的图像。请参阅此处



使用ZIP文件,因为最后的目录列出了偏移量的文件,所以实际上可以在每个文件之间插入任意数据。有关详细信息,请参见此处。不幸的是,我对RAR文件的内部不熟悉。


Does anyone have any info on changing the hash of a file without corrupting it?

I read about appending a null byte to the end of the file, thus changing the MD5 without corrupting it. Anyone have any info?

The language I wish to do this in is PHP.

Thanks.

解决方案

It depends on exactly what the applications expect when they read this file. If, for example, it's a text file, you could simply insert a space following one of the paragraphs. This doesn't change the readability of the file by humans but it will change the MD5.

Likewise for basic HTML files or source files such as C or PHP where the spacing doesn't matter (as long as you insert the space in a syntactically insignificant area, so not inside string constants for example) . Put in some extra spaces or add newline characters at the end and you'll find the behavior of your web pages doesn't change.

However this is unlikely to work for an executable file since it will probably crash and burn when you run it (if indeed it even loads - some loaders may use checksums for the load sections).

You need to specify exactly what corruption means in the case you're talking about.

Update:

For example, in JPEG files, it's probably a simple matter of replacing the EOI marker at the end with a unique COM section followed by an EOI marker. The EOI marker is the end of image and you should be able to insert the comment section (with a unique comment) before it. This would make each JPEG have a different MD5 while stil presenting the same image. See here.

With ZIP files, you can actually insert arbitrary data in between each file since the catalog at the end lists files with their offsets. See here for details. Unfortunately, I'm not familiar with the internals of RAR files.

这篇关于以编程方式更改文件的散列而不会破坏它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆