NotionCommotion Posted December 28, 2015 Share Posted December 28, 2015 Using class https://github.com/dflydev/dflydev-base32-crockford, I am trying to encode a random number. Below is my failed attempt. Trying to typecast the $decimal into an integer also sets it to zero. How is this best accomplished? Thanks $binary=mcrypt_create_iv(16, MCRYPT_DEV_URANDOM); echo($binary."\n"); $hex=bin2hex($binary); echo($hex."\n"); $decimal=hexdec($hex); echo($decimal."\n"); $encodedValue = Crockford::encode($decimal); echo($encodedValue."\n"); 4?1V*8G?ۑL?? 34fe31562ac28b3847f0db914cfe19cf 7.043969984781E+37 0 Quote Link to comment https://forums.phpfreaks.com/topic/300032-encoding-using-crockfords-base32/ Share on other sites More sharing options...
Jacques1 Posted December 28, 2015 Share Posted December 28, 2015 (edited) If this library sucks, just use a different one. I've tried skleeschulte/php-base32, and it seems to work fine: <?php require_once __DIR__.'/Base32.php'; use SKleeschulte\Base32; $rawInput = mcrypt_create_iv(16, MCRYPT_DEV_URANDOM); $encoded = Base32::encodeByteStrToCrockford($rawInput); echo "encoded: $encoded<br>"; $decoded = Base32::decodeCrockfordToByteStr($encoded); echo ($decoded === $rawInput) ? 'decoding successful' : 'decoding failed'; Edited December 28, 2015 by Jacques1 Quote Link to comment https://forums.phpfreaks.com/topic/300032-encoding-using-crockfords-base32/#findComment-1528716 Share on other sites More sharing options...
NotionCommotion Posted December 28, 2015 Author Share Posted December 28, 2015 Thanks Jacques, Seems to work fine. According to http://www.crockford.com/wrmg/base32.html, it is used for numbers, and the earlier class seemed to only work with numbers as well. This document describes a 32-symbol notation for expressing numbers in a form that can be conveniently and accurately transmitted between humans and computer systems. The new number of characters always appears to be 26. Why is that? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/300032-encoding-using-crockfords-base32/#findComment-1528718 Share on other sites More sharing options...
Jacques1 Posted December 28, 2015 Share Posted December 28, 2015 According to http://www.crockford.com/wrmg/base32.html, it is used for numbers, and the earlier class seemed to only work with numbers as well. Every byte sequence can be interpreted as a number (e. g. as the binary representation of an integer). However, the class uses PHP integers, and that means the input is limited to just a few bytes. A more reasonable approach is to encode the input bitwise, which is what the second class does. This way there are no specific limits. The new number of characters always appears to be 26. Why is that? I assume you're talking about 128-bit strings. In Base32, every digit carries 5 bits. So you need ceil(128/5) = 26 digits to represent 128 bits. Quote Link to comment https://forums.phpfreaks.com/topic/300032-encoding-using-crockfords-base32/#findComment-1528722 Share on other sites More sharing options...
NotionCommotion Posted December 28, 2015 Author Share Posted December 28, 2015 I assume you're talking about 128-bit strings. In Base32, every digit carries 5 bits. So you need ceil(128/5) = 26 digits to represent 128 bits. Yes I was. On the way to work, thought about it another way. 2^128=32^x, then x=128*log(2)/log(32)=25.6. Been a while since I've done this! Now I see 32=2^5, so really the same as your example. Not a PHP question but a UX question..... Would you rather type in 26 alphanumerical values or 39 numbers? Quote Link to comment https://forums.phpfreaks.com/topic/300032-encoding-using-crockfords-base32/#findComment-1528733 Share on other sites More sharing options...
Jacques1 Posted December 28, 2015 Share Posted December 28, 2015 Not a PHP question but a UX question..... Would you rather type in 26 alphanumerical values or 39 numbers? I'd definitely prefer the shorter ID with 26 digits. Quote Link to comment https://forums.phpfreaks.com/topic/300032-encoding-using-crockfords-base32/#findComment-1528736 Share on other sites More sharing options...
Psycho Posted December 28, 2015 Share Posted December 28, 2015 If the number is 26 characters or more, I would copy/paste. So, the length would be inconsequential. Quote Link to comment https://forums.phpfreaks.com/topic/300032-encoding-using-crockfords-base32/#findComment-1528739 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.