Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:61075 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 21461 invoked from network); 3 Jul 2012 00:49:06 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 3 Jul 2012 00:49:06 -0000 Authentication-Results: pb1.pair.com header.from=ircmaxell@gmail.com; sender-id=pass Authentication-Results: pb1.pair.com smtp.mail=ircmaxell@gmail.com; spf=pass; sender-id=pass Received-SPF: pass (pb1.pair.com: domain gmail.com designates 209.85.216.49 as permitted sender) X-PHP-List-Original-Sender: ircmaxell@gmail.com X-Host-Fingerprint: 209.85.216.49 mail-qa0-f49.google.com Received: from [209.85.216.49] ([209.85.216.49:52234] helo=mail-qa0-f49.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 7F/98-13131-28142FF4 for ; Mon, 02 Jul 2012 20:49:06 -0400 Received: by qabj40 with SMTP id j40so2534746qab.8 for ; Mon, 02 Jul 2012 17:49:03 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :cc:content-type:content-transfer-encoding; bh=vNaFB2FeDnV2Bgdr5YL3HheUcKEXgae8AzGPTomXVTc=; b=MlQiVZSPc4LH1yVfnGMmVd7MMDAG6KM9qXvfbGAY+z/LPQ3mHFBXaOyfHV0VWhRtvU gGhr6EvvxddWfXRHYUAVykW0VmD7ijG+9EOy3KUqhL633WQ6l0kP7ugn/359aDmM1hCG 03Y+w00HjZiDIRdG8HriZComjtqc2zpIdsLelrrbOLh2LHEsOdHoqBxUipcfhVG6gC+z 5to0CQ+gdN+i88sdPG1lH2zQSPNdTs4Kw1RiRCNP/HEOnMMD8RybpMlgikP0Ds5l20d4 06JMwR6ABzcN90VtKbqFzW8Vto5aE3baj0Qv1Hkh6BD9t26AJVS64jIM44V02NS4LZBc 2qFw== MIME-Version: 1.0 Received: by 10.229.105.163 with SMTP id t35mr7811252qco.49.1341276543061; Mon, 02 Jul 2012 17:49:03 -0700 (PDT) Received: by 10.229.232.11 with HTTP; Mon, 2 Jul 2012 17:49:03 -0700 (PDT) In-Reply-To: References: Date: Mon, 2 Jul 2012 20:49:03 -0400 Message-ID: To: Sara Golemon Cc: internals@lists.php.net Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable Subject: Re: [PHP-DEV] [VOTE] Hash_pbkdf2 From: ircmaxell@gmail.com (Anthony Ferrara) Sara, On Mon, Jul 2, 2012 at 8:24 PM, Sara Golemon wrote: > I'd like to see hash_init() support this mode as well (for completeness). > =A0Perhaps something like the following: > > $ctx =3D hash_init("sha512", HASH_PBKDF2, $salt,=A0array('length' =3D> 32= , > 'iterations' =3D> 5000)); > > The new fourth parameter being an overloadable options generic so that we > don't have an endless list of parameters for all the possible hashing typ= es. The only problem that I have with this sort of implementation is that the only way it would work is to buffer the entire input (each hash_update call), and run it all at the end. That's because the data (password) is used in every iteration, so there's no pre-computation that can be done. And at that point, what's the benefit to it? public function pbkdf2($algo, $password, $salt, $iterations, $length) { $size =3D getHashBlockSize($hash); $len =3D ceil($length / $size); $result =3D ''; for ($i =3D 1; $i <=3D $len; $i++) { $tmp =3D hash_hmac($hash, $salt . pack('N', $i), $password, tru= e); $res =3D $tmp; for ($j =3D 1; $j < $iterations; $j++) { $tmp =3D hash_hmac($hash, $tmp, $password, true); $res ^=3D $tmp; } $result .=3D $res; } return substr($result, 0, $length); } Contrast that to a normal hash function (used by the current hash_init) which iterates over blocks of input, and once it's done with a block, it doesn't need it anymore (which is where using something like hash_init/hash_update can make sense on large input). Additionally, hmac only uses the message in a single hash, so it can be streamed in as well (pre-compute the earlier hashes/data, stream the intermediate hash, then hash the final one). So while it could be added (just make hash_update buffer into a context, then have hash_final actually run the derivation), I'm not sure it makes sense to add it there. Thoughts? Anthony