Jump to content

Recommended Posts

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
Link to comment
https://forums.phpfreaks.com/topic/300032-encoding-using-crockfords-base32/
Share on other sites

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 by Jacques1

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

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.

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?

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.