IPv4 Fremnet Logo
TOOLS, TINKERINGS & CODE

If it helps

Class: Base32 · Nov 8, 16:51 by Shannon Wynter

This class can be used to encode data in MIME Base32 encoding according to the RFC 3548 specification.

There are 3 output character sets available:

  • RFC 3548 (A-Z2-7)
  • Safe (0-9A-HJKMNP-TV-Z)
  • Hex like (0-9A-V)

Base32 (spelled without a space) is an alternative to Base64 as a notation for encoding arbitrary byte data using a restricted set of symbols which can be conveniently used by humans and processed by old computer systems which only recognize restricted character sets.

I included three character sets becaues I beleive each of them is important to interoperability.

Hex like character set
Some earlier implementations of Base32 use a Hex(Hexadecimal) like (0-9A-V) character set, so it stands to reason that you might need to comunicate with some old peice of software at this level.

RFC 3548 character set
The RFC 3548 (A-Z2-7) character set is included because it’d be silly not to include it.

Safe character set
The Safe (0-9A-HJKMNP-TV-Z) charset is implemented as suggested by Douglas Crockford and is useful specificly when dealing with humans.

The most common input errors are delt with very simply in the safe character set.

  • o, O, and 0 will all be recognised as 0
  • i, I, l, L, and1 will all be recognised as 1

And we remove the chance of generating most of the common ‘swear words’ by not using the letter U anywhere in the encoding

Example

  1. <?php
  2. include('class.base32.php5');
  3.  
  4. // I'm real lazy...
  5. header('Content-type: text/plain');
  6.  
  7. // Create a new Base32 object
  8. /*
  9. You could also call (for example)
  10.   $b = new Base32(Base32::csSafe);
  11. */
  12. $b = new Base32;
  13.  
  14. $instr = 'Hello World - How are we all today?';
  15. print "Base32::csRFC3548> Input string: $instr\n";
  16. $bstr = $b->fromString($instr);
  17. print "Base32::csRFC3548> Base32 string: $bstr\n";
  18. $outstr = $b->toString($bstr);
  19. print "Base32::csRFC3548> Output string: $outstr\n\n";
  20.  
  21. // Switch to using Base32::csSafe
  22. $b->setCharset(Base32::csSafe);
  23. print "Base32::csSafe> Input string: $instr\n";
  24. $bstr = $b->fromString($instr);
  25. print "Base32::csSafe> Base32 string: $bstr\n";
  26. $outstr = $b->toString($bstr);
  27. print "Base32::csSafe> Output string: $outstr\n\n";
  28.  
  29. // Switch to using Base32::cs09AV
  30. $b->setCharset(Base32::cs09AV);
  31. print "Base32::cs09AV> Input string: $instr\n";
  32. $bstr = $b->fromString($instr);
  33. print "Base32::cs09AV> Base32 string: $bstr\n";
  34. $outstr = $b->toString($bstr);
  35. print "Base32::cs09AV> Output string: $outstr\n\n";
  36.  
  37.  
  38. print "Now to demonstrate why Base32::csSafe is so handy\n";
  39. // Switch to using Base32::csSafe
  40. $b->setCharset(Base32::csSafe);
  41. $bstr = $b->fromString($instr);
  42. print "Here is the string you want the user to enter: $bstr\n";
  43.  
  44. // Pretend to be a human...
  45. $fstr = str_replace('1','L',$bstr);
  46. $fstr = str_replace('0','o',$fstr);
  47.  
  48. print "Here is the string the user has entered: $fstr\n";
  49. print "Note the 1 is an l and the 0 is an o\n";
  50. $outstr = $b->toString($fstr);
  51. print "Here is the output of the decoded string: $outstr\n\n";
  52.  
  53. // Now we show off
  54. print "\nOf course, you arn't restricted to the default character sets\n";
  55. // Must be 32 chars, and should be upper case...
  56. $chrset = '0987654321QPWOEIRUTYLAKSJDHFGMCZ';
  57.  
  58. print "Here's a new character set, perhaps you're generating passwords: $chrset\n";
  59. // Switch to customer character set
  60. $b->setCharset($chrset);
  61. print "Customer chrset> Input string: $instr\n";
  62. $bstr = $b->fromString($instr);
  63. print "Customer chrset> Base32 string: $bstr\n";
  64. $outstr = $b->toString($bstr);
  65. print "Customer chrset> Output string: $outstr\n";
  66.  
  67. Download this code: base32.example.php (Downloaded 561 time(s))

Download
File: class.base32.php5 [7.11 kB]
Download: 595

Comments

Spam no more - rel=nofollow is active here, spamming my comments will not help your page rank.

  Textile help
---== Copyright Shannon Wynter - All rights reserved - All wrongs avenged ==--- email