Coder Social home page Coder Social logo

xobotyi / basen Goto Github PK

View Code? Open in Web Editor NEW
12.0 12.0 3.0 25 KB

Text encoding utilities for PHP with no extensions dependencies. Base32, Base58, Base64 and much more!

License: MIT License

PHP 100.00%
base16 base32 base36 base58 base62 base64 binary encoding php7 rfc4648 text

basen's People

Contributors

peter279k avatar xobotyi avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

basen's Issues

Intolerant of truncated data, inelegant failure mode

I've noticed that this library has the same problem as the Tuupola Base62 encoder in that it is intolerant of corruption - even a single bit error will destroy the entire output. This is not true of PHP's built-in base64 encoders.

To be fair, this is an edge case, but URLs cannot always relied on to remain intact - for example it's common for email clients to truncate them, and if this happens, the URL will always break completely if it used this encoder.

I suspect it fails for exactly the same reason - the encoding treats the entire string as single arbitrary precision number, and so it results in the corruption we see. If the encoder used a chunk based approach (which is especially appropriate for base62 since it does not fit evenly into 8-bit chars), it would not suffer this fragility.

Steps to reproduce, backtrace or example script

$str = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ01234567';
$e = \xobotyi\basen\Base62::encode($str);
$d = \xobotyi\basen\Base62::decode($e);
$t = \xobotyi\basen\Base62::decode(substr($e, 0, -1));
$b6 = base64_encode($str);
$b6d = base64_decode($b6);
$b6td = base64_decode(substr($b6, 0, -1));
echo 'Original string: ', $str, "\n";
echo 'Base62 Encoded string:  ', $e, "\n";
echo 'Decoded Base62 string:  ', $d, "\n";
echo 'Decoded Base62 with 1 char truncated: ', $t, "\n";
echo 'Base64 Encoded string:  ', $b6, "\n";
echo 'Decoded Base64 string:  ', $b6d, "\n";
echo 'Decoded Base64 with 1 char truncated: ', $b6td, "\n";

Example output:

Original string: abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ01234567
Base62 Encoded string:  4P1nFLX8O0GCD8E3Xwm0iDXG3fUXK4r0ErC9FylJ2b83x5frxW82yb3kAOYylUnbN1ljw2kWvQmiaHo5p
Decoded Base62 string:  abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ01234567
Decoded Base62 with 1 char truncated: ���`�G�`�܎���"�d�3i��3y�`��S�d6ϙ�}��6��\&}7	�l�}O���!ۮG�
Base64 Encoded string:  YWJjZGVmZ2hpamtsbW5vcHFyc3R1dnd4eXpBQkNERUZHSElKS0xNTk9QUVJTVFVWV1hZWjAxMjM0NTY3
Decoded Base64 string:  abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ01234567
Decoded Base64 with 1 char truncated: abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.