
decryptId() checks if the second part of the base64 encoded string is correct. encryptionKey is a global secret key for encryption. The optional 2nd argument is the class name, so two equal ids of different tables will not result in two equal obfuscated ids. Return substr(sha1($class.$int.encryptionKey), 0, 6) = $parts $parts = explode('-', base64url_decode($string)) Return base64url_encode($int.'-'.substr(sha1($class.$int.encryptionKey), 0, 6)) My solution (uses modified base64 functions by Tom): The users should not be able to increment the id in the URL and see the next data record in the database table. I needed a simple way to obfuscate auto_increment primary keys in databases when they are visible to users in URIs or API calls. fputs($fh2, base64_decode($put)) // for comparing Here comes the corrected version which I have tested for all kind of files and length (up to 4,5 Gb!) without any error: The error was caused by the use of "empty()". But lower values then 1024 are bad overall because they slow down the whole process, so 4096 will be fine for all purposes, I guess. The first led to an endless loop because of a missing "$feof"-check, the second caused the rare mentioned errors when encoding failed for some reason in larger files, especially when Unfortunately my "function" for encoding base64 on-the-fly from 2007 had 2 errors!

However, this does make the assumption that the encoded data really is MIME-formatted without that assurance it is necessary to accept that the base64 data won't be so conveniently arranged. What that means is that each line can be decoded independently of the others, and the decoded chunks can then be concatenated together or written out sequentially. Obviously the last chunk will probably be shorter, but encoding it is still independent of the rest.Ĭonversely, each 76-character MIME-formatted line (not counting the trailing CRLF) contains exactly enough data for 57 bytes of output without needing to retain leftover bits that need prepending to the next line. Just read a chunk, encode it, write it out, and go on to the next chunk.

There is no need to retain left-over symbols (either six- or eight-bit) from one chunk to the next. So if you read from the input file in chunks of 8151 (=57*143) bytes you will get (up to) 8151 eight-bit symbols, which encode as exactly 10868 six-bit symbols, which then wrap to exactly 143 MIME-formatted lines. Finally, PHP's default buffer size is 8192 bytes - enough for 143 MIME lines' worth of input. Reading 57 eight-bit symbols provides exactly enough data for a complete MIME-formatted line.

76 characters is enough for 19 quadruples of six-bit symbols thus representing 19 triples of eight-bit symbols.
COMMAND LINE DECODE BASE64 PLUS
MIME additionally enforces a line length of 76 characters plus the CRLF. Reading the input file in chunks that are a multiple of three bytes in length results in a chunk that can be encoded independently of the rest of the input file.
COMMAND LINE DECODE BASE64 ARCHIVE
Getting Started Introduction A simple tutorial Language Reference Basic syntax Types Variables Constants Expressions Operators Control Structures Functions Classes and Objects Namespaces Enumerations Errors Exceptions Fibers Generators Attributes References Explained Predefined Variables Predefined Exceptions Predefined Interfaces and Classes Predefined Attributes Context options and parameters Supported Protocols and Wrappers Security Introduction General considerations Installed as CGI binary Installed as an Apache module Session Security Filesystem Security Database Security Error Reporting User Submitted Data Hiding PHP Keeping Current Features HTTP authentication with PHP Cookies Sessions Dealing with XForms Handling file uploads Using remote files Connection handling Persistent Database Connections Command line usage Garbage Collection DTrace Dynamic Tracing Function Reference Affecting PHP's Behaviour Audio Formats Manipulation Authentication Services Command Line Specific Extensions Compression and Archive Extensions Cryptography Extensions Database Extensions Date and Time Related Extensions File System Related Extensions Human Language and Character Encoding Support Image Processing and Generation Mail Related Extensions Mathematical Extensions Non-Text MIME Output Process Control Extensions Other Basic Extensions Other Services Search Engine Extensions Server Specific Extensions Session Extensions Text Processing Variable and Type Related Extensions Web Services Windows Only Extensions XML Manipulation GUI Extensions Keyboard Shortcuts ? This help j Next menu item k Previous menu item g p Previous man page g n Next man page G Scroll to bottom g g Scroll to top g h Goto homepage g s Goto searchīase64 encoding converts triples of eight-bit symbols into quadruples of six-bit symbols.
