简体   繁体   English

什么等效于.Net在PHP中的GetHashCode()?

[英]What is the equivalent of .Net's GetHashCode() in PHP?

I am new to PHP scripting. 我是PHP脚本的新手。 I developed a VB.net application in which I am using GetHashCode() function for generating activation key. 我开发了一个VB.net应用程序,其中使用了GetHashCode()函数来生成激活密钥。 Now I want to generate activation key using PHP. 现在,我想使用PHP生成激活密钥。

Can anyone help me to get the equivalent PHP function? 谁能帮助我获得等效的PHP函数? Below I have written the example of VB.Net. 下面我写了VB.Net的示例。

Dim MyString As String = "hello"
MsgBox(MyString.GetHashCode)

Output: 输出:

-695839 -695839

No, you can't do that with String.GetHashCode . 不,您不能使用String.GetHashCode做到这String.GetHashCode

You can not persist result of String.GetHashCode as it may/will change between launches of the same program - so even if there is "equivalent" method in PHP you can't use really compare they results. 您不能持久化String.GetHashCode结果,因为它可能/将在同一程序的启动之间改变-因此,即使PHP中存在“等效”方法,您也无法真正使用它们来比较它们的结果。

You need to use some custom code both sides agree on. 您需要使用双方都同意的一些自定义代码。

Ie use SHA256 to compute hash of the string - there are many samples how to compute hash in either language ie hmac_sha256 in php and c# differ covers both. 即使用SHA256来计算字符串的哈希-有很多示例如何使用任一语言来计算哈希,即php和c#中的hmac_sha256都涵盖了两者。 If writing code yourself make sure to understand how to encode strings (ie UTF8) and result (ie Hex or Base64). 如果您自己编写代码,请确保了解如何编码字符串(即UTF8)和结果(即Hex或Base64)。

Yes, in .NET you can persist GetHashCode() results when using ValueTypes, the approach mentioned is only for Object (ReferenceTypes). 是的,在.NET中,您可以在使用ValueTypes时保留GetHashCode()结果,所述方法仅适用于Object(ReferenceTypes)。

https://msdn.microsoft.com/pt-br/library/system.object.gethashcode(v=vs.110).aspx https://msdn.microsoft.com/pt-br/library/system.object.gethashcode(v=vs.110).aspx

Cheers 干杯

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM