一个解决方案来验证使用文件签名(.NET)的幻数的文件类型? [英] A solution to validate a file type using magic number in file signature (.NET)?

查看:136
本文介绍了一个解决方案来验证使用文件签名(.NET)的幻数的文件类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想验证一个类型的上传文件(假设该文件具有不正确的扩展名)。

I want to validate a type of an uploaded file (suppose the file has incorrect extension).

我想,这可以通过检查文件签名的幻数来完成。同样,因为它是在UNIX 的完成。所以我想我需要阅读的文件的第一个字节,并将它们与幻数映射比较(我需要有映射,我需要保持他们)。

I figure it could be done by checking the magic number of file signature. Similarly, as it is done in unix. So I guess I need to read the first bytes of the file and compare them with the magic number mappings (and I need to have the mappings and I need to maintain them).

所以,我想也许还有一类是在那里只是为了这个目的吗?或者,也许有人已经这样做了,可以在该解决方案分享他们的见解?谢谢

So I thought maybe there is a class out there just for this purpose? Or maybe someone has done this and can share their insights on the solution? Thanks

推荐答案

我怀疑有此问题的银弹解决方案。没有人说,每个文件格式有任何形式的幻数。考虑纯文本文件 - 根本没有合理的指示(可能的字节顺序标记旁一开始,然而,并不一定意味着什么)和纯启发式必须用来猜测它是一个纯文本文件。像明智地认识到一个XML或HTML(即SGML)文件,它是指以查了很多可能性。

I doubt there's a "silver bullet" solution for this problem. Nobody says every file format has any form of a magic number. Consider plain text files — no reasonable indication at all (beside a possible byte-order-mark at the beginning which, however, need not mean anything) and pure heuristics must be used to guess it's a plain text file. Like wise to recognize an XML or HTML (i.e. SGML) file it means to check a lot of possibilities.

您应该采取的办法是:

  • 缩小下来的一组预期的文件格式。
  • 要么做它自己的基础上,整体结构和/或文件格式的特性,你需要支持或者寻找类似的 GNU文件提到重新运行。您还可以在网站,如 MagicDB 一看 - 但是,这个人是特别过时(2005年)<。 / LI>
  • 指定意外的文件格式的行为。
  • Narrow down the set of expected file formats.
  • Either do-it-yourself based on the knowledge of the overall structure and/or characteristics of the file formats you need to support or look for a solution like GNU File mentioned by rerun. You can also have a look on sites like MagicDB — however, this one is particularly outdated (year 2005).
  • Specify behavior for unexpected file formats.

在情况设置窄够了我会考虑一个简单的自定义解决方案是正确的。

In case the set is narrow "enough" I'd consider a simple custom solution as the right one.

这篇关于一个解决方案来验证使用文件签名(.NET)的幻数的文件类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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