base32

RFC 4648 Base32, Crockford Base32 and Base58 encoders and decoders.

v1.0.0Security & encodingbundledimport "base32";
Overview Installation Documentation Examples

Overview

Zero-dependency RFC 4648 Base32, Crockford Base32, and Base58 (Bitcoin alphabet) encoders and decoders for HolyPHP.

bundled — works offline zero dependencies encode decode

Quick start

import "base32";

// RFC 4648 Base32
$b32 = base32_encode("foobar");          // "MZXW6YTBOI======"
$raw = base32_decode($b32);              // "foobar"

// Crockford Base32 (human-friendly, ignores hyphens, maps I/L->1 and O->0)
$crock = base32_crockford_encode("Hello"); // "91JPRV3F"
$raw2  = base32_crockford_decode("91JP-RV3F"); // "Hello"

// Base58 (Bitcoin alphabet)
$b58 = base58_encode("Hello World!");    // "2NEpo7TZRRrLZSi2U"
$raw3 = base58_decode($b58);             // "Hello World!"

Installation

In a project

Inside a project (a folder with hphp.yaml, see Projects & packages):

hphp install base32

This adds the library to hphp.yaml, installs it into .hphp/packages/base32/ and pins the exact version in hphp.lock:

require:
  base32: "^1.0"

Use it

import "base32";

base32 is bundled with the compiler, so import "base32"; also works in a single file without installing anything, and hphp install works offline.

Outside a project

hphp install base32          # for your user, shared by all scripts
hphp create myapp                 # or start a project, then: hphp install base32
Packagebase32
Version1.0.0 (follows the compiler)
Entry filebase32.hphp
Sourcepackages/lib/base32 ↗

Documentation

API Reference

RFC 4648 Base32

Crockford Base32

Base58

Error Handling

All decoders validate input strictly and throw Exception with clear, descriptive messages prefixed with base32::

Examples

01Quick start

Bundled with the compiler — import "base32"; works offline in any file, or add it to a project with hphp install base32.

import "base32";

// RFC 4648 Base32
$b32 = base32_encode("foobar");          // "MZXW6YTBOI======"
$raw = base32_decode($b32);              // "foobar"

// Crockford Base32 (human-friendly, ignores hyphens, maps I/L->1 and O->0)
$crock = base32_crockford_encode("Hello"); // "91JPRV3F"
$raw2  = base32_crockford_decode("91JP-RV3F"); // "Hello"

// Base58 (Bitcoin alphabet)
$b58 = base58_encode("Hello World!");    // "2NEpo7TZRRrLZSi2U"
$raw3 = base58_decode($b58);             // "Hello World!"
Try it
hphp install base32
hphp run main.hphp

Covered by the official test suite in tests/positive/lib_base32.hphp.