Page MenuHomePhabricator

Use md5() instead of hash() for hashing
Closed, ResolvedPublic

Description

I just tested usage of #suboject in SMW 1.7.0.2 and 1.7.1. Both versions give the follow error on page save, with the action=submit URL parameter:

Fatal error: Call to undefined function hash_init() in /path/to/extensions/SemanticMediaWiki/includes/SMW_SemanticData.php on line 172

The URL does not load, but returning to view the page reveals that the edit was never saved. I tried this with a transcluded template to get around the page save, and the results were the same. This was the minimum required text to trigger the bug:


Version: unspecified
Severity: minor

Details

Reference
bz38185

Event Timeline

bzimport raised the priority of this task from to High.Nov 22 2014, 12:56 AM
bzimport set Reference to bz38185.
bzimport added a subscriber: Unknown Object (MLST).

The trigger does not seem to work on SMW 1.8 Alpha:

http://scratchpad.referata.com/wiki/Main_Page/Bug_38185

I don't know if there's some other factor involved or not, but the release notes do not mention any changes affecting #subobject:

https://gerrit.wikimedia.org/r/gitweb?p=mediawiki/extensions/SemanticMediaWiki.git;a=blob;f=RELEASE-NOTES

Unknown Object (User) added a comment.Jul 8 2012, 11:55 PM

The above message "Call to undefined function hash_init()" shows that the PHP used does not inherent this function (has nothing to do with SMW).

hash_init is available from (PHP 5 >= 5.1.2, PECL hash >= 1.1) onwards and with [1] stating clear that PHP 5.2 is required therefore this is not a SMW bug.

Note

[1] http://semantic-mediawiki.org/wiki/Help:Installation

I don't think the reason for closing this is correct, so I'm reopening it. This bug was found on a much more recent version of PHP than you described. Here's the output of php --version:

PHP 5.3.14 with Suhosin-Patch (cli) (built: Jun 22 2012 09:31:19)
Copyright (c) 1997-2012 The PHP Group
Zend Engine v2.3.0, Copyright (c) 1998-2012 Zend Technologies

Some PHP distributions disable hashing support by default; esp. this is reported for FreeBSD. You could install the according PHP functions. This makes the bug far less critical.

However, there is also a function "md5()" in PHP that we might prefer to use, since it is apparently not causing problems on your site (MW uses it in many places, maybe out of similar considerations). It should also be quite fast. I change this bug to ask for this change now.

Ultimately fixed in https://gerrit.wikimedia.org/r/#/c/28399/

However, the only function that still used the problematic hash methods is not currently called in SMW. Hence, the bug should have disappeared earlier already.

I encountered this bug in SMW 1.8.0.5, in SMW_Show:

http://wikimedia.7.x6.nabble.com/SMW-1-8-PHP-Fatal-error-Call-to-undefined-function-hash-td5006421.html#a5006484

I had forgotten that I had already reported this issue here. It is FreeBSD where we encountered the problem. I'm reopening it based on Markus's comments that indicate this should be fixed. I'm not sure, but maybe the problem in 1.8.0.5 is actually a regression, and not really the same bug as this one? If so, it might be better to close this bug as FIXED again, and then open a new one.

hash_init is no longer present in 1.8.

Comment 4 indicates the problem also lies in hash(), which is used in 1.8.0.5 on line 43 in this file:

\path\to\SemanticMediaWiki\includes\parserhooks\SMW_Show.php

This is the line in use:

$queryKey = hash( 'md4', implode( '|', $rawParams ) , false );

Comment 5 indicates that hash() was not being used in SMW 1.7, so this may indeed be a regression. Jeroen, should I open a separate bug for it, or can we keep it all in one place here?

This bug is fine. Indeed still using hash. For md4... Not sure what a better alternative is, of why this is breaking for you.

Markus says in comment #4 that some PHP distributions disable hashing support by default. He mentioned FreeBSD is one of them does that, and that's what my server is using. He also suggests that we could use md5() instead of hash() to eliminate the problem, and ensure wider compatibility. I don't know why hash() is disabled by default in some PHP distributions. I've never heard of this problem for anything except SMW, but the PHP gurus seemed to be familiar with it, and I assume they agree with Markus to just use MD5 instead.

I did some researching about MD4 versus MD5, and I found out that MD4 has much faster performance. Because of that, simply switching to md5() instead of hash() might not be the ideal solution. Instead, the problem could be solved by checking for hash(), and if it is unavailable, switch to using md5(). If that's what ends up being done, then it should be mentioned in the documentation somewhere that enabling hash() will result in a small performance improvement.

Unknown Object (User) added a comment.Aug 28 2013, 12:12 AM

SMW 1.9 ([1], [2]) relies on \SMW\HashIdGenerator to generate appropriate information.

[1] https://gerrit.wikimedia.org/r/#/c/80502/
[2] https://gerrit.wikimedia.org/r/#/c/75286/