note 86239 added to function.sha1

From: Date: Thu, 09 Oct 2008 01:28:32 +0000
Subject: note 86239 added to function.sha1
Groups: php.notes 
Request: Send a blank email to php-notes+get-145197@lists.php.net to get a copy of this message
Here's a better version of the getDigestNotation() function I posted earlier. (The first version had a bug in the argument checking.) <?php function getDigestNotation($rawDigest, $bitsPerCharacter, $chars = NULL) { if ($chars === NULL || strlen($chars) < 2) { $chars = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-,'; } if ($bitsPerCharacter < 1) { // $bitsPerCharacter must be at least 1 $bitsPerCharacter = 1; } elseif (strlen($chars) < pow(2, $bitsPerCharacter)) { // Character length of $chars is too small for $bitsPerCharacter // Set $bitsPerCharacter to greatest value allowed by length of $chars $bitsPerCharacter = 1; $minCharLength = 2; while (strlen($chars) >= ($minCharLength *= 2)) { $bitsPerCharacter++; } unset($minCharLength); } $bytes = unpack('C*', $rawDigest); $byteCount = count($bytes); $out = ''; $byte = array_shift($bytes); $bitsRead = 0; for ($i = 0; $i < $byteCount * 8 / $bitsPerCharacter; $i++) { if ($bitsRead + $bitsPerCharacter > 8) { // Not enough bits remain in this byte for the current character // Get remaining bits and get next byte $oldBits = $byte - ($byte >> 8 - $bitsRead << 8 - $bitsRead); if (count($bytes) == 0) { // Last bits; match final character and exit loop $out .= $chars[$oldBits]; break; } $oldBitCount = 8 - $bitsRead; $byte = array_shift($bytes); $bitsRead = 0; } else { $oldBitCount = 0; } // Read only the needed bits from this byte $bits = $byte >> 8 - ($bitsRead + ($bitsPerCharacter - $oldBitCount)); $bits = $bits - ($bits >> $bitsPerCharacter - $oldBitCount << $bitsPerCharacter - $oldBitCount); $bitsRead += $bitsPerCharacter - $oldBitCount; if ($oldBitCount > 0) { // Bits come from seperate bytes, add $oldBits to $bits $bits = ($oldBits << $bitsPerCharacter - $oldBitCount) | $bits; } $out .= $chars[$bits]; } return $out; } ?> ---- Server IP: 208.69.120.35 Probable Submitter: 207.88.114.211 ---- Manual Page -- http://www.php.net/manual/en/function.sha1.php Edit -- https://master.php.net/note/edit/86239 Del: integrated -- https://master.php.net/note/delete/86239/integrated Del: useless -- https://master.php.net/note/delete/86239/useless Del: bad code -- https://master.php.net/note/delete/86239/bad+code Del: spam -- https://master.php.net/note/delete/86239/spam Del: non-english -- https://master.php.net/note/delete/86239/non-english Del: in docs -- https://master.php.net/note/delete/86239/in+docs Del: other reasons-- https://master.php.net/note/delete/86239 Reject -- https://master.php.net/note/reject/86239 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#145197) next »