为什么为相同代码和相同 .net 框架版本生成的 .net dll 的二进制文件不相同?

发布于 2024-10-31 21:59:53 字数 273 浏览 0 评论 0原文

我有一个 C# 项目,当在不同的机器上编译时,会生成二进制不相等的 dll。所以我的问题是为什么生成的dll不同?有没有办法让不同的机器上生成完全相同的dll?

编辑 这就是我正在尝试做的事情。有多个客户端计算机从服务器获取一些代码片段并编译它。 dll编译完成后,会在客户端反复使用。如果我可以在所有计算机上生成相同的 DLL,那么我可以使用加密哈希轻松检查 DLL 是否在客户端被篡改。

由于某些原因,代码必须在客户端计算机上编译。因此,数字签名不是一种选择。

I have a C# project which when compiled on different machines generate dlls which are not binary equal. So my question is why the generated dlls are different? and is there some way to get exactly same dll generated on different machines?

EDIT
Here is what i am trying to do. There are multiple client machines which get some code snippet from a server and compile it. After the dll is compiled, it get used over and over again on the client. If I could have the same DLLs generated on all machines then I can easily check that the DLL is not tampered with on the client side using cryptographic hash.

For some reasons the code has to be compiled at the client machine. Therefore digital signing is not an option.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

世俗缘 2024-11-07 21:59:53

您不能期望重新创建的任何文件的二进制文件都相同 - 更不用说程序集了。所有文档元数据包括创建数据、修改日期都会不同。

如果需要比较,则需要对程序集进行签名(强命名)并比较公钥令牌加程序集版本。


更新

你所做的就是自找麻烦,尽管我可以理解有时必须这样做。如果我是你,我会在服务器上编译二进制文件并让客户端下载二进制文件而不是代码。最终,客户的计算机上可能没有 C# 编译器。

为了回答你的问题,如果你坚持在客户端编译,每次编译时,创建一个本地哈希并存储在注册表、某个文件等中,然后比较该哈希。


UPDATE 2

C# 编译器从不保证它将创建相同的二进制文件。许多东西都是在编译时创建的,例如命名匿名函数、自动属性的支持值、匿名方法、内部 GUID...所有这些都将在编译时创建,而编译器使用命名约定,其名称通常以同样,无法保证

You cannot expect binaries to be same for any file recreated - let alone assemblies. All document metadata including data created, date modified will be different.

If you need to compare, you need to sign the assembly (strong naming) and compare the public key token plus assembly version.


UPDATE

What you are doing is asking for trouble although I can understand sometimes this has to be done. If I were you, I would compile the binary on server and get the clients to download the binary and not the code. At the end of the day, clients might not have the C# compiler on their machine.

To answer your question, if you insist on compiling on client, everytime you compile, create a local hash and store in registry, some file, etc and then compare that hash.


UPDATE 2

C# compiler never guarantees that it is going to create the same binary. Many things are created at the compiled time for example naming anonymous functions, backing value for automatic properties, anonymous methods, internal GUIDs... all of these will be created at compile time and while compiler uses a naming convention which names end up usually the same, it is not guaranteed.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文