1

I am new to PHP scripting. I developed a VB.net application in which I am using GetHashCode() function for generating activation key. Now I want to generate activation key using PHP.

Can anyone help me to get the equivalent PHP function? Below I have written the example of VB.Net.

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

Output:

-695839

halfer
  • 19,824
  • 17
  • 99
  • 186
Krishna
  • 27
  • 1
  • 8
  • Check out this related post http://stackoverflow.com/questions/8804875/php-internal-hashcode-function – xspydr Jan 17 '14 at 17:33
  • 4
    The implementation of GetHashCode() is undefined in .net and can and does change between versions, use a specified hash function like SHA256 – user1937198 Jan 17 '14 at 17:34
  • 1
    See also: http://blogs.msdn.com/b/ericlippert/archive/2011/02/28/guidelines-and-rules-for-gethashcode.aspx - `GetHashCode` on the .Net side "Consumers of GetHashCode cannot rely upon it being stable over time or across appdomains" – Rowland Shaw Jan 17 '14 at 17:35
  • And http://stackoverflow.com/questions/2099998/hash-quality-and-stability-of-string-gethashcode-in-net – user1937198 Jan 17 '14 at 17:38

1 Answers1

1

No, you can't do that with 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.

You need to use some custom code both sides agree on.

I.e. use SHA256 to compute hash of the string - there are many samples how to compute hash in either language i.e. hmac_sha256 in php and c# differ covers both. If writing code yourself make sure to understand how to encode strings (i.e. UTF8) and result (i.e. Hex or Base64).

Community
  • 1
  • 1
Alexei Levenkov
  • 98,904
  • 14
  • 127
  • 179